当前位置: 代码迷 >> Sql Server >> SQL存储过程给变量赋值,该怎么解决
  详细解决方案

SQL存储过程给变量赋值,该怎么解决

热度:68   发布时间:2016-04-27 13:55:23.0
SQL存储过程给变量赋值
declare @str nvarchar(max)
declare @tem int


set @str=' and type=1 or sort>20 '
set @tem=select sum(id) from order where [email protected]+ and Pid=20


提示报错,请高手指教?

------解决方案--------------------
SQL code
create table #order(id int,type int,sort int,Pid int)insert into #order(id,type,sort,Pid)select 1,1,20,20  union allselect 2,1,20,20  union allselect 3,1,20,20  union allselect 4,1,20,20  union allselect 5,0,20,20  union allselect 6,0,21,20  select * from #orderdeclare @str nvarchar(max)declare @tem intset @str=' type=1 or sort>20 'set @str=N'select @tem_1=sum(id) from #order where ' [email protected]+ N' and Pid=20'print @strexec sp_executesql @str,[email protected]_1 int output',@[email protected] outputselect @tem--orset @str=' type=1 or sort>20 'set @str=N'select sum(id) from #order where ' [email protected]+ N' and Pid=20'declare @tb_value table(total int)insert into @tb_value(total)exec sp_sqlexec @strselect @tem=total from @tb_valueselect @tem
  相关解决方案