悉野小楼

导航

[转]sql利用游标循环,遍历表循环结果集

用  游标(Cursor) + While循环 的方法, 对Customers表中的CompanyName列进行遍历


declare @customer nvarchar(50)


declare pcurr cursor for
select distinct companyname from customers

open pcurr
fetch next from pcurr into @customer


while (@@fetch_status = 0)
begin


 print (@customer)
 fetch next from pcurr into @customer


end

close pcurr
deallocate pcurr 

 

 

posted on 2013-06-20 16:35  悉野  阅读(267)  评论(1编辑  收藏  举报