0
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

これは基本的に、個々のロケーション番号を返すカーソルを通過します。ロケーション番号は、カーソルからの変数として格納されます。これは、特定の回数(20)を繰り返すwhileループ内で使用する必要があります。返されるのは、ロケーション番号のリスト全体に対してカーソルが移動しているだけですが、updateステートメントでwhileループを繰り返すことはありません。

4

1 に答える 1

2

カーソルとwhileループは一般的に問題を解決するための間違った方法ですが、セットベースのソリューションを提案するためにあなたが何をしているのかを正確に理解する時間はありませんが、真剣にセットで考え始め、ループを考えるのをやめる必要があります。

ただし、問題は@iがnullであり、nullが<20ではないことです。

私の理論のこのテストを参照してください

declare @i int

if @i<20 print'hi'
else print 'bye'
于 2012-05-10T14:09:57.640 に答える