Exforsys

H I D E

Home arrow Technical Training arrow Oracle 11g Tutorial

Conditional compilation in Oracle PL/SQL Page - 2

Page 2 of 3
Author: Saurabh Gupta     Published on: 28th Jun 2011

Conditional compilation in Oracle PL/SQL

2.3. Error directive

The error directive $ERROR $END handles the error situation in compilation conditions. If the program doesn’t fits for compilation in any of the database environments, its creation can be restricted by raising compilation errors. The program would be created in INVALID state and the compilation error would be held up with USER_ERRORS dictionary view, until it is resolved. It can be displayed using SHOW ERRORS command in SQL. Oracle associates a generic message number PLS-00179 with the compilation errors.

Example Syntax [3]

Sample Code
  1. $ERROR varchar2_static_expression $END
Copyright exforsys.com


3. DBMS_DB_VERSION

It is an Oracle supplied package, introduced in Oracle 10g Release 2, to hold the persistent and static constants to denote the database versions and its release number. The constants can be referenced externally to get information about the current version, current release, and applicable database version. For demonstration, See the table below shows the values of the constants as applicable to Oracle 11g release 2.

Values of the constants
Constant Attributes Type Value
VERSION CONSTANT PLS_INTEGER 11
RELEASE CONSTANT PLS_INTEGER 2
VER_LE_9_1 CONSTANT BOOLEAN FALSE
VER_LE_9_2 CONSTANT BOOLEAN FALSE
VER_LE_9 CONSTANT BOOLEAN FALSE
VER_LE_10_1 CONSTANT BOOLEAN FALSE
VER_LE_10_2 CONSTANT BOOLEAN FALSE
VER_LE_10 CONSTANT BOOLEAN FALSE
VER_LE_11_1 CONSTANT BOOLEAN TRUE
VER_LE_11 CONSTANT BOOLEAN TRUE

















4. Illustration of Conditional compilation

We take up the previous scenario where a support team maintains a testing database in 10g version and development database in 11g version. A new customization requires a new procedure creation.

Result caching feature was introduced in Oracle 11g release. To make use of the feature, programmers design a single procedure and impose conditions upon the compiler to evaluate selected code applicable for their database version.

A function P_GET_SAL has been created which returns the salary of an employee. For 11g environment, it must declare a cursor with result cache feature. On the other hand, for 10g environment, the cursor should be a normal one. Check the compilation conditions in the below function code. It uses DBMS_DB_VERSION constant to get the database version.

Sample Code
  1. CREATE OR REPLACE FUNCTION P_GET_SAL (P_EMPID NUMBER)
  2. RETURN NUMBER
  3. IS
  4. L_SAL NUMBER;
  5. $IF DBMS_DB_VERSION.VERSION > 11 $THEN
  6.   CURSOR C IS
  7.   SELECT /*+ RESULT_CACHE */ SALARY
  8.   FROM EMPLOYEES
  9.   WHERE EMPLOYEE_ID = P_EMPID;
  10. $ELSE
  11.   CURSOR C IS
  12.   SELECT SALARY
  13.   FROM EMPLOYEES
  14.   WHERE EMPLOYEE_ID = P_EMPID;
  15. $END
  16. BEGIN
  17.    OPEN C;
  18.    FETCH C INTO L_SAL;
  19.    CLOSE C;
  20.    RETURN L_SAL;
  21. END;
  22. /
  23. FUNCTION created.
Copyright exforsys.com


Testing the object level compilation parameters – Object level compilation flags can be set to read important information about the object. They are used by the inquiry directives within the compilation conditions.

Before that, you will need to check current setting of compilation flags.

Sample Code
  1. SQL> SELECT PLSQL_CCFLAGS FROM USER_PLSQL_OBJECT_SETTINGS
  2. WHERE NAME = ‘P_GET_SAL’;
  3.  
  4. PLSQL_CCFLAGS
  5. -----------------------
Copyright exforsys.com


Include a compilation flag TESTING in the session and recompile the function P_GET_SAL using the session compilation flags.

Sample Code
  1. SQL> ALTER SESSION SET PLSQL_CCFLAGS = ‘TESTING:TRUE’;
Copyright exforsys.com


From now onwards, all the objects created in the current session would inherit the same compilation flags. I shall create a function F_GETSAL_USING_COMP_PARAM, which uses the TESTING flag. Note the usage of inquiry directive.

