SQL Server 2008请求关于创建游标以循环遍历记录的示例

时间:2012-08-08 01:41:27

标签: sql-server-2008

是否有任何经验可以创建游标来循环遍历一堆记录?如果你这样做,你能为我提供一个基本的例子。感谢

1 个答案:

答案 0 :(得分: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)