cancel
Showing results for 
Search instead for 
Did you mean: 

Cursor in nested loop gives error after migration

Former Member
3,742

I have following code in a procedure, it works fine in the Sybase ASA 6, but after migrating it to the 12.5 version, it gives error.

Here's the basic example of the code:

while(@@sqlstatus = 0) 
begin 
  declare crs_sid dynamic scroll cursor for 
  select s_th from dba.table where s_var in(1,2,3) open crs_sid with hold
  fetch next crs_sid into @sid
  while(@@sqlstatus = 0) 
  begin
    ..
    fetch next crs_sid into @sid 
  end
  close crs_sid
end

Here's the error I get:

Item crs_sid already exists.

Accepted Solutions (0)

Answers (1)

Answers (1)

VolkerBarth
Contributor

AFAIK, cursor names must be unique in current SA releases - I don't know whether that was different in very old days... (BTW, you're not using version 12.5, as that would be an ASE version, right?)

Confine this discussion (though it's based on FOR loops - but they do use cursor names, too):

Is there any purpose to the FOR for-loop-name and cursor-name fields?

There you'll find the definite answer by Ivan, as documented in DCX for the DECLARE CURSOR statement:

When a single statement is processed, all of the DECLARE CURSOR statements must use distinct names (even if the cursors are declared in scopes that do not overlap). [...]

Former Member
0 Kudos

Thanks for the answer. Sorry I meant SQL Anywhere 12.

Former Member
0 Kudos

Ok it seems that the problem was solved only by deallocating the cursor after closing it.

VolkerBarth
Contributor
0 Kudos

That seems odd: The SA 12 docs tell:

DEALLOCATE statement
This statement has no effect in SQL Anywhere, and is ignored. It is provided for compatibility with Adaptive Server Enterprise and Microsoft SQL Server. Refer to your Adaptive Server Enterprise or Microsoft SQL Server documentation for more information about this statement.