Exit execution when error occurs PL/SQL Exit execution when error occurs PL/SQL oracle oracle

Exit execution when error occurs PL/SQL


If you create a stored procedure, you have more control and can exit whenever you like with a return statement.

So create a stored proc:

create or replace procedure myProc asbegin   dbms_ouput.put_line('i am here');   return;   dbms_ouput.put_line('and not here');end;

Then in sqlplus or developer:

exec myProc();


You can nest the blocks into a single 'program unit'.In this way an exception in the first block will stop the whole program unit from executing, rather than just being limited in scope to the first block.

set serveroutput on;BEGIN  BEGIN     insert into test values(1);    insert into test values(1);    COMMIT;      dbms_output.put_line('PRINT SOMETHING 1');   EXCEPTION     WHEN OTHERS THEN       if sqlcode <> 0       then          dbms_output.put_line(SQLCODE || '  ' || SQLERRM);          RAISE;        end if;       return;  END;  BEGIN     insert into test values(6);    COMMIT;      dbms_output.put_line('PRINT SOMETHING');   EXCEPTION     WHEN OTHERS THEN       if sqlcode <> 0       then          dbms_output.put_line(SQLCODE || '  ' || SQLERRM);          RAISE;        end if;       return;  END;END;/ 


You should be able to use "exit" - see the Oracle documentation here: http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12023.htm

Note that this will end your SqlPlus session, but I don't know of another way of doing it aside from using a single block or stored procedure.

Another useful statement is:

WHENEVER SQLERROR EXIT SQL.SQLCODE

Oracle documentation: http://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12052.htm