Oracle Stored Procedure exception capture

Source: Internet
Author: User

Oracle stored procedure exception capture learning, execution, and presentation process:

Stored procedures:

    CREATE OR REPLACE PROCEDUREsp_test_2 (param1inch int,--Input ParametersParam2inch int, Out_return outvarchar2 --return Results    )       is         --Global VariablesValint; Errorexception exception; --declaring exceptionsErrorCode Number;--Exception CodeErrorMsgvarchar2( +);--Exception InformationFlagvarchar2(Ten); beginflag:= 'true'; Out_return:= 'flag=' ||Flag|| ', errorcode=' ||ErrorCode|| ', errormsg=' ||errormsg; Val:=param1/param2; --/*  Exception whenErrorexception ThenErrorCode:=SQLCODE; ErrorMsg:=SUBSTR (SQLERRM,1, $); Flag:= 'false'; Out_return:= 'flag=' ||Flag|| ', errorcode=' ||ErrorCode|| ', errormsg=' ||errormsg;  whenOthers ThenErrorCode:=SQLCODE; ErrorMsg:=SUBSTR (SQLERRM,1, $); Flag:= 'false'; Out_return:= 'flag=' ||Flag|| ', errorcode=' ||ErrorCode|| ', errormsg=' ||errormsg; --dbms_output.put_line (ErrorCode | | ', ' | |   ERRORMSG);          --*/      Endsp_test_2;

To demonstrate a stored procedure:

    DECLAREOut_returnvarchar2( +); Valint;--Global VariablesErrorexception exception;--declaring exceptionsErrorCode Number;--Exception EncodingErrorMsgvarchar2( +);--Exception InformationFlagvarchar2(Ten); beginflag:= 'true'; Out_return:= 'flag=' ||Flag|| ', errorcode=' ||ErrorCode|| ', errormsg=' ||errormsg; Val:= 1/0; Exception--exception snapping, do not put the required code behind the exception catch, there is an exception to execute the exception code under all code, no exception will not be executed              whenErrorexception ThenErrorCode:=SQLCODE; ErrorMsg:=SUBSTR (SQLERRM,1, $); Flag:= 'false'; Out_return:= 'flag=' ||Flag|| ', errorcode=' ||ErrorCode|| ', errormsg=' ||errormsg;  whenOthers ThenErrorCode:=SQLCODE; ErrorMsg:=SUBSTR (SQLERRM,1, $); Flag:= 'false'; Out_return:= 'flag=' ||Flag|| ', errorcode=' ||ErrorCode|| ', errormsg=' ||errormsg;           Dbms_output.put_line (Out_return); End;

Execute stored procedure in Sqlplus:

    DECLARE       varchar2 (+);             begin            sp_test_2 (1,0, out_return);            -- Print results      End;  

Execute the Stored procedure 1 divided by 0 results:

Oracle Stored Procedure exception capture

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.