--------------------利用游标对查询的数据集进行遍历-----------------------
DECLARE @gcode VARCHAR(10) ,@name VARCHAR(50),@price CHAR(10)DECLARE sel CURSOR SCROLL FORSELECT c_gcode,c_name,c_price FROM tb_gds WHERE c_gcode IN ('000001','000002','000003')ORDER BY c_gcode;OPEN sel
FETCH NEXT FROM sel INTO @gcode,@name,@price
WHILE @@FETCH_STATUS=0BEGINPRINT '商品编码:'+@gcodePRINT '商品名称:'+@namePRINT '商品价格:'+@pricePRINT '................'FETCH NEXT FROM sel INTO @gcode,@name,@price
ENDCLOSE sel
DEALLOCATE sel
-------------------利用游标修改、删除数据--------------------------BEGIN TRAN
DECLARE update_gdsstore CURSOR SCROLL FOR
SELECT c_gcode,c_price,c_store_id FROM dbo.tb_gdsstore WHERE c_gcode IN ('000001','000002','000003') FOR UPDATE OF c_priceOPEN update_gdsstore
DECLARE @ROWCOUNT INTSET @rowcount=1FETCH ABSOLUTE @rowcount FROM update_gdsstoreUPDATE dbo.tb_gdsstore SET c_price='12' WHERE CURRENT OF update_gdsstoreSET @ROWCOUNT=@ROWCOUNT+1
FETCH NEXT FROM update_gdsstore
UPDATE dbo.tb_gdsstore SET c_price='11' WHERE CURRENT OF update_gdsstoreSET @ROWCOUNT=@ROWCOUNT+1
FETCH NEXT FROM update_gdsstore
UPDATE dbo.tb_gdsstore SET c_price='10' WHERE CURRENT OF update_gdsstoreCLOSE update_gdsstore
DEALLOCATE update_gdsstore
ROLLBACK
-----------------------------利用游标对查询的数据集进行遍历-------------------------------
DECLARE @ggcode VARCHAR(10),@gname VARCHAR(50),@produce VARCHAR(30)DECLARE get_info CURSOR SCROLL FORSELECT c_gcode,c_name,c_produce FROM tb_gds WHERE c_adno='201'ORDER BY c_gcode;OPEN get_info
FETCH NEXT FROM get_info INTO @ggcode,@gname,@produce
WHILE @@FETCH_STATUS=0BEGINPRINT '商品编码:'+@ggcodePRINT '商品名称:'+@gnamePRINT '商品产地:'+@producePRINT '................'FETCH NEXT FROM get_info INTO @ggcode,@gname,@produce
ENDCLOSE get_info
DEALLOCATE get_info