本文转载自:http://blog.csdn.net/xiaoxu0123/article/details/5684680
[SQL Server] 动态sql给变量赋值(或返回值给变量)
declare @i_counts int, @i_times int; set @str_sql = 'select @tmp_counts = counts, @tmp_times=times ' + ' from ' +@str_dbname+ '.dbo.t_msisdn with(nolock) '; exec sp_executesql @str_sql,'@tmp_counts int output, @tmp_times int output', @tmp_counts = @i_counts output, --分数 @tmp_times=@i_times output; --时间 select @i_counts as 'counts', @i_times as 'times'