Oracle PL/SQL - Compiling Different Code for Different Database Versions

Introduction

The following code sets the values of the user-defined inquiry directives $$my_debug and $$my_tracing and then uses conditional compilation:

Demo

SQL>
SQL>--   w  ww.  j a  v a  2 s.  co m
SQL> ALTER SESSION SET PLSQL_CCFLAGS = 'my_debug:FALSE, my_tracing:FALSE';

Session altered.

SQL>
SQL> CREATE OR REPLACE PACKAGE my_pkg AS
  2    SUBTYPE my_real IS
  3      $IF DBMS_DB_VERSION.VERSION < 10 $THEN
  4        NUMBER;
  5      $ELSE
  6        BINARY_DOUBLE;
  7      $END
  8    my_pi my_real;
  9    my_e  my_real;
 10  END my_pkg;
 11  /

Package created.

SQL>
SQL> CREATE OR REPLACE PACKAGE BODY my_pkg AS
  2  BEGIN
  3    $IF DBMS_DB_VERSION.VERSION < 10 $THEN
  4      my_pi := 3;
  5      my_e  := 2;
  6    $ELSE
  7      my_pi := 4;
  8      my_e  := 3;
  9    $END
 10  END my_pkg;
 11  /

Package body created.

SQL>
SQL> CREATE OR REPLACE PROCEDURE circle_area(radius my_pkg.my_real) IS
  2    my_area       my_pkg.my_real;
  3    my_data_type  VARCHAR2(30);
  4  BEGIN
  5    my_area := my_pkg.my_pi * (radius**2);
  6
  7    DBMS_OUTPUT.PUT_LINE('Radius: ' || TO_CHAR(radius) || ' Area: ' || TO_CHAR(my_area));
  8
  9    $IF $$my_debug $THEN
 10      SELECT DATA_TYPE INTO my_data_type
 11      FROM USER_ARGUMENTS
 12      WHERE OBJECT_NAME = 'CIRCLE_AREA'
 13      AND ARGUMENT_NAME = 'RADIUS';
 14      DBMS_OUTPUT.PUT_LINE('Data type of the RADIUS argument is: ' || my_data_type);
 15    $END
 16  END;
 17  /

Procedure created.

Related Topic