请问能不能将'11','222'作为一个参数传递给存储过程。''11'',''222''好像不行的(200分)

C

cxz9

Unregistered / Unconfirmed
GUEST, unregistred user!

请问能不能将'11','222'作为一个参数传递给存储过程。''11'',''222''好像不行的

if @strs<>''
begin
这儿打出来是对的但就是不能执行。
--print 'select xh,tdh from tdzxd where tybh in ('+@strs+')'
if (@st='TY')
begin
declare xh_cur cursor for select xh,tdh from tdzxd where tybh in (@strs)
select @m=count(distinct cs) from tdzxd where tybh in (@strs)
end
else if (@st='BL')
begin
declare xh_cur cursor for select xh,tdh from tdzxd where tdh in (@strs)
select @m=count(distinct cs) from tdzxd where tdh in (@strs)
end
else if (@st='CN')
begin
declare xh_cur cursor for select xh,tdh from tdzxd where xh in (@strs)
select @m=count(distinct cs) from tdzxd where xh in (@strs)
end

--print @runs
--exec (@runs)

--WAITFOR DELAY '000:00:01'

open xh_cur
fetch next from xh_cur into @xh,@bl
while @@fetch_status=0
begin
if (@xh<>'') and (charindex(@xh,@xhs)=0)
set @xhs=@xhs+''''+@xh+''','

-- if @bl<>''
if (@bl<>'') and (charindex(@bl,@bls)=0)
set @bls=@bls+''''+@bl+''','

fetch next from xh_cur into @xh,@bl
end
close xh_cur
deallocate xh_cur
这样也不行
begin
set @runs='declare xh_cur cursor for select xh,tdh from tdzxd where xh in ('+@strs+')
select @m=count(distinct cs) from tdzxd where xh in ('+@strs+')
go'
end

--commit tran
print @runs
exec (@runs)

--WAITFOR DELAY '000:00:01'

open xh_cur
fetch next from xh_cur into @xh,@bl
while @@fetch_status=0
begin



 
试一下'''11''','''222'''
 
要加双引号
SQL Server中和Delphi中的引号的使用方法是一样的
'11'-->'''11'''
'222'-->'''222'''
 
来晚了,楼上二位的
 
>>这儿打出来是对的但就是不能执行。
>>--print 'select xh,tdh from tdzxd where tybh in ('+@strs+')'
将'11'代入,得到
select xh, tdh from tdzxd where tybh in (11)
这个SQL能得到正确结果?
所以应该是
--print 'select xh, tdh from tdzxd where tybh in (''' + @strs + ''')'

>>declare xh_cur cursor for select xh,tdh from tdzxd where tybh in (@strs)
declare @curstr varchar(100)
set @curstr = 'declare xh_cur cursor for select xh,tdh from tdzxd where tybh in (''' + @strs + ''')'
....
exec(@curstr)

 
把你的''111''换成Quotedstr('111')实验
 
谢谢各位,其实一开始就是正确的。
是因为游标不能动态定义的
alter PROCEDURE t2
@st varchar(10),
@strs varchar(200)
AS

declare @xhs varchar(200),@bls varchar(200),@xh varchar (200),@bl varchar (200),
@k int,@num int,@m int,@tt varchar(200),@runs varchar(1000)

if @strs<>''
begin
print 'select xh,tdh from tdzxd where tybh in ('+@strs+')'
set @runs='select xh,tdh from tdzxd where tybh in ('+@strs+')'
exec (@runs)
end

t2 'a','''KE0044634C'',''A25'''


 
多人接受答案了。
 
顶部