sqlserver中使用游标示例

游标使用示例:

使用步骤:
1、定义游标
2打开游标
3提取数据
4关闭数据
例子:

    取出CategoryID='1' 的商品的编号和名称:

游标使用示例
declare ProductCursor cursor
for select ProductID,ProductName from Products where CategoryID='1'
declare @ProductID char(4)
declare @ProductName char(200)
fetch from ProductCursor into @ProductID,@PRoductName
while @@FETCH_STATUS=0
begin
if @ProductID='1'
print 'ProductID:'+@ProductID+ +'ProductName:'+@ProductName
break
--fetch next from ProductCursor into @ProductID,@ProductName
end


运行结果:

 

posted @ 2012-04-02 16:34  木子易  Views(275)  Comments(0Edit  收藏  举报