游标FETCH的应用和实例
2008-04-18 10:29
239 查看
FETCH
从 Transact-SQL 服务器游标中检索特定的一行。语法
FETCH
[ [ NEXT | PRIOR | FIRST | LAST
| ABSOLUTE { n | @nvar }
| RELATIVE { n | @nvar }
]
FROM
]
{ { [ GLOBAL ] cursor_name } | @cursor_variable_name }
[ INTO @variable_name [ ,...n ] ]
参数
NEXT
返回紧跟当前行之后的结果行,并且当前行递增为结果行。如果 FETCH NEXT 为对游标的第一次提取操作,则返回结果集中的第一行。NEXT 为默认的游标提取选项。
PRIOR
返回紧临当前行前面的结果行,并且当前行递减为结果行。如果 FETCH PRIOR 为对游标的第一次提取操作,则没有行返回并且游标置于第一行之前。
FIRST
返回游标中的第一行并将其作为当前行。
LAST
返回游标中的最后一行并将其作为当前行。
ABSOLUTE {n | @nvar}
如果 n 或 @nvar 为正数,返回从游标头开始的第 n 行并将返回的行变成新的当前行。如果 n 或 @nvar 为负数,返回游标尾之前的第 n 行并将返回的行变成新的当前行。如果 n 或 @nvar 为 0,则没有行返回。n 必须为整型常量且 @nvar 必须为 smallint、tinyint 或 int。
RELATIVE {n | @nvar}
如果 n 或 @nvar 为正数,返回当前行之后的第 n 行并将返回的行变成新的当前行。如果 n 或 @nvar 为负数,返回当前行之前的第 n 行并将返回的行变成新的当前行。如果 n 或 @nvar 为 0,返回当前行。如果对游标的第一次提取操作时将 FETCH RELATIVE 的 n 或 @nvar 指定为负数或 0,则没有行返回。n 必须为整型常量且 @nvar 必须为 smallint、tinyint 或 int。
GLOBAL
指定 cursor_name 指的是全局游标。
cursor_name
要从中进行提取的开放游标的名称。如果同时有以 cursor_name 作为名称的全局和局部游标存在,若指定为 GLOBAL 则 cursor_name 对应于全局游标,未指定 GLOBAL 则对应于局部游标。
@cursor_variable_name
游标变量名,引用要进行提取操作的打开的游标。
INTO @variable_name[,...n]
允许将提取操作的列数据放到局部变量中。列表中的各个变量从左到右与游标结果集中的相应列相关联。各变量的数据类型必须与相应的结果列的数据类型匹配或是结果列数据类型所支持的隐性转换。变量的数目必须与游标选择列表中的列的数目一致。
注释
如果 SCROLL 选项未在 SQL-92 样式的 DECLARE CURSOR 语句中指定,则 NEXT 是唯一受支持的 FETCH 选项。如果在 SQL-92 样式的 DECLARE CURSOR 语句中指定了 SCROLL 选项,则支持所有的 FETCH 选项。
如果使用 Transact_SQL DECLARE 游标扩展,以下规则适用:
如果指定了 FORWARD-ONLY 或 FAST_FORWARD,NEXT 是唯一受支持的 FETCH 选项。
如果未指定 DYNAMIC、FORWARD-ONLY 或 FAST_FORWARD 选项,并且指定了 KEYSET、STATIC 或 SCROLL 中的某一个,则支持所有 FETCH 选项。
DYNAMIC SCROLL 支持除 ABSOLUTE 之外的所有 FETCH 选项。
@@FETCH_STATUS 函数报告上一个 FETCH 语句的状态。相同的信息记录于由 sp_describe_cursor 返回的游标中的 fetch_status 列中。这些状态信息应该用于在对由 FETCH 语句返回的数据进行任何操作之前,以确定这些数据的有效性。有关更多信息,请参见 @@FETCH_STATUS。
权限
FETCH 的默认权限为任何合法用户。
示例
A. 在简单的游标中使用 FETCH
下例为 authors 表中姓以字母 B 开头的行声明了一个简单的游标,并使用 FETCH NEXT 逐个提取这些行。FETCH 语句以单行结果集形式返回由 DECLARE CURSOR 指定的列的值。
USE pubs GO DECLARE authors_cursor CURSOR FOR SELECT au_lname FROM authors WHERE au_lname LIKE "B%" ORDER BY au_lname OPEN 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 = 0 BEGIN -- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM authors_cursor END CLOSE authors_cursor DEALLOCATE authors_cursor GO au_lname ---------------------------------------- Bennet au_lname ---------------------------------------- Blotchet-Halls au_lname ----------------------------------------
B. 使用 FETCH 将值存入变量
下例与上例相似,但 FETCH 语句的输出存储于局部变量而不是直接返回给客户端。PRINT 语句将变量组合成单一字符串并将其返回到客户端。
USE pubs GO -- Declare the variables to store the values returned by FETCH. DECLARE @au_lname varchar(40), @au_fname varchar(20) DECLARE authors_cursor CURSOR FOR SELECT au_lname, au_fname FROM authors WHERE au_lname LIKE "B%" ORDER BY au_lname, au_fname OPEN 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_cursor INTO @au_lname, @au_fname -- Check @@FETCH_STATUS to see if there are any more rows to fetch. WHILE @@FETCH_STATUS = 0 BEGIN -- 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_fname END CLOSE authors_cursor DEALLOCATE authors_cursor GO Author: Abraham Bennet Author: Reginald Blotchet-Halls
C. 声明 SCROLL 游标并使用其它 FETCH 选项
下例创建一个 SCROLL 游标,使其通过 LAST、PRIOR、RELATIVE 和 ABSOLUTE 选项支持所有滚动能力。
USE pubs GO -- Execute the SELECT statement alone to show the -- full result set that is used by the cursor. SELECT au_lname, au_fname FROM authors ORDER BY au_lname, au_fname -- Declare the cursor. DECLARE authors_cursor SCROLL CURSOR FOR SELECT au_lname, au_fname FROM authors ORDER BY au_lname, au_fname OPEN 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_cursor CLOSE authors_cursor DEALLOCATE authors_cursor GO au_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 O'Leary 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 au_fname ---------------------------------------- -------------------- White Johnson au_lname au_fname ---------------------------------------- -------------------- Blotchet-Halls Reginald au_lname au_fname ---------------------------------------- -------------------- del Castillo Innes au_lname au_fname ---------------------------------------- -------------------- Carson Cheryl
相关文章推荐
- SQL Server游标应用实例
- 存储过程使用表变量或临时表代替游标Fetch实例,访问远程数据库
- snoopy fetchlinks 应用实例
- SQL Server游标应用实例
- 关于游标的应用实例
- mysql存储过程、游标、自定义函数实例应用
- Oracle 游标应用实例
- 在oracle利用游标取数据库的结果集应用实例:
- snoopy fetchlinks 应用实例(下载mp3)
- samba应用实例 (没完成)
- GestureDetector应用实例
- 【设计模式】---状态模式详解及应用实例
- mvc结合web应用实例分析
- Sensor应用实例
- angular实现spa单页面应用实例
- C/C++刁钻问题各个击破之位运算及其应用实例(1)
- 012_04Thread+Handler实例应用之号码归属地查询
- Java Web开发详解——XML+DTD+XML Schema+XSLT+Servlet 3.0+JSP 2.2深入剖析与实例应用
- Quartz+spring实例应用
- springMvc中websokect的应用实例