Exceptions in database (pre-defined, non-predefined, user-defined)

Source: Internet
Author: User
Tags define exception

DECLARE --  Predefined exception information does not declare an exception V_empno emp.empno%TYPE:=&empno; V_sal Emp.sal%TYPE; BEGIN SELECT sal into v_sal from emp WHERE empno=V_empno; IF V_sal<=1500Then UPDATE emp SET sal=sal+100 WHERE empno=V_empno; Dbms_output. Put_Line (' Encode as ' | | v_empno| | ' Employee pay has been updated! '); ELSE Dbms_output. Put_Line (' Encode as ' | | v_empno| | ' Employee pay has exceeded the specified value! ');  END IF; END; -------------------------------------------------------------------------------------------------------- user-defined exception DECLARE v_empno emp.empno%TYPE:=&empno; --Declaring variable v_empno, type emp.empno%The type is  consistent with the EMP table empno column type no_result EXCEPTION; -- declares an exception named No_result BEGIN UPDATE emp SET sal=sal+100 WHERE empno=V_empno; IF SQL%NOTFOUND then RAISE no_result;--Trigger exception                      -- raise trigger END IF; EXCEPTION when No_result then-- When an exception is triggered dbms_output. Put_Line (' Your data UPDATE statement failed! '); When OTHERS Then-- can be understood as exception packets, which receive exception dbms_output beyond the definition exception . Put_Line (SQLCODE| | ' ---' | |SQLERRM); END; --------------------------------------------------------------------------------------------------------- non-pre-defined exception --Define exception conditions in the Declarations section of the PL/SQL block: --< anomalies > EXCEPTION; --to associate its defined anomalies with standard Oracle errors, using the Exception_init statement--pragma Exception_init (< exception, < error code >);--In pl/The  exception handling section of the SQL block handles the exception situation accordingly DECLARE V_deptno Dept.deptno%TYPE:=&Deptno; Deptno_remaining EXCEPTION;--Declaring exceptions---PRAGMA exception_init (deptno_remaining,-2292);/*-2292 is an error code that violates the consistency constraint*/                          --Abnormal situation       --oracle Error code/*deptno_remaining exception is triggered if a 2292 error occurs in Oracle statement execution*/    ---BEGIN DELETE from dept WHERE Deptno=V_deptno; EXCEPTION when Deptno_remaining and then Dbms_output. Put_Line (' Violate data integrity constraints! '); When OTHERS and then Dbms_output. Put_Line (SQLCODE| | ' ---' | |SQLERRM); END; -------------------------------------------------------------------------------------------------------

Exceptions in database (pre-defined, non-predefined, user-defined)

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.