報告當前為連接打開的服務器游標的特性。
sp_cursor_list [ @cursor_return = ] cursor_variable_name OUTPUT
, [ @cursor_scope = ] cursor_scope
[@cursor_return =] cursor_variable_name OUTPUT
聲明的游標變量的名稱。cursor_variable_name 的數(shù)據(jù)類型為 cursor,沒有默認值。游標是可滾動的、動態(tài)的只讀游標。
[@cursor_scope =] cursor_scope
指定要報告的游標級別。cursor_scope 的數(shù)據(jù)類型為 int,沒有默認值,可以是下列值中的一個。
| 值 | 描述 |
|---|---|
| 1 | 報告所有本地游標。 |
| 2 | 報告所有全局游標。 |
| 3 | 報告本地游標和全局游標。 |
無
sp_cursor_list 返回的報告是 Transact-SQL 游標輸出參數(shù),而不是結(jié)果集。這樣,Transact-SQL 批處理、存儲過程和觸發(fā)器就得以按一次一行的方式處理輸出。這還意味著無法直接從數(shù)據(jù)庫 API 函數(shù)調(diào)用該過程。游標輸出參數(shù)必須綁定到程序變量,但是數(shù)據(jù)庫 API 不支持綁定游標參數(shù)或變量。
以下是 sp_cursor_list 返回的游標格式。游標格式與 sp_describe_cursor 返回的格式相同。
| 列名 | 數(shù)據(jù)類型 | 描述 |
|---|---|---|
| reference_name | sysname | 用來引用游標的名稱。如果通過 DECLARE CURSOR 語句中給定的名稱引用游標,則引用名稱與游標名稱相同。如果通過變量引用游標,則引用名稱即為游標變量的名稱。 |
| cursor_name | sysname | 來自 DECLARE CURSOR 語句的游標名稱。如果游標是通過將游標變量設置為游標而創(chuàng)建的,則游標名稱為系統(tǒng)生成的名稱。 |
| cursor_scope | smallint | 1 = LOCAL 2 = GLOBAL |
| status | smallint | 與 CURSOR_STATUS 系統(tǒng)函數(shù)報告的值相同: 1 = 游標名稱或變量引用的游標打開。如果游標為不感知游標、靜態(tài)游標或鍵集游標,則至少包含一行。如果游標是動態(tài)游標,則結(jié)果集包含零行或更多的行。 |
| model | smallint | 1 = 不感知(或靜態(tài)) 2 = 鍵集 3 = 動態(tài) 4 = 快進 |
| concurrency | smallint | 1 = 只讀 2 = 滾動鎖 3 = 樂觀 |
| scrollable | smallint | 0 = 只進 1 = 可滾動 |
| open_status | smallint | 0 = 關(guān)閉 1 = 打開 |
| cursor_rows | int | 結(jié)果集中合格的行數(shù)。有關(guān)更多信息,請參見 @@CURSOR_ROWS。 |
| fetch_status | smallint | 此游標上次提取的狀態(tài)。有關(guān)更多信息,請參見 @@FETCH_STATUS。 0 = 提取成功。 |
| column_count | smallint | 游標結(jié)果集中的列數(shù)。 |
| row_count | smallint | 上次對游標的操作所影響的行數(shù)。有關(guān)更多信息,請參見 @@ROWCOUNT。 |
| last_operation | smallint | 上次對游標執(zhí)行的操作: 0 = 沒有對游標執(zhí)行操作。 |
| cursor_handle | int | 在服務器范圍內(nèi)標識游標的唯一值。 |
sp_cursor_list 生成連接打開的當前服務器游標列表,并描述每個游標的全局特性,例如游標的可滾動性和可更新性。sp_cursor_list 列出的游標包括:
使用 sp_describe_cursor_columns 描述由游標返回的結(jié)果集的特性。使用 sp_describe_cursor_tables 報告游標引用的基表。sp_describe_cursor 與 sp_cursor_list 報告的信息相同,但前者只適用于指定的游標。
執(zhí)行權(quán)限默認授予 public 角色。
下面的示例打開一個全局游標,并使用 sp_cursor_list 報告游標的特性。
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)文章