当前位置: 代码迷 >> Sql Server >> 一个不知道是难还是不难的有关问题`麻烦高手了`
  详细解决方案

一个不知道是难还是不难的有关问题`麻烦高手了`

热度:74   发布时间:2016-04-27 19:39:25.0
一个不知道是难还是不难的问题``麻烦高手了```
declare   cro_fastread   cursor   scroll
        for     后面跟参数可以吗`??

本来是:
        declare   cro_fastread   cursor   scroll
        for         select   Pr_id   from   Product   where   tint_level=0   and   [email protected]_tableid   order   by   int_id   desc

[email protected],[email protected]= "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 [email protected]_tableid ' +
CASE WHEN isnull(@x1, ' ') = ' ' THEN ' ' ELSE ' and ' + @x1 END + [email protected]*/
' 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
  相关解决方案