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

一个不知道是难还是不难的问题``麻烦高手了```
declare   cro_fastread   cursor   scroll
        for     后面跟参数可以吗`??

本来是:
        declare   cro_fastread   cursor   scroll
        for         select   Pr_id   from   Product   where   tint_level=0   and   tint_tableid=@tint_tableid   order   by   int_id   desc

可是现在我想放进一个条件``@xl,里面是一段的字符串``就@xl= "id=1   and   name= 'aaa ' "

有人帮助下吗`> > ??


------解决方案--------------------
可以使用动态游标,因为游标可以使用EXEC()动态生成然后引用:
declare @sql varchar(8000),@x1 varchar(100)
----设置参数
set @x1 = 'id=1 and name= ' 'aaa ' ' '
----定义动态游标字符串
set @sql = '
declare cro_fastread cursor scroll
for select Pr_id from Product where tint_level=0 and tint_tableid=@tint_tableid ' +
CASE WHEN isnull(@x1, ' ') = ' ' THEN ' ' ELSE ' and ' + @x1 END + /*@x1参数*/
' order by int_id desc '
----创建动态游标
EXEC(@sql)
----打开游标
open cro_fastread
fetch next from cro_fastread into ...
----循环游标
while @@fetch_status = 0
begin
...
fetch next from cro_fastread into ...
end
----关闭游标
close cro_fastread
----释放游标资源
deallocate cro_fastread