How to get sp_executesql result into a variable?

后端 未结 10 1442
刺人心
刺人心 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 16:53

    This worked for me:

    DECLARE @SQL NVARCHAR(4000)
    
    DECLARE @tbl Table (
        Id int,
        Account varchar(50),
        Amount int
    ) 
    
    -- Lots of code to Create my dynamic sql statement
    
    insert into @tbl EXEC sp_executesql @SQL
    
    select * from @tbl
    

提交回复
热议问题