当前位置: 代码迷 >> Sql Server >> 关于存储过程字符串的构造解决方法
  详细解决方案

关于存储过程字符串的构造解决方法

热度:60   发布时间:2016-04-27 18:49:09.0
关于存储过程字符串的构造
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   '[email protected]

set   @total=exec(@sqlStr)

[email protected]?
请各位指教一下

------解决方案--------------------
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 @[email protected] from #tmp
exec(@sql)
print @total
------解决方案--------------------
declare @total int
declare @str Nvarchar(500)

set @str= 'select @total=count(0) from tab_1 where '[email protected]
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
  相关解决方案