Assign result of dynamic sql to variable

前端 未结 5 1100
难免孤独
难免孤独 2020-11-30 06:20

I\'m doing dynamic SQL to convert all columns in a table a string

so After after all I do

EXEC(@template); 

where @template is the

5条回答
  •  难免孤独
    2020-11-30 06:52

    You could use sp_executesql instead of exec. That allows you to specify an output parameter.

    declare @out_var varchar(max);
    execute sp_executesql 
        N'select @out_var = ''hello world''', 
        N'@out_var varchar(max) OUTPUT', 
        @out_var = @out_var output;
    select @out_var;
    

    This prints "hello world".

提交回复
热议问题