報(bào)告當(dāng)前為連接打開(kāi)的服務(wù)器游標(biāo)的特性。
sp_cursor_list [ @cursor_return = ] cursor_variable_name OUTPUT
, [ @cursor_scope = ] cursor_scope
[@cursor_return =] cursor_variable_name OUTPUT
聲明的游標(biāo)變量的名稱。cursor_variable_name 的數(shù)據(jù)類型為 cursor,沒(méi)有默認(rèn)值。游標(biāo)是可滾動(dòng)的、動(dòng)態(tài)的只讀游標(biāo)。
[@cursor_scope =] cursor_scope
指定要報(bào)告的游標(biāo)級(jí)別。cursor_scope 的數(shù)據(jù)類型為 int,沒(méi)有默認(rèn)值,可以是下列值中的一個(gè)。
值 | 描述 |
---|---|
1 | 報(bào)告所有本地游標(biāo)。 |
2 | 報(bào)告所有全局游標(biāo)。 |
3 | 報(bào)告本地游標(biāo)和全局游標(biāo)。 |
無(wú)
sp_cursor_list 返回的報(bào)告是 Transact-SQL 游標(biāo)輸出參數(shù),而不是結(jié)果集。這樣,Transact-SQL 批處理、存儲(chǔ)過(guò)程和觸發(fā)器就得以按一次一行的方式處理輸出。這還意味著無(wú)法直接從數(shù)據(jù)庫(kù) API 函數(shù)調(diào)用該過(guò)程。游標(biāo)輸出參數(shù)必須綁定到程序變量,但是數(shù)據(jù)庫(kù) API 不支持綁定游標(biāo)參數(shù)或變量。
以下是 sp_cursor_list 返回的游標(biāo)格式。游標(biāo)格式與 sp_describe_cursor 返回的格式相同。
列名 | 數(shù)據(jù)類型 | 描述 |
---|---|---|
reference_name | sysname | 用來(lái)引用游標(biāo)的名稱。如果通過(guò) DECLARE CURSOR 語(yǔ)句中給定的名稱引用游標(biāo),則引用名稱與游標(biāo)名稱相同。如果通過(guò)變量引用游標(biāo),則引用名稱即為游標(biāo)變量的名稱。 |
cursor_name | sysname | 來(lái)自 DECLARE CURSOR 語(yǔ)句的游標(biāo)名稱。如果游標(biāo)是通過(guò)將游標(biāo)變量設(shè)置為游標(biāo)而創(chuàng)建的,則游標(biāo)名稱為系統(tǒng)生成的名稱。 |
cursor_scope | smallint | 1 = LOCAL 2 = GLOBAL |
status | smallint | 與 CURSOR_STATUS 系統(tǒng)函數(shù)報(bào)告的值相同: 1 = 游標(biāo)名稱或變量引用的游標(biāo)打開(kāi)。如果游標(biāo)為不感知游標(biāo)、靜態(tài)游標(biāo)或鍵集游標(biāo),則至少包含一行。如果游標(biāo)是動(dòng)態(tài)游標(biāo),則結(jié)果集包含零行或更多的行。 |
model | smallint | 1 = 不感知(或靜態(tài)) 2 = 鍵集 3 = 動(dòng)態(tài) 4 = 快進(jìn) |
concurrency | smallint | 1 = 只讀 2 = 滾動(dòng)鎖 3 = 樂(lè)觀 |
scrollable | smallint | 0 = 只進(jìn) 1 = 可滾動(dòng) |
open_status | smallint | 0 = 關(guān)閉 1 = 打開(kāi) |
cursor_rows | int | 結(jié)果集中合格的行數(shù)。有關(guān)更多信息,請(qǐng)參見(jiàn) @@CURSOR_ROWS。 |
fetch_status | smallint | 此游標(biāo)上次提取的狀態(tài)。有關(guān)更多信息,請(qǐng)參見(jiàn) @@FETCH_STATUS。 0 = 提取成功。 |
column_count | smallint | 游標(biāo)結(jié)果集中的列數(shù)。 |
row_count | smallint | 上次對(duì)游標(biāo)的操作所影響的行數(shù)。有關(guān)更多信息,請(qǐng)參見(jiàn) @@ROWCOUNT。 |
last_operation | smallint | 上次對(duì)游標(biāo)執(zhí)行的操作: 0 = 沒(méi)有對(duì)游標(biāo)執(zhí)行操作。 |
cursor_handle | int | 在服務(wù)器范圍內(nèi)標(biāo)識(shí)游標(biāo)的唯一值。 |
sp_cursor_list 生成連接打開(kāi)的當(dāng)前服務(wù)器游標(biāo)列表,并描述每個(gè)游標(biāo)的全局特性,例如游標(biāo)的可滾動(dòng)性和可更新性。sp_cursor_list 列出的游標(biāo)包括:
使用 sp_describe_cursor_columns 描述由游標(biāo)返回的結(jié)果集的特性。使用 sp_describe_cursor_tables 報(bào)告游標(biāo)引用的基表。sp_describe_cursor 與 sp_cursor_list 報(bào)告的信息相同,但前者只適用于指定的游標(biāo)。
執(zhí)行權(quán)限默認(rèn)授予 public 角色。
下面的示例打開(kāi)一個(gè)全局游標(biāo),并使用 sp_cursor_list 報(bào)告游標(biāo)的特性。
USE Northwind
GO
-- Declare and open a keyset-driven cursor.
DECLARE abc CURSOR KEYSET FOR
SELECT LastName
FROM Employees
WHERE LastName LIKE 'S%'
OPEN abc
-- Declare a cursor variable to hold the cursor output variable
-- from sp_cursor_list.
DECLARE @Report CURSOR
-- Execute sp_cursor_list into the cursor variable.
EXEC master.dbo.sp_cursor_list @cursor_return = @Report OUTPUT,
@cursor_scope = 2
-- Fetch all the rows from the sp_cursor_list output cursor.
FETCH NEXT from @Report
WHILE (@@FETCH_STATUS <> -1)
BEGIN
FETCH NEXT from @Report
END
-- Close and deallocate the cursor from sp_cursor_list.
CLOSE @Report
DEALLOCATE @Report
GO
-- Close and deallocate the original cursor.
CLOSE abc
DEALLOCATE abc
GO
相關(guān)文章