Oracle PL/SQL/PL SQL/Your own Exception

Материал из SQL эксперт
Версия от 09:59, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Define and use your own exception

SQL>
SQL> -- define and use your own exception
SQL>
SQL>
SQL>  declare
  2      CHILD_ERROR exception;
  3    begin
  4      raise CHILD_ERROR;
  5    end;
  6    /
 declare
*
ERROR at line 1:
ORA-06510: PL/SQL: unhandled user-defined exception
ORA-06512: at line 4

SQL>



Handling a user-defined exception

 
SQL>
SQL> CREATE TABLE session (
  2    department       CHAR(3),
  3    course           NUMBER(3),
  4    description      VARCHAR2(2000),
  5    max_lecturer     NUMBER(3),
  6    current_lecturer NUMBER(3),
  7    num_credits      NUMBER(1),
  8    room_id          NUMBER(5)
  9    );
Table created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("HIS", 101, "History 101", 30, 11, 4, 20000);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("HIS", 301, "History 301", 30, 0, 4, 20004);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("CS", 101, "Computer Science 101", 50, 0, 4, 20001);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("ECN", 203, "Economics 203", 15, 0, 3, 20002);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("CS", 102, "Computer Science 102", 35, 3, 4, 20003);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("MUS", 410, "Music 410", 5, 4, 3, 20005);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("ECN", 101, "Economics 101", 50, 0, 4, 20007);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("NUT", 307, "Nutrition 307", 20, 2, 4, 20008);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("MUS", 100, "Music 100", 100, 0, 3, NULL);
1 row created.
SQL>
SQL>
SQL> CREATE TABLE myLogTable (
  2    code             NUMBER,
  3    message          VARCHAR2(200),
  4    info             VARCHAR2(100)
  5    );
Table created.
SQL>
SQL>
SQL> DECLARE
  2    myException EXCEPTION;
  3    studentCount NUMBER(3);
  4    studentMax NUMBER(3);
  5  BEGIN
  6    SELECT current_lecturer, max_lecturer
  7      INTO studentCount, studentMax
  8      FROM session
  9      WHERE department = "HIS" AND course = 101;
 10    IF studentCount > studentMax THEN
 11      RAISE myException;
 12    END IF;
 13  EXCEPTION
 14    WHEN myException THEN
 15      INSERT INTO myLogTable (info) VALUES ("History 101 has " || studentCount ||
 16        "lecturer: max allowed is " || studentMax);
 17  END;
 18  /
PL/SQL procedure successfully completed.
SQL>
SQL> drop table myLogTable;
Table dropped.
SQL>
SQL> drop table session;
Table dropped.



How to define your own exception

SQL>
SQL>
SQL> -- User defined exception
SQL>
SQL>  begin
  2      declare --start of nested block
  3        NESTED_EXCEPTION exception;
  4      begin
  5        raise NESTED_EXCEPTION;
  6      end; --end of nested block
  7    exception
  8      when NESTED_EXCEPTION then
  9        dbms_output.put_line("NESTED_EXCEPTION caught!");
 10    end;
 11    /
    when NESTED_EXCEPTION then
         *
ERROR at line 8:
ORA-06550: line 8, column 10:
PLS-00201: identifier "NESTED_EXCEPTION" must be declared
ORA-06550: line 0, column 0:
PL/SQL: Compilation unit analysis terminated

SQL>
SQL>



Insert Exception message to an audit table

SQL> -- log exception to log
SQL>
SQL>
SQL> create table Employee(
  2    ID                 VARCHAR2(4 BYTE)         NOT NULL,
  3    First_Name         VARCHAR2(10 BYTE),
  4    Last_Name          VARCHAR2(10 BYTE),
  5    Start_Date         DATE,
  6    End_Date           DATE,
  7    Salary             Number(8,2),
  8    City               VARCHAR2(10 BYTE),
  9    Description        VARCHAR2(15 BYTE)
 10  )
 11  /
