首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >游标While循环内的while循环- SQL

游标While循环内的while循环- SQL
EN

Stack Overflow用户
提问于 2012-05-10 22:04:40
回答 1查看 10.8K关注 0票数 0
代码语言:javascript
复制
declare @i int
declare @skool float

declare schoolCursor cursor for  
select distinct choice from tempstuas

open schoolCursor
fetch next from schoolCursor into @skool
while @@fetch_status = 0
begin
        while @i < 20
            begin
                update top(1) tempstuas set cnt=@i where cnt = '' and cat = 1 and choice=@skool
                update top(1) tempstuas set cnt=@i where cnt = '' and cat = 2 and choice=@skool
                update top(1) tempstuas set cnt=@i where cnt = '' and cat=3 and choice=@skool

                set @i = @i + 1
            end
        fetch next from schoolCursor 
end
close schoolCursor
deallocate schoolCursor

这基本上是通过一个光标返回一个单独的位置编号。位置数字被存储为游标中的一个变量,我需要在一个while循环中使用它,该循环迭代特定的次数(20)。返回给我的只是整个位置号码列表的游标,而不是使用update语句遍历while循环。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-10 22:09:58

虽然游标和循环通常是解决问题的错误方法,但我现在没有时间弄清楚您到底在做什么来建议基于集合的解决方案,但说真的,您需要开始在集合中思考,并停止思考循环。

但是你的问题是@i是null,null不是<20。

看看这个测试我的理论

代码语言:javascript
复制
declare @i int

if @i<20 print'hi'
else print 'bye'
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10535698

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档