Oracle 10gR2+ conditional PL/SQL Compilation
Oracle Database 10g Release 2 delivers a new PL/SQL language feature: conditional compilation. The feature is elegant and easy to understand.

Oracle Database 10g Release 2 delivers a new PL/SQL language feature: conditional compilation. The feature is elegant and easy to understand.
There are three kinds of PL/SQL conditional compilation construct: the selection directive, the inquiry directive, and the error directive. Code_1 uses all of these. The
following sections discuss them in detail and suggest some best-practice principles for their use.
The selection directive
The motivating requirement for conditional compilation is that it must select between alternative fragments of source text at compilation time. PL/SQL
conditional compilation satisfies this requirement with the selection directive. Code_2 illustrates this.
Code_2 relies on the package CC_Control: Code_2
$if CC_Control.Trace_Level > 0 $then Print(Sparse_Collection.Count()); $if CC_Control.Trace_Level > 1 $then declare Idx Idx_t := Sparse_Collection.First(); begin while Idx is not null loop Print(Idx||' '||Sparse_Collection(Idx)); Idx := Sparse_Collection.Next(Idx); end loop; end; $end $end
package CC_Control is
Trace_Level constant pls_integer := 2; end CC_Control; package CC
The selection directive must test a so-called static boolean expression. The rules that such an expression must satisfy are defined in the PL/SQL User’s Guide and
Reference book; the evaluation of a static expression must always give the same result unless anything it depends on — a static package constant or the result of
an inquiry directive (see The inquiry directive on page 9) — has been deliberately changed. A static boolean expression must be composed using so-called static
package constants, literals, or inquiry directives. Thus, for example, d in the following is not a static package constant:
package CC_Control is ... d constant pls_integer := To_Char(Sysdate, 'J'); end CC_Control;
Therefore, the following is not a static boolean expression
CC_Control.d > 0 or CC_Control.Trace_Level
The selection directive uses these special building blocks
$if $then $elsif $else $end
The meaning of each is exactly symmetrical with that of the corresponding run-time if building block. (Notice, though, that the selection directive ends with just $end
rather than with $end $if.) The rules for evaluating the static boolean expression that the selection directive uses
are the same rules that PL/SQL uses at run-time. In particular, null has its usual significance.
Examples
create or replace procedure p as begin $IF $$debug_code $THEN dbms_output.put_line( 'Our debug code' ); dbms_output.put_line( 'Would go here' ); $END dbms_output.put_line( 'And our real code here' ); end; / alter procedure P compile plsql_ccflags = 'debug_code:true' reuse settings /
create or replace procedure p2 as begin $if $$plsql_debug $then $error 'This program must be compiled with PLSQL_DEBUG disabled' $end $end dbms_output.put_line( 'This is where it happens!' ); end; / Procedure created ALTER SESSION SET PLSQL_DEBUG=TRUE / alter procedure p2 compile / Warning: Procedure altered with compilation errors. show err Errors for PROCEDURE P2: LINE/COL ERROR -------- ----------------------------------------------------------------- 6/5 PLS-00179: $ERROR: This program must be compiled with PLSQL_DEBUG disabled
Source:
- http://www.oracle.com/technetwork/database/features/plsql/overview/plsql-conditional-compilation-133587.pdf
- Oracle 10gR2 – Conditional PL/SQL Compilation – AMIS Oracle and Java Blog