欢迎来到天天文库
浏览记录
ID:38419882
大小:43.50 KB
页数:4页
时间:2019-06-12
《游标使用事例》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、示例A.在简单的游标中使用FETCH下例为authors表中姓以字母B开头的行声明了一个简单的游标,并使用FETCHNEXT逐个提取这些行。FETCH语句以单行结果集形式返回由DECLARECURSOR指定的列的值。USEpubsGODECLAREauthors_cursorCURSORFORSELECTau_lnameFROMauthorsWHEREau_lnameLIKE"B%"ORDERBYau_lnameOPENauthors_cursor--Performthefirstfetch.FETCHNEXTFROMauthors_cursor--Check@@
2、FETCH_STATUStoseeifthereareanymorerowstofetch.WHILE@@FETCH_STATUS=0BEGIN--Thisisexecutedaslongasthepreviousfetchsucceeds.FETCHNEXTFROMauthors_cursorENDCLOSEauthors_cursorDEALLOCATEauthors_cursorGOau_lname----------------------------------------Bennetau_lname---------------------------
3、-------------Blotchet-Hallsau_lname----------------------------------------B.使用FETCH将值存入变量下例与上例相似,但FETCH语句的输出存储于局部变量而不是直接返回给客户端。PRINT语句将变量组合成单一字符串并将其返回到客户端。USEpubsGO--DeclarethevariablestostorethevaluesreturnedbyFETCH.DECLARE@au_lnamevarchar(40),@au_fnamevarchar(20)DECLAREauthors_curs
4、orCURSORFORSELECTau_lname,au_fnameFROMauthorsWHEREau_lnameLIKE"B%"ORDERBYau_lname,au_fnameOPENauthors_cursor--Performthefirstfetchandstorethevaluesinvariables.--Note:Thevariablesareinthesameorderasthecolumns--intheSELECTstatement.FETCHNEXTFROMauthors_cursorINTO@au_lname,@au_fname--Che
5、ck@@FETCH_STATUStoseeifthereareanymorerowstofetch.WHILE@@FETCH_STATUS=0BEGIN--Concatenateanddisplaythecurrentvaluesinthevariables.PRINT"Author:"+@au_fname+""+@au_lname--Thisisexecutedaslongasthepreviousfetchsucceeds.FETCHNEXTFROMauthors_cursorINTO@au_lname,@au_fnameENDCLOSEauthors_cur
6、sorDEALLOCATEauthors_cursorGOAuthor:AbrahamBennetAuthor:ReginaldBlotchet-HallsC.声明SCROLL游标并使用其它FETCH选项下例创建一个SCROLL游标,使其通过LAST、PRIOR、RELATIVE和ABSOLUTE选项支持所有滚动能力。USEpubsGO--ExecutetheSELECTstatementalonetoshowthe--fullresultsetthatisusedbythecursor.SELECTau_lname,au_fnameFROMauthorsORDE
7、RBYau_lname,au_fname--Declarethecursor.DECLAREauthors_cursorSCROLLCURSORFORSELECTau_lname,au_fnameFROMauthorsORDERBYau_lname,au_fnameOPENauthors_cursor--Fetchthelastrowinthecursor.FETCHLASTFROMauthors_cursor--Fetchtherowimmediatelypriortothecurrentrowinthecursor.FETCHPRIORFROMauthors_
8、curso
此文档下载收益归作者所有