tags:

views:

31

answers:

4

I have a situation where one sql server stored procedure is calling another one and both happen to use cursors that have the same name. This is causing a conflict as apparently the cursors don't have visibility restricted to the stored procedure where they are declared.

Is there any way to make the cursors private to the procedure they were declared in?

+2  A: 

Apparently there is a local keyword.

http://www.codeguru.com/cpp/data/mfc_database/sqlserver/article.php/c7177

chuck taylor
+2  A: 

Use the LOCAL option when declaring the cursor.

LOCAL

Specifies that the scope of the cursor is local to the batch, stored procedure, or trigger in which the cursor was created. The cursor name is only valid within this scope. The cursor can be referenced by local cursor variables in the batch, stored procedure, or trigger, or a stored procedure OUTPUT parameter. An OUTPUT parameter is used to pass the local cursor back to the calling batch, stored procedure, or trigger, which can assign the parameter to a cursor variable to reference the cursor after the stored procedure terminates. The cursor is implicitly deallocated when the batch, stored procedure, or trigger terminates, unless the cursor was passed back in an OUTPUT parameter. If it is passed back in an OUTPUT parameter, the cursor is deallocated when the last variable referencing it is deallocated or goes out of scope.

Joe Stefanelli
+1  A: 

Are the cursors local cursors or global cursors?...Make them local or just change the name of one of the cursors and be done with it

SQLMenace
+2  A: 

Yes you can restrict the scope of a cursor to the stored procedure with

DECLARE CURSOR LOCAL

James Gaunt