Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2004
    Posts
    12

    Unanswered: How to get a result from a execute()

    The Transact-SQL statements in the executed string do not have access to any of the variables declared in the batch that contains the sp_executesql or EXECUTE statement. The batch containing the sp_executesql or EXECUTE statement does not have access to variables or local cursors defined in the executed string

    but I do not like to create a store procedure to get a output.
    How can I get result from execute('...') like this:
    declare @result int
    set @result=0
    execute('select @result=count(*) from table_cost')

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    You can use:
    Code:
    DECLARE @i		INT
    
    SET @i = 0		-- Just to prove something happened
    
    EXECUTE sp_executesql N'SET @i = 1', N'@i INT OUTPUT', @i OUTPUT
    
    SELECT @i
    -PatP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •