SQL sp_cursor_details

sp_cursor_details

CREATE PROC sp_cursor_details
as

-- Build a temp table to hold the results from sp_cursor_list
SET NOCOUNT ON

create table #cursors
(  reference_name char(30), 
   cursor_name char(30), 
   cursor_scope tinyint,
   status int,  
   model tinyint, 
   concurrency tinyint, 
   scrollable tinyint, 
   open_status tinyint, 
   cursor_rows decimal(10,0), 
   fetch_status smallint,
   column_count smallint, 
   row_count decimal(10,0),    
   last_operation tinyint, 
   cursor_handle  int
)

-- Declare a cursor variable to hold the cursor output variable
-- from sp_cursor_list.
DECLARE @Report CURSOR

-- Declare variables to hold the fetched values from @Report
DECLARE
   @reference_name char(30), 
   @cursor_name char(30),
   @cursor_scope tinyint,
   @status int,  
   @model tinyint, 
   @concurrency tinyint, 
   @scrollable tinyint, 
   @open_status tinyint, 
   @cursor_rows decimal(10,0), 
   @fetch_status smallint,
   @column_count smallint, 
   @row_count decimal(10,0),    
   @last_operation tinyint, 
   @cursor_handle  int


-- Execute sp_cursor_list into the cursor variable.

EXEC master.dbo.sp_cursor_list @cursor_return = @Report OUTPUT,
	@cursor_scope = 3
  
-- Fetch the first row from the sp_cursor_list output cursor.
FETCH NEXT from @Report INTO
   @reference_name, 
   @cursor_name,
   @cursor_scope,
   @status,  
   @model, 
   @concurrency , 
   @scrollable , 
   @open_status, 
   @cursor_rows, 
   @fetch_status,
   @column_count, 
   @row_count,    
   @last_operation, 
   @cursor_handle

WHILE (@@FETCH_STATUS = 0)
BEGIN
-- Insert the values into the temp table
   INSERT INTO #cursors
     VALUES ( 
                 @reference_name, 
                 @cursor_name,
                 @cursor_scope,
                 @status,  
                 @model, 
                 @concurrency , 
                 @scrollable , 
                 @open_status, 
                 @cursor_rows, 
                 @fetch_status,
                 @column_count, 
                 @row_count,    
                 @last_operation, 
                 @cursor_handle
             )

-- Fetch subsequent rows from @Report
      FETCH NEXT from @Report  INTO
         @reference_name, 
         @cursor_name,
         @cursor_scope,
         @status,  
         @model, 
         @concurrency , 
         @scrollable , 
         @open_status, 
         @cursor_rows, 
         @fetch_status,
         @column_count, 
         @row_count,    
         @last_operation, 
         @cursor_handle
END
  
-- Close and deallocate the cursor from sp_cursor_list.
CLOSE @Report
DEALLOCATE @Report

-- Return the information from the temp table, translated into readable descriptions
IF (SELECT count(*) FROM #cursors) > 0
  SELECT 
  'Cursor Reference' = reference_name, 
  'Declared Name' = cursor_name, 

   'Scope   ' = CASE cursor_scope
     WHEN 1 THEN 'local'
     ELSE 'global'
   END,

   Status = CASE status
     WHEN 1  THEN 'open'
     WHEN 0  THEN 'no rows'
     WHEN -1 THEN 'closed'
     WHEN -2 THEN 'no reference'
     WHEN -3 THEN 'does not exist'
   END,
 
   Model =  CASE model
     WHEN 1 THEN 'static'
     WHEN 2 THEN 'keyset'
     WHEN 3 THEN 'dynamic'
     WHEN 4 THEN 'fast forward'
   END,

   LOCKING = CASE concurrency 
     WHEN 1 THEN 'read only'
     WHEN 2 THEN 'scroll locks'
     WHEN 3 THEN 'optimistic'
   END,

   Scrolling = CASE scrollable 
     WHEN 0 THEN 'forward only'
     WHEN 1 THEN 'scrollable'
   END,
 
   'Open' = CASE open_status 
     WHEN 0 THEN 'no'
     WHEN 1 THEN 'yes'
   END,

   'Qualifying Rows' =  cursor_rows,
 
   'Fetch Status' = CASE fetch_status 
     WHEN 0  THEN 'successful'
     WHEN -1 THEN 'out of bounds'
     WHEN -2 THEN 'missing row'
     WHEN -9 THEN 'no fetch done'
   END,

   'Columns' = column_count, 

   'Rows for last operation' = row_count,  

   'Last operation' = CASE last_operation 
     WHEN 0 THEN 'none'
     WHEN 1 THEN 'OPEN'
     WHEN 2 THEN 'FETCH'
     WHEN 3 THEN 'INSERT'
     WHEN 4 THEN 'UPDATE'
     WHEN 5 THEN 'DELETE'
     WHEN 6 THEN 'CLOSE'
     WHEN 7 THEN 'DEALLOCATE'
   END,
 
   Handle = cursor_handle

  FROM #cursors

RETURN