2

Can someone explain the concept of Conditional Compilation in PL/SQL? I have researched but can not quite understand the reasoning behind using it and what it actually does. An example would be great!

Also, I would like to know more about Conditional Compilation Control Tokens. What is a token in PL/SQL?

Thank you in advance for your help.

1 Answers1

5

The best resource for this is Oracle's own documentation (11g, 12c)

However, the concept of conditional compilation is that it can be used to compile your code differently based on some static condition at compile time.

For example you might originally write a piece of code in one version of Oracle, say for example Oracle 11g. Development moves along, and perhaps you discover in Oracle 12c that there new features that can do the same thing better. You could use conditional compilation to compile the original code in an 11g instance, but compile the newer 12c optimized code only 12c and newer databases. For example:

begin
  $IF DBMS_DB_VERSION.VERSION < 12 $THEN
    -- Do your old school stuff here
  $ELSE
    -- Do the new stuff here
  $END
end;

The DBMS_DB_VERSION package contains static constants such as the VERSION constant used above. You can use any constant PLS_INTEGER or BOOLEAN values defined in any package in the conditional compilation boolean expressions. This means that you can even base the conditional compilation on specific versions of your own code provided you include a constant revision number or constant boolean flag in the package spec. For example:

begin
  $IF MY_PACKAGE.REVISION < 2 $THEN
    -- Use the legacy code here
  $ELSIF MY_PACKAGE.REVISION < 4 $THEN
    -- Use the newer code here
  $ELSE
    -- Use the latest code here
  $END
end;
Sentinel
  • 6,379
  • 1
  • 18
  • 23