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 必須為 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 游標擴展,以下規(guī)則適用:
@@FETCH_STATUS 函數報告上一個 FETCH 語句的狀態(tài)。相同的信息記錄于由 sp_describe_cursor 返回的游標中的 fetch_status 列中。這些狀態(tài)信息應該用于在對由 FETCH 語句返回的數據進行任何操作之前,以確定這些數據的有效性。有關更多信息,請參見 @@FETCH_STATUS。
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
----------------------------------------
下例與上例相似,但 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
下例創(chuàng)建一個 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