Table created.
SQL>
SQL> -- prepare data
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2               values ("01","Jason",    "Martin",  to_date("19960725","YYYYMMDD"), to_date("20060725","YYYYMMDD"), 1234.56, "Toronto",  "Programmer")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("02","Alison",   "Mathews", to_date("19760321","YYYYMMDD"), to_date("19860221","YYYYMMDD"), 6661.78, "Vancouver","Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("03","James",    "Smith",   to_date("19781212","YYYYMMDD"), to_date("19900315","YYYYMMDD"), 6544.78, "Vancouver","Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("04","Celia",    "Rice",    to_date("19821024","YYYYMMDD"), to_date("19990421","YYYYMMDD"), 2344.78, "Vancouver","Manager")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary,  City,       Description)
  2                values("05","Robert",   "Black",   to_date("19840115","YYYYMMDD"), to_date("19980808","YYYYMMDD"), 2334.78, "Vancouver","Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values("06","Linda",    "Green",   to_date("19870730","YYYYMMDD"), to_date("19960104","YYYYMMDD"), 4322.78,"New York",  "Tester")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values("07","David",    "Larry",   to_date("19901231","YYYYMMDD"), to_date("19980212","YYYYMMDD"), 7897.78,"New York",  "Manager")
  3  /
1 row created.
SQL> insert into Employee(ID,  First_Name, Last_Name, Start_Date,                     End_Date,                       Salary, City,        Description)
  2                values("08","James",    "Cat",     to_date("19960917","YYYYMMDD"), to_date("20020415","YYYYMMDD"), 1232.78,"Vancouver", "Tester")
  3  /
1 row created.
SQL>
SQL>
SQL>
SQL> -- display data in the table
SQL> select * from Employee
  2  /
ID   FIRST_NAME LAST_NAME  START_DAT END_DATE      SALARY CITY       DESCRIPTION
---- ---------- ---------- --------- --------- ---------- ---------- ---------------
01   Jason      Martin     25-JUL-96 25-JUL-06    1234.56 Toronto    Programmer
02   Alison     Mathews    21-MAR-76 21-FEB-86    6661.78 Vancouver  Tester
03   James      Smith      12-DEC-78 15-MAR-90    6544.78 Vancouver  Tester
04   Celia      Rice       24-OCT-82 21-APR-99    2344.78 Vancouver  Manager
05   Robert     Black      15-JAN-84 08-AUG-98    2334.78 Vancouver  Tester
06   Linda      Green      30-JUL-87 04-JAN-96    4322.78 New York   Tester
07   David      Larry      31-DEC-90 12-FEB-98    7897.78 New York   Manager
08   James      Cat        17-SEP-96 15-APR-02    1232.78 Vancouver  Tester
8 rows selected.
SQL>
SQL>     CREATE OR REPLACE PROCEDURE add_new_employee
  2          (employee_id_in IN NUMBER, salary_in IN NUMBER)
  3      IS
  4          no_salary EXCEPTION;
  5
  6      BEGIN
  7          IF salary_in = 0 THEN
  8              RAISE no_salary;
  9          ELSE
 10              INSERT INTO employee (id, salary ) VALUES ( employee_id_in, salary_in );
 11      END IF;
 12
 13      EXCEPTION
 14           WHEN no_salary THEN
 15             raise_application_error (-20001,"You must have salary in order to insert the employee.");
 16
 17  
 18    
 19
 20      END;
 21      /
Procedure created.
SQL>
SQL> exec add_new_employee("99",0);
BEGIN add_new_employee("99",0); END;
*
ERROR at line 1:
ORA-20001: You must have salary in order to insert the employee.
ORA-06512: at "sqle.ADD_NEW_EMPLOYEE", line 15
ORA-06512: at line 1

SQL>
SQL> -- clean the table
SQL> drop table Employee
  2  /
Table dropped.
SQL>



Raise User-defined exceptions

SQL>
SQL>
SQL>
SQL> DECLARE
  2    myException EXCEPTION;
  3  BEGIN
  4
  5      RAISE myException;
  6
  7      DBMS_OUTPUT.PUT_LINE("This enrollment has been deleted");
  8  EXCEPTION
  9
 10   WHEN myException THEN
 11    DBMS_OUTPUT.PUT_LINE("This enrollment is already confirmed");
 12    DBMS_OUTPUT.PUT_LINE("It cannot be deleted");
 13  END;
 14
 15  /
This enrollment is already confirmed
It cannot be deleted
PL/SQL procedure successfully completed.
SQL>



used defined exception

 
SQL>
SQL>  declare
  2        NESTED_EXCEPTION exception;
  3   begin
  4
  5        raise NESTED_EXCEPTION;
  6
  7    exception
  8
  9      when NESTED_EXCEPTION then
 10        dbms_output.put_line("NESTED_EXCEPTION caught!");
 11    end;
 12    /
NESTED_EXCEPTION caught!
PL/SQL procedure successfully completed.
SQL>
SQL>



Using SQLCODE and SQLERRM

 
SQL>
SQL> CREATE TABLE session (
  2    department       CHAR(3),
  3    course           NUMBER(3),
  4    description      VARCHAR2(2000),
  5    max_lecturer     NUMBER(3),
  6    current_lecturer NUMBER(3),
  7    num_credits      NUMBER(1),
  8    room_id          NUMBER(5)
  9    );
Table created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("HIS", 101, "History 101", 30, 11, 4, 20000);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("HIS", 301, "History 301", 30, 0, 4, 20004);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("CS", 101, "Computer Science 101", 50, 0, 4, 20001);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("ECN", 203, "Economics 203", 15, 0, 3, 20002);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("CS", 102, "Computer Science 102", 35, 3, 4, 20003);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("MUS", 410, "Music 410", 5, 4, 3, 20005);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("ECN", 101, "Economics 101", 50, 0, 4, 20007);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("NUT", 307, "Nutrition 307", 20, 2, 4, 20008);
1 row created.
SQL>
SQL> INSERT INTO session(department, course, description, max_lecturer, current_lecturer, num_credits, room_id)
  2              VALUES ("MUS", 100, "Music 100", 100, 0, 3, NULL);
1 row created.
SQL>
SQL>
SQL> CREATE TABLE myLogTable (
  2    code             NUMBER,
  3    message          VARCHAR2(200),
  4    info             VARCHAR2(100)
  5    );
Table created.
SQL>
SQL>
SQL>
SQL> DECLARE
  2    myException EXCEPTION;
  3    studentCount NUMBER(3);
  4    studentMax NUMBER(3);
  5
  6    v_ErrorCode NUMBER;
  7    v_ErrorText VARCHAR2(200);
  8
  9  BEGIN
 10    SELECT current_lecturer, max_lecturer
 11      INTO studentCount, studentMax
 12      FROM session
 13      WHERE department = "HIS" AND course = 101;
 14    IF studentCount > studentMax THEN
 15      RAISE myException;
 16    END IF;
 17  EXCEPTION
 18    WHEN myException THEN
 19      INSERT INTO myLogTable (info) VALUES ("History 101 has " || studentCount ||
 20        "lecturer: max allowed is " || studentMax);
 21    WHEN OTHERS THEN
 22      v_ErrorCode := SQLCODE;
 23      v_ErrorText := SUBSTR(SQLERRM, 1, 200);  -- Note the use of SUBSTR here.
 24      INSERT INTO myLogTable (code, message, info) VALUES
 25        (v_ErrorCode, v_ErrorText, "Oracle error occurred");
 26  END;
 27  /
PL/SQL procedure successfully completed.
SQL>
SQL> drop table session;
Table dropped.
SQL> drop table myLogTable;
Table dropped.
SQL>