关于存储过程字符串的构造
declare @total int;
declare @str varchar(50);
--原来是我想这样的
--select @total=count(0) from tab_1 where @str
--原来是不行
--于是我就改为
declare @sqlStr varchar(500)
set @sqlStr= 'select count(0) from tab_1 where '+@str
set @total=exec(@sqlStr)
不知道怎样才sqlStr的结果赋值到@total变量呢?
请各位指教一下
------解决方案--------------------create table #tmp
(
total int
)
declare @total nvarchar(100)
declare @sql varchar(4000)
set @sql= '
insert into #tmp select count(1) from xyza where portid=1 '
select @total=@total from #tmp
exec(@sql)
print @total
------解决方案--------------------declare @total int
declare @str Nvarchar(500)
set @str= 'select @total=count(0) from tab_1 where '+@str
exec sp_executesql @str, '@total int out ',@total out
select @total
------解决方案--------------------set @sqls = 'select @a=count(*) from [ ' + @BtoStr0 + '] where ' + @BtoStr1
else
set @sqls = 'select @a=count(*) from [ ' + @BtoStr0 + '] '
exec sp_executesql @sqls,N '@a int output ',@num output