Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How do you close a cursor in SQL 2000 in production?

Posted on 2008-10-02
10
Medium Priority
?
339 Views
Last Modified: 2012-05-05
We have a VB6/SQL Server 2000 app running. We got the "A cursor with the name of ... already exists' error. After that, we've been getting an 'Unspecified Error' error message from the same process. Assuming that the new errors come from the open cursor, how do we close down the cursor on our production machine?

As for the code, I'm fixing the underlying problem, it did not close and deallocate the cursor when errors might occur.
0
Comment
Question by:CRESLISA
  • 4
  • 4
  • 2
10 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22626426
close cursorname
deallocate cursorname
0
 
LVL 1

Author Comment

by:CRESLISA
ID: 22626487
Sounds simple. But in order to know if this is going to work, how do I test to see if the cursor is open? DO i just log onto the production machine and run those commands in SQL Query Analyzer?
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22626604
The problem most likely stems from the same cursor being declared at different stages of the procedure stack.

So if procedure up_Proc1 declares, opens, closes and deallocates a cursor called dataCursor, but if somewhere inside of up_proc2, the cursor name dataCursor is ALSO declared, opened, closed and deallocated, you will get this error.


create procedure up_proc1
as
declare datacursor cursor for
....
open  datacursor
fetch next.....
while @@fetch_Status=0
begin

exec up_proc2 @someVariable

fetch next...
end
close  datacursor
deallocate  datacursor
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 60

Expert Comment

by:chapmandew
ID: 22626638
you can use the CURSOR_STATUS function
0
 
LVL 1

Author Comment

by:CRESLISA
ID: 22626945
It did not work.

I entered this in Query Analyzer:
cursor_status curoption

and got this message:
Server: Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'cursor_status'.
0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22626957
I think it has to be on the same connection...
0
 
LVL 1

Author Comment

by:CRESLISA
ID: 22627016
I just looked up this function in Books Online. It looks like you have to use it within the procedure that opens the cursor. That's not my situation, I need to know if I can determine the status of cursor that was opened by a procedure in a production environment, and then, if the cursor is open, close and deallocate the cursor.

I did identify and fix the cause of my particular problem. There was an exception within the cursor loop and the 'error trap' set uip in the code did not close and deallocate the cursor. Hence, it was left open and subsequent calls resulted in the error.

However, I'd still like to know if there is a way to monitor a cursor and shut it down if anything like this happens again.
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 1500 total points
ID: 22627035
you might be able to use SET CURSOR_CLOSE_ON_COMMIT to do what you want...but overall, you just have to be diligent when writing the code to take care of these scenarios
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22627065
That needs to be handled in the procedure code.  A cursor should never be declared and not deallocated.
0
 
LVL 1

Author Closing Comment

by:CRESLISA
ID: 31502470
Looks like there may not be a way to do what I want. Since this was informative, I'll award points and close this out.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question