34,838
社区成员




create table tb(id int,num varchar(20))
insert into tb select 1,2
insert into tb select 2,3
DECLARE @TB nvarchar(50)
Declare @SQ nvarchar(50)
declare @num varchar(50)
SET @TB='tb'
SET @SQ='select @num=num from '+@TB +' where id=1'
exec sp_executesql @SQ,N'@num varchar(50) output',@num output
select @num
set @sq='select ' + @aa + '=num from ' + @tb + ' where id=1'
@aa未赋值,为null
因此 @sq得到null
exec(@sq) 即 exec一个null字串,未进行任何操作.
@aa,@sq均为null
insert tb4(col,tt) select @aa,@sq
所以插入的是null
不知道你想实现什么,还是说出来,想想给你怎么改.