例如
exec (select Name from admin where id = 1)
执行结果是 :admin
declare @name varchar(20)
怎么把这个结果赋值给变量 @name?
------解决方案--------------------
改用sp_executesql
------解决方案--------------------
declare @name varchar(20),@tsql nvarchar(4000)
select @tsql=N'select @name=Name from admin where id=1'
exec sp_executesql @tsql,N'@name varchar(20) output',@name=@name output
select @name
------解决方案--------------------
这个建议用sp_executesql:
declare @name nvarchar(20)
exec sp_executesql N'select @name = Name from admin where id = @id',
N'@name varchar(20) output,@id int',
@name out,1
select @name
------解决方案--------------------
DECLARE @ta TABLE (NAME varchar(50))
DECLARE @NAME varchar(50)
INSERT @ta EXEC('select Name from admin where id = 1')
SELECT @NAME=NAME FROM @ta