日期:2014-05-18  浏览次数:20564 次

请教一个存储过程参数处理问题
有一个存储过程,代码如下
SQL code

CREATE procedure [dbo].[BBB] 
@spid varchar(8000)
as

select * from BI_SPZL where spid in(@spid)



当传递一个参数'''SP000000193'',''SP000000202'''时,
exec BBB '''SP000000193'',''SP000000202''' 
查询未能得出结果,但是执行下面这条语句,可以得出结果
select * from BI_SPZL where spid in('SP000000193','SP000000202')

请问怎么处理参数,才能让存储过程返回正确的结果集?

------解决方案--------------------
CREATE procedure [dbo].[BBB] 
@spid varchar(8000)
as
declare @sql varchar(8000)
set @sql = 'select * from BI_SPZL where spid in('+@spid+')'
exec(@sql)
------解决方案--------------------
SQL code

CREATE PROCEDURE [dbo].[BBB] @spid VARCHAR(8000)
AS 
    DECLARE @sql VARCHAR(8000)
    SET @sql = 'select * from BI_SPZL where spid in(' + @spid + ')'
    EXEC ( @sql  )

------解决方案--------------------
你这样传递参数,相当于让字符串SP000000193与字符串'SP000000193'或字符串'SP000000202'去比,
两者当然不相等
改成下面这样
SQL code
CREATE procedure [dbo].[BBB] 
@spid varchar(8000)
as

exec('select * from BI_SPZL where spid in('+@spid+')')