游标FETCH的应用和实例

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 必须为 smallinttinyint int

RELATIVE {n | @nvar}

如果 n@nvar 为正数,返回当前行之后的第 n 行并将返回的行变成新的当前行。如果 n@nvar 为负数,返回当前行之前的第 n 行并将返回的行变成新的当前行。如果 n@nvar 为 0,返回当前行。如果对游标的第一次提取操作时将 FETCH RELATIVE 的 n@nvar 指定为负数或 0,则没有行返回。n 必须为整型常量且 @nvar 必须为 smallinttinyint 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
 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值