Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-10-02
10
Medium Priority
?
337 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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