Exforsys

H I D E

Home arrow Technical Training arrow Oracle 11g Tutorial

Oracle Pragma EXCEPTION_INIT

Page 3 of 4
Author: Saurabh Gupta     Published on: 6th Jul 2011

Oracle Pragma

Oracle Pragma EXCEPTION_INIT

The pragma EXCEPTION INIT allows the association of a self chosen error number with a user defined exception. The error number can be any of the system recognized error number.

Pragma EXCEPTION_INIT Syntax

Sample Code
  1. PRAGMA EXCEPTION_INIT([EX NAME],[ERROR NUMBER], [TRUE | FALSE])
Copyright exforsys.com


In the PL/SQL block below, the LOCAL_EXCEPTION exception is linked with the error number -100 through the PRAGMA EXCEPTION_INIT. Note the usage of SQLCODE, which returns the same associated error number.

Pragma EXCEPTION_INIT Example Code

The procedure P_GET_ORDERS has been created to get the order code and order quantity for a given date. If no order booking has been made, procedure must display appropriate message.

Sample Code
  1. CREATE OR REPLACE PROCEDURE P_GET_ORDERS (P_ORD_DATE DATE)
  2. IS
  3.    L_ORD_CODE VARCHAR2(100);
  4.    L_ORD_QTY NUMBER;
  5.    ORD_EXCEPTION EXCEPTION;
  6.    PRAGMA EXCEPTION_INIT(ORD_EXCEPTION, -100);
  7. CURSOR C IS
  8.    SELECT ORD_CODE, ORD_QTY
  9.    FROM ORDERS
  10.    WHERE ORD_DATE= P_ORD_DATE;
  11. BEGIN
  12.    OPEN C;
  13.    FETCH C INTO L_ORD_CODE, L_ORD_QTY;
  14.    IF C%ROWCOUNT = 0 THEN
  15.       RAISE ORD_EXCEPTION;
  16.    END IF;
  17.    CLOSE C;
  18. EXCEPTION
  19.    WHEN ORD_EXCEPTION THEN
  20.      DBMS_OUTPUT.PUT_LINE ('No Orders booked on:'||TO_CHAR(P_ORD_DATE,'DD-MM-YYYY');
  21. END;
  22. /
  23.  
  24. SQL> EXEC P_GET_ORDERS(TRUNC(SYSDATE,'YYYY'));
  25.  
  26. No Orders booked ON 01-01-2011
  27.  
  28. PL/SQL procedure successfully completed.
Copyright exforsys.com


Oracle Pragma SERIALLY_REUSABLE

The pragma SERIALLY_REUSABLE enables the package state initialization for each and every server call. As per the conventional behavior of the packages, their state is initialized only during the first time they are referenced in a session. This state remains persistent for the complete session and is flushed off only at the termination of the session.

With the use of the pragma SERIALLY_REUSABLE, the package state is persistent only for a sever call. It gets initialized with every call made to the server while invoking any of its member construct.

Ads

Pragma SERIALLY_REUSABLE Syntax

Sample Code
  1. CREATE [OR REPLACE] PACKAGE [NAME] IS
  2. PRAGMA SREIALLY_REUSABLE
  3.    [MEMBER CONSTRUCT PROTOTYPES]
  4. END;
  5.  
  6. CREATE [OR REPLACE] PACKAGE BODY [NAME] IS
  7. PRAGMA SREIALLY_REUSABLE
  8.    [MEMBER CONSTRUCT DEFINTIONS]
  9. END;
Copyright exforsys.com


Pragma SERIALLY_REUSABLE Example Code

The package PKG_TEST_PRAGMA demonstrates non persistent package state at every server call.

Sample Code
  1. CREATE OR REPLACE PACKAGE PKG_TEST_PRAGMA
  2. IS
  3. PRAGMA SERIALLY_REUSABLE;
  4.    L_PKG_STATE NUMBER := 1;
  5.    PROCEDURE ASSIGN_SUM_VALUE(P_FACTOR NUMBER);
  6.    FUNCTION DISPLAY_VALUE RETURN NUMBER;
  7. END;
  8.  
  9. Package created.
  10.  
  11. CREATE OR REPLACE PACKAGE BODY PKG_TEST_PRAGMA
  12. IS
  13. PRAGMA SERIALLY_REUSABLE;
  14.    PROCEDURE ASSIGN_SUM_VALUE (P_FACTOR NUMBER)
  15.    IS
  16.    BEGIN
  17.       L_PKG_STATE:= L_PKG_STATE + P_FACTOR;
  18.       DBMS_OUTPUT.PUT_LINE(‘CHANGE IN package state’|| L_PKG_STATE);
  19.    END;
  20.  
  21.    FUNCTION DISPLAY_VALUE RETURN NUMBER
  22.    IS
  23.    BEGIN
  24.       RETURN L_PKG_STATE;
  25.    END;
  26. END;
  27.  
  28. Package body created.
  29.  
  30. SET SERVEROUT ON
  31. BEGIN
  32.   DBMS_OUTPUT.PUT_LINE(‘Package state before call: ’||PKG_TEST_PRAGMA.DISPLAY_VALUE);
  33.   PKG_TEST_PRAGMA.ASIGN_SUM_VALUE(5);
  34.   DBMS_OUTPUT.PUT_LINE(‘Package state after call: ’||PKG_TEST_PRAGMA.DISPLAY_VALUE);
  35. END;
  36. /
  37.  
  38. Package state before call: 1
  39. CHANGE IN package state: 6
  40. Package state after call: 1
  41.  
  42. PL/SQL procedure successfully completed.
Copyright exforsys.com


Read Next: UTL_FILE


 
This tutorial is part of a Oracle 11g Tutorial tutorial series. Read it from the beginning and learn yourself.

Oracle 11g Tutorial

 

Comments