Precautions for handling exceptions in Oracle stored procedures

Source: Internet
Author: User
If a primary stored procedure and a primary function are composed of sub-procedures or sub-functions, whether a sub-function or sub-program can contain exceptions must follow the following principles: 1. If the primary and sub-functions

If a primary stored procedure and a primary function are composed of sub-procedures or sub-functions, whether a sub-function or sub-program can contain exceptions must follow the following principles: 1. If the primary and sub-functions

If the primary stored procedure and primary function are composed of sub-procedure or sub-function, can the sub-function or sub-program contain exception handling?

Follow these principles:

1. If the main program and subprograms are to be processed as a transaction, exceptions cannot be triggered in the subprogram. exceptions must be processed in the main program. If exceptions are processed in the subprogram, if an exception occurs in the subroutine, after Exception Processing, the following code of the abnormal subroutine called in the main program will continue to be executed, and a new transaction is started, this destroys transaction consistency and data consistency.

The main program is as follows:

Procedure sp_procedure

Is
Vs_pay_year_month varchar2 (6 );
Vs_ SQL varchar2 (10000 );

Begin
Sp_procedure1;

Sp_procedure2;

Update emp set empname = 'zhang san' where empid = '000000'

Exception
WHEN OTHERS THEN
-- If an exception occurs, set the disabled trigger to valid.
Vs_ SQL: = 'alter table sal_t_mainper enable all trigger ';
Execute immediate vs_ SQL;
End;

Subroutine:

Procedure sp_procedure1

Is

Begin

Update department set dept_name = 'personnel authorization' where deptid = '201312'

Delete from department where deptid = '20140901'

Exception

WHEN OTHERS THEN
-- If an exception occurs, set the disabled trigger to valid.
Raise_application_error (-20001, 'deletion error! ');

End;

Therefore, exceptions cannot be processed in subprograms, which will damage data consistency.

Second, if the main program and subroutine are not processed as a transaction, exceptions can be triggered in the subroutine, but this situation is rare.

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.