Solved

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

Posted on 2008-10-02
10
319 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 500 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now