Sqlserver游标的使用

137 阅读1分钟

Sqlserver游标的使用

###用于循环表里的数据


  declare @x varchar(50);
--定义游标
declare tempIndex cursor for (SELECT orgid from WX_StoreInfos) 
--打开游标
open tempIndex  
--抓取下一行数据给变量,一个或者多个
fetch next from tempIndex into @x
--进行循环,0表示抓取成功,1表示抓取失败,2表示不存在抓取行
while @@fetch_status=0  
begin
-- todo
print @x


fetch next from tempIndex into @x
end
close tempIndex  --关闭游标
deallocate tempIndex  --释放游标