How to get sp_executesql result into a variable?

后端 未结 10 1432
刺人心
刺人心 2020-11-22 16:06

I have a piece of dynamic SQL I need to execute, I then need to store the result into a variable.

I know I can use sp_executesql but can\'t find clear e

相关标签:
10条回答
  • 2020-11-22 17:04

    Return values are generally not used to "return" a result but to return success (0) or an error number (1-65K). The above all seem to indicate that sp_executesql does not return a value, which is not correct. sp_executesql will return 0 for success and any other number for failure.

    In the below, @i will return 2727

    DECLARE @s NVARCHAR(500)
    DECLARE @i INT;
    SET @s = 'USE [Blah]; UPDATE STATISTICS [dbo].[TableName] [NonExistantStatisticsName];';
    EXEC @i = sys.sp_executesql @s
    SELECT @i AS 'Blah'
    

    SSMS will show this Msg 2727, Level 11, State 1, Line 1 Cannot find index 'NonExistantStaticsName'.

    0 讨论(0)
  • 2020-11-22 17:05

    This was a long time ago, so not sure if this is still needed, but you could use @@ROWCOUNT variable to see how many rows were affected with the previous sql statement.

    This is helpful when for example you construct a dynamic Update statement and run it with exec. @@ROWCOUNT would show how many rows were updated.

    Here is the definition

    0 讨论(0)
  • 2020-11-22 17:06

    Here's something you can try

    DECLARE  @SqlStatement  NVARCHAR(MAX) = ''
           ,@result     XML
           ,@DatabaseName  VARCHAR(100)
           ,@SchemaName    VARCHAR(10)
           ,@ObjectName    VARCHAR(200);
    
    SELECT   @DatabaseName = 'some database'
           ,@SchemaName   = 'some schema'
           ,@ObjectName   = 'some object (Table/View)'
    
    SET @SqlStatement = '
                        SELECT @result = CONVERT(XML,
                                                STUFF( ( SELECT *
                                                         FROM 
                                                           (
                                                              SELECT TOP(100) 
                                                              * 
                                                              FROM ' + QUOTENAME(@DatabaseName) +'.'+ QUOTENAME(@SchemaName) +'.' + QUOTENAME(@ObjectName) + '
                                                           ) AS A1 
                                                        FOR XML PATH(''row''), ELEMENTS, ROOT(''recordset'')
                                                     ), 1, 0, '''')
                                           )
                    ';
    
    EXEC sp_executesql @SqlStatement,N'@result XML OUTPUT', @result = @result OUTPUT;
    
    SELECT DISTINCT
        QUOTENAME(r.value('fn:local-name(.)', 'VARCHAR(200)')) AS ColumnName
    FROM @result.nodes('//recordset/*/*') AS records(r)
    ORDER BY ColumnName
    
    0 讨论(0)
  • 2020-11-22 17:08
    DECLARE @tab AS TABLE (col1 VARCHAR(10), col2 varchar(10)) 
    INSERT into @tab EXECUTE  sp_executesql N'
    SELECT 1 AS col1, 2 AS col2
    UNION ALL
    SELECT 1 AS col1, 2 AS col2
    UNION ALL
    SELECT 1 AS col1, 2 AS col2'
    
    SELECT * FROM @tab
    
    0 讨论(0)
提交回复
热议问题