因为xx表是变化的
所以存储过程中的select 语句只能写成这样
declare @title nvarchar(100)
set @sql='select @Title = title from '+@xx表+' where id=1'
exec(@sql)@title 在此存储过程下面还会用到,所以就定义了下。
实操的时候发现这个语句有问题,请问怎么才能将查询出来的title赋值给@title呢?
所以存储过程中的select 语句只能写成这样
declare @title nvarchar(100)
set @sql='select @Title = title from '+@xx表+' where id=1'
exec(@sql)@title 在此存储过程下面还会用到,所以就定义了下。
实操的时候发现这个语句有问题,请问怎么才能将查询出来的title赋值给@title呢?
set @cmd = 'select @title = title from [' + @xx表 + '] where id = 1';exec sp_executesql @cmd ,N'@title nvarchar(100) output',@title output;select @title;
set @sql='select '+ @title +'=title from '+ @xx表 +' where id=1'
exec(@sql)
select @title=title from 表名 where id=1
set方法是要求直接给一个值,不能接查询语句,上边才是正确的查询赋值
set @cmd = 'select @title = title from [' + @xx表 + '] where id = 1';exec sp_executesql @cmd ,N'@title nvarchar(100) output',@title output;select @title;