Solved

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

Posted on 2008-10-02
10
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Server 2012 r2 and SQL 2014 6 34
SQL Job Failed 6 31
Datatable / Dates ? 4 32
Section based report in SSRS 14 35
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

751 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