游标使用事例.doc_第1页
游标使用事例.doc_第2页
游标使用事例.doc_第3页
游标使用事例.doc_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

示例A. 在简单的游标中使用 FETCH下例为 authors 表中姓以字母 B 开头的行声明了一个简单的游标,并使用 FETCH NEXT 逐个提取这些行。FETCH 语句以单行结果集形式返回由 DECLARE CURSOR 指定的列的值。USE pubsGODECLARE authors_cursor CURSOR FORSELECT au_lname FROM authorsWHERE au_lname LIKE B%ORDER BY au_lnameOPEN authors_cursor- Perform the first fetch.FETCH NEXT FROM authors_cursor- Check FETCH_STATUS to see if there are any more rows to fetch.WHILE FETCH_STATUS = 0BEGIN - This is executed as long as the previous fetch succeeds. FETCH NEXT FROM authors_cursorENDCLOSE authors_cursorDEALLOCATE authors_cursorGOau_lname - Bennet au_lname - Blotchet-Halls au_lname -B. 使用 FETCH 将值存入变量下例与上例相似,但 FETCH 语句的输出存储于局部变量而不是直接返回给客户端。PRINT 语句将变量组合成单一字符串并将其返回到客户端。USE pubsGO- Declare the variables to store the values returned by FETCH.DECLARE au_lname varchar(40), au_fname varchar(20)DECLARE authors_cursor CURSOR FORSELECT au_lname, au_fname FROM authorsWHERE au_lname LIKE B%ORDER BY au_lname, au_fnameOPEN authors_cursor- Perform the first fetch and store the values in variables.- Note: The variables are in the same order as the columns- in the SELECT statement. FETCH NEXT FROM authors_cursorINTO au_lname, au_fname- Check FETCH_STATUS to see if there are any more rows to fetch.WHILE FETCH_STATUS = 0BEGIN - Concatenate and display the current values in the variables. PRINT Author: + au_fname + + au_lname - This is executed as long as the previous fetch succeeds. FETCH NEXT FROM authors_cursor INTO au_lname, au_fnameENDCLOSE authors_cursorDEALLOCATE authors_cursorGOAuthor: Abraham BennetAuthor: Reginald Blotchet-HallsC. 声明 SCROLL 游标并使用其它 FETCH 选项下例创建一个 SCROLL 游标,使其通过 LAST、PRIOR、RELATIVE 和 ABSOLUTE 选项支持所有滚动能力。USE pubsGO- Execute the SELECT statement alone to show the - full result set that is used by the cursor.SELECT au_lname, au_fname FROM authorsORDER BY au_lname, au_fname- Declare the cursor.DECLARE authors_cursor SCROLL CURSOR FORSELECT au_lname, au_fname FROM authorsORDER BY au_lname, au_fnameOPEN authors_cursor- Fetch the last row in the cursor.FETCH LAST FROM authors_cursor- Fetch the row immediately prior to the current row in the cursor.FETCH PRIOR FROM authors_cursor- Fetch the second row in the cursor.FETCH ABSOLUTE 2 FROM authors_cursor- Fetch the row that is three rows after the current row.FETCH RELATIVE 3 FROM authors_cursor- Fetch the row that is two rows prior to the current row.FETCH RELATIVE -2 FROM authors_cursorCLOSE authors_cursorDEALLOCATE authors_cursorGOau_lname au_fname - - Bennet Abraham Blotchet-Halls Reginald Carson Cheryl DeFrance Michel del Castillo Innes Dull Ann Green Marjorie Greene Morningstar Gringlesby Burt Hunter Sheryl Karsen Livia Locksley Charlene MacFeather Stearns McBadden Heather OLeary Michael Panteley Sylvia Ringer Albert Ringer Anne Smith Meander Straight Dean Stringer Dirk White Johnson Yokomoto Akiko au_lname au_fname - - Yokomoto Akiko au_lname

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论