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
Solved

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

Posted on 2008-10-02
10
328 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

808 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