17

一連のレコードをループするカーソルを作成した経験はありますか? もしそうなら、基本的な例を教えてください。ありがとう

4

1 に答える 1

27
declare cur cursor for 
select id from tbl 
open cur
declare @id int
fetch next from cur into @id
while (@@FETCH_STATUS = 0)
begin
    print(@id)
    fetch next from cur into @id
end
close cur
deallocate cur

-- just replace "tbl" with your table name and "id" with your field name
-- and do whatever you want in begin-end block (now it simply prints the id of each record)
于 2012-08-08T03:11:54.187 に答える