Sample Code
  1. SQL> CREATE FUNCTION F_GETSAL_USING_COMP_PARAM(P_EMPID NUMBER)
  2.   2  RETURN NUMBER
  3.   3  IS
  4.   4  L_SAL NUMBER;
  5.   5  $IF $$TESTING $THEN
  6.   6    CURSOR C IS
  7.   7    SELECT /*+ RESULT_CACHE */ SALARY
  8.   8    FROM EMPLOYEES
  9.   9    WHERE EMPLOYEE_ID = P_EMPID;
  10.  10  $ELSE
  11.  11    CURSOR C IS
  12.  12    SELECT SALARY
  13.  13    FROM EMPLOYEES
  14.  14    WHERE EMPLOYEE_ID = P_EMPID;
  15.  15  $END
  16.  16  BEGIN
  17.  17     OPEN C;
  18.  18     FETCH C INTO L_SAL;
  19.  19     CLOSE C;
  20.  20     RETURN L_SAL;
  21.  21* END;
  22.        
  23. FUNCTION created.
Copyright exforsys.com


Inquiry directives can even make use of static packaged variables while defining the compilation conditions. I shall create a package PKG_COND_COMPILE to declare the statics constants.

Sample Code
  1. CREATE OR REPLACE PACKAGE PKG_COND_COMPILE IS
  2.  $IF DBMS_DB_VERSION.VERSION >= 11 $THEN
  3.     PARAM_TESTING CONSTANT BOOLEAN := TRUE;
  4.  $ELSE
  5.     PARAM_TESTING CONSTANT BOOLEAN := FALSE;
  6.  $END
  7. END;
Copyright exforsys.com


Note that the packaged variable must be a static constant, else it would raise exception ‘PLS-00174: a static boolean expression must be used’. Refer the below function which uses the package variables.

Sample Code
  1. CREATE OR REPLACE FUNCTION P_GET_SAL (P_EMPID NUMBER)
  2. RETURN NUMBER
  3. IS
  4. L_SAL NUMBER;
  5. $IF PKG_COND_COMPILE.PARAM_TESTING $THEN
  6.   CURSOR C IS
  7.   SELECT /*+ RESULT_CACHE */ SALARY
  8.   FROM EMPLOYEES
  9.   WHERE EMPLOYEE_ID = P_EMPID;
  10. $ELSE
  11.   CURSOR C IS
  12.   SELECT SALARY
  13.   FROM EMPLOYEES
  14.   WHERE EMPLOYEE_ID = P_EMPID;
  15. $END
  16. BEGIN
  17.    OPEN C;
  18.    FETCH C INTO L_SAL;
  19.    CLOSE C;
  20.    RETURN L_SAL;
  21. END;
  22. /
  23.  
  24. FUNCTION created.
Copyright exforsys.com


Demonstrating error directive: An error number can be included under compilation flags using ALTER SESSION command as below.

Sample Code
  1. SQL> ALTER SESSION SET PLSQL_CCFLAGS = 'TESTING:TRUE,DEV:FALSE,ERR:83949';
  2. Session altered.
Copyright exforsys.com


The function below F_CHECK_ERR checks the compilation flag TESTING to return 1 when it is true. If its creation has to be restricted in all other databases, it must raise compilation error upon execution. Check the function below.

Sample Code
  1. CREATE OR REPLACE FUNCTION F_CHECK_ERR
  2. RETURN NUMBER
  3. IS
  4. BEGIN
  5.    $IF $$TESTING $THEN
  6.            RETURN 1;
  7.    $ELSE
  8.            $ERROR 'This is a Development database:'||$$ERR $END
  9.            RETURN 0;
  10.    $END
  11. END;
  12. /
  13.  
  14. FUNCTION created.
Copyright exforsys.com


Now we execute the function in a SELECT statement.

Sample Code
  1. SQL> SELECT F_CHECK_ERR FROM DUAL;
  2.  
  3. F_CHECK_ERR
  4. -----------
  5.           1
Copyright exforsys.com


Now, Let's modify the value of TESTING flag from TRUE to FALSE and recompile the above function to synchronize with the changes. Check, what happens upon compilation.

Ads

Sample Code
  1. SQL> ALTER SESSION SET PLSQL_CCFLAGS = 'TESTING:FALSE,DEV:TRUE,ERR:83949';
  2.  
  3. Session altered.
  4.  
  5. SQL> ALTER FUNCTION F_CHECK_ERR COMPILE;
  6.  
  7. Warning: FUNCTION altered WITH compilation errors.
  8.  
  9. SQL> SHOW ERR
  10. Errors FOR FUNCTION F_CHECK_ERR:
  11.  
  12. LINE/COL ERROR
  13. -------- -----------------------------------------------------------------
  14. 8/5      PLS-00179: $ERROR: This IS a Development DATABASE:83949
Copyright exforsys.com


Read Next: Oracle Pragma


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

Oracle 11g Tutorial

 

Comments