如何执行拼接的sql语句,并把查询结果赋值给变量
@NewTicket是我声明的变量,sql语句是我拼接的,我当前是这么写的,并不能赋值给变量@NewTicket
exec ('select @NewTicket = count(t.tid) from Ticket as t' + @StrWhere + ' and t.TState = 1')
各位大师指教一下吧!
sql
拼接sql
存储过程
存储过程中变量赋值
------解决方案--------------------
declare @sql nvarchar(1000)
set @sql='select @NewTicket = count(t.tid) from Ticket as t ' + @StrWhere + ' and t.TState = 1'
exec sp_executesql @sql,N'@NewTicket int out',@NewTicket out
select @NewTicket
------解决方案--------------------使用sp_executesql output 返回,
参考http://msdn.microsoft.com/zh-cn/library/ms188001.aspx
------解决方案--------------------上面例子写的好好的,你就没看出你写的有什么差异?
set @sql='select
@内部变量 = count(t.tid) from Ticket as t ' + @StrWhere + ' and t.TState = 1'
exec sp_executesql @sql,N'@内部变量 int out',@NewTicket out