I have written a PL/SQL code to to print only records greater than id=4. I used a goto statement in the program, which is not detected in exception. Please help me continue the program after encountering the exception. My code is
declare
cursor cur is select * from student;
c student%rowtype;
lt3 exception;
PRAGMA
exception_init(lt3,-77);
begin
open cur;
loop
<<backup>>
fetch cur into c;
if c.id < 4 then
raise lt3;
else
dbms_output.put_line(c.name);
end if;
end loop;
close cur;
exception
when lt3 then
dbms_output.put_line('Exception encountered');
goto backup;
end;
/
Where should i change?
I got the error at
ERROR at line 24:
ORA-06550: line 24, column 7:
PLS-00201: identifier 'BACKUP' must be declared
ORA-06550: line 24, column 2:
PL/SQL: Statement ignored
when you use goto
in cursor
, cursor will be closed, hence you cannot achieve the expected behaviour.
From Doc
If you use the GOTO statement to exit a cursor FOR loop prematurely, the cursor is closed automatically. The cursor is also closed automatically if an exception is raised inside the loop.
One thing you can do is to use continue
, break
, exit
in the loop to control the execution
open cur;
loop
fetch cur into c;
if c.id < 4 then
continue;
else
dbms_output.put_line(c.name);
end if;
end loop;
close cur;