Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if cursor exists (open status)

How do I check if a cursor is open or not? Because many times I am encountering the error 'Cursor already exists'. Please let me know how can I check whether a cursor is already in open status.

In fact I have closed as well as Deallocated it at the end (CLOSE ppm_cursor; DEALLOCATE ppm_cursor;) But Still i am getting the same error what could be the reason.

like image 792
Maddy Avatar asked Sep 15 '11 12:09

Maddy


People also ask

What is the cursor status?

An application uses the cursor status property to determine how a cancel operation has affected a cursor. CS_CUR_STATUS is a command structure property and cannot be retrieved at the connection or context levels. Cursor properties are useful to gateway applications that send cursor information to clients.

What does @@ Fetch_status mean?

@@FETCH_STATUS (Transact-SQL) This function returns the status of the last cursor FETCH statement issued against any cursor currently opened by the connection.

How do I find open cursors in SQL Server?

You can use the sp_cursor_list system stored procedure to get a list of cursors visible to the current connection, and sp_describe_cursor, sp_describe_cursor_columns, and sp_describe_cursor_tables to determine the characteristics of a cursor.

How we can know the status of cursor is open or not?

You can use the CURSOR_STATUS function to determine its state. For anyone else trying this with a cursor you yourself opened, and is maybe is left allocated because of error handling, you may have to change the global part to local .


1 Answers

You can use the CURSOR_STATUS function to determine its state.

IF CURSOR_STATUS('global','myCursor')>=-1 BEGIN  DEALLOCATE myCursor END 
like image 143
Gary W Avatar answered Sep 19 '22 10:09

Gary W