MySQL Forums
Forum List  »  Stored Procedures

Re: Want to Avoid Cursor
Posted by: Roland Bouman
Date: January 31, 2006 10:12AM

You could do that, providing you put the declare inside it's own BEGIN..END block. Unfortunately, you'd have to put the loop in scope, meaning you'd have to duplicate the loop as well:

main_block: begin
declare v_done bit
default false
;
declare continue handler
for not found
set v_done := true
;
case school
when 'bla' then
begin
declare csr cursor for
SELECT ...
FROM ..
etc.
;
open csr;
csr_loop: loop
fetch csr into
...
, ...
, ...
;
if v_done then
close csr;
leave csr_loop;
end if;
..
.. interesting stuff goes here
..
end loop;
end;
when 'blabla' then
begin
declare csr cursor for
SELECT ...
FROM ..
etc.
;
open csr;
csr_loop: loop
fetch csr into
...
, ...
, ...
;
if v_done then
close csr;
leave csr_loop;
end if;
..
.. interesting stuff goes here
..
end loop;
end;
else
end;
end main_block;

Alternatively (and this is what I'd probably do) you declare as many cursors as you need, but open only one conditionally:

main_block: begin
declare v_done bit
default false
;
declare continue handler
for not found
set v_done := true
;

declare cursor csr_bla for
SELECT ...
FROM bla.<tablename>
...
;
declare cursor csr_blabla for
SELECT ...
FROM bla.<tablename>
...
;
case school
when 'bla' then
open csr_bla;
csr_loop: loop
...
...etc.
...
end loop;
when 'blabla' then
open csr_blabla;
csr_loop: loop
...
...etc.
...
end loop;
end;
end main_block;

Options: ReplyQuote


Subject
Views
Written By
Posted
2751
January 30, 2006 12:41PM
1759
January 30, 2006 06:24PM
1521
January 31, 2006 08:46AM
Re: Want to Avoid Cursor
1697
January 31, 2006 10:12AM
1383
January 31, 2006 11:07AM
1440
January 31, 2006 02:28PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.