FETCH FETCH - 北京怡康軟件科技有限公司 資源網(wǎng) "/>
從 Transact-SQL 服務(wù)器游標(biāo)中檢索特定的一行。
FETCH
[ [ NEXT | PRIOR | FIRST | LAST
| ABSOLUTE { n | @nvar }
| RELATIVE { n | @nvar }
]
FROM
]
{ { [ GLOBAL ] cursor_name } | @cursor_variable_name }
[ INTO @variable_name [ ,...n ] ]
NEXT
返回緊跟當(dāng)前行之后的結(jié)果行,并且當(dāng)前行遞增為結(jié)果行。如果 FETCH NEXT 為對(duì)游標(biāo)的第一次提取操作,則返回結(jié)果集中的第一行。NEXT 為默認(rèn)的游標(biāo)提取選項(xiàng)。
PRIOR
返回緊臨當(dāng)前行前面的結(jié)果行,并且當(dāng)前行遞減為結(jié)果行。如果 FETCH PRIOR 為對(duì)游標(biāo)的第一次提取操作,則沒(méi)有行返回并且游標(biāo)置于第一行之前。
FIRST
返回游標(biāo)中的第一行并將其作為當(dāng)前行。
LAST
返回游標(biāo)中的最后一行并將其作為當(dāng)前行。
ABSOLUTE {n | @nvar}
如果 n 或 @nvar 為正數(shù),返回從游標(biāo)頭開始的第 n 行并將返回的行變成新的當(dāng)前行。如果 n 或 @nvar 為負(fù)數(shù),返回游標(biāo)尾之前的第 n 行并將返回的行變成新的當(dāng)前行。如果 n 或 @nvar 為 0,則沒(méi)有行返回。n 必須為整型常量且 @nvar 必須為 smallint、tinyint 或 int。
RELATIVE {n | @nvar}
如果 n 或 @nvar 為正數(shù),返回當(dāng)前行之后的第 n 行并將返回的行變成新的當(dāng)前行。如果 n 或 @nvar 為負(fù)數(shù),返回當(dāng)前行之前的第 n 行并將返回的行變成新的當(dāng)前行。如果 n 或 @nvar 為 0,返回當(dāng)前行。如果對(duì)游標(biāo)的第一次提取操作時(shí)將 FETCH RELATIVE 的 n 或 @nvar 指定為負(fù)數(shù)或 0,則沒(méi)有行返回。n 必須為整型常量且 @nvar 必須為 smallint、tinyint 或 int。
GLOBAL
指定 cursor_name 指的是全局游標(biāo)。
cursor_name
要從中進(jìn)行提取的開放游標(biāo)的名稱。如果同時(shí)有以 cursor_name 作為名稱的全局和局部游標(biāo)存在,若指定為 GLOBAL 則 cursor_name 對(duì)應(yīng)于全局游標(biāo),未指定 GLOBAL 則對(duì)應(yīng)于局部游標(biāo)。
@cursor_variable_name
游標(biāo)變量名,引用要進(jìn)行提取操作的打開的游標(biāo)。
INTO @variable_name[,...n]
允許將提取操作的列數(shù)據(jù)放到局部變量中。列表中的各個(gè)變量從左到右與游標(biāo)結(jié)果集中的相應(yīng)列相關(guān)聯(lián)。各變量的數(shù)據(jù)類型必須與相應(yīng)的結(jié)果列的數(shù)據(jù)類型匹配或是結(jié)果列數(shù)據(jù)類型所支持的隱性轉(zhuǎn)換。變量的數(shù)目必須與游標(biāo)選擇列表中的列的數(shù)目一致。
如果 SCROLL 選項(xiàng)未在 SQL-92 樣式的 DECLARE CURSOR 語(yǔ)句中指定,則 NEXT 是唯一受支持的 FETCH 選項(xiàng)。如果在 SQL-92 樣式的 DECLARE CURSOR 語(yǔ)句中指定了 SCROLL 選項(xiàng),則支持所有的 FETCH 選項(xiàng)。
如果使用 Transact_SQL DECLARE 游標(biāo)擴(kuò)展,以下規(guī)則適用:
@@FETCH_STATUS 函數(shù)報(bào)告上一個(gè) FETCH 語(yǔ)句的狀態(tài)。相同的信息記錄于由 sp_describe_cursor 返回的游標(biāo)中的 fetch_status 列中。這些狀態(tài)信息應(yīng)該用于在對(duì)由 FETCH 語(yǔ)句返回的數(shù)據(jù)進(jìn)行任何操作之前,以確定這些數(shù)據(jù)的有效性。有關(guān)更多信息,請(qǐng)參見 @@FETCH_STATUS。
FETCH 的默認(rèn)權(quán)限為任何合法用戶。
下例為 authors 表中姓以字母 B 開頭的行聲明了一個(gè)簡(jiǎn)單的游標(biāo),并使用 FETCH NEXT 逐個(gè)提取這些行。FETCH 語(yǔ)句以單行結(jié)果集形式返回由 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 語(yǔ)句的輸出存儲(chǔ)于局部變量而不是直接返回給客戶端。PRINT 語(yǔ)句將變量組合成單一字符串并將其返回到客戶端。
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)建一個(gè) SCROLL 游標(biāo),使其通過(guò) LAST、PRIOR、RELATIVE 和 ABSOLUTE 選項(xiàng)支持所有滾動(dòng)能力。
USE pubs相關(guān)文章
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