我的 SQL 查询有问题。我需要使用 EXEC 执行 SQL 语句,并且其中一列已经用变量表示,现在我必须用另一个变量来表示该列的值。这可以实现吗?如果可以,您能建议其他解决方案吗?
我通过 Google 找到了这个片段。但即使这样也没有给我想要的输出。
我的SQL语句:
declare @i int, @j int =10
exec sp_executesql N'select @i ='+ @j, N'@i int output', @i output
select @i
最佳答案
这在我的 Sql Server 2005 中有效:
declare @i int, @j int
set @j =10
declare @sql nvarchar(max)
set @sql = 'select @i =' + str(@j)
exec sp_executesql @sql, N'@i int output', @i output
select @i
关于sql - EXEC Select 语句与动态列将值存储到变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10028460/