SQLServer 2008如何请求关于创建游标以遍历记录的示例?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (61)

有没有在创建游标以循环遍历一组记录方面的经验?

提问于
用户回答回答于
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)

扫码关注云+社区

领取腾讯云代金券

年度创作总结 领取年终奖励