Conditional logic in a main SQLPLUS script file

  • From: Dylan Farre <dylan.farre@xxxxxxxxx>
  • To: oracle-l@xxxxxxxxxxxxx
  • Date: Wed, 5 Jan 2011 11:12:45 +0000

I am looking for a way to implement conditional logic in a main sql script
file, that calls other script files.

In PLSQL, this is easy using regular IF THEN ELSE statements. It is even
easy to add or remove blocks of PLSQL from compiled objects (using
pre-compiler directives). For example, in a stored procedure, by including
something like this:

CREATE OR REPLACE PROCEDURE ZZZ AS
..

BEGIN

  $IF packageA.option_1 = 1 $THEN
    do_something();
  $ELSE
    do_something_else();
  $END

END;
/

Which will create two different versions of procedure ZZZ, depending on the
value of packageA.option_1. Sometimes this is handy, if most of the code
in procedure ZZZ is the same and there is only a small difference required
under certain circumstances.

But what I want to do is something like this:

@script_0.sql
 $IF packageA.option_1 = 1 $THEN
    @script_1.sql
 $ELSE
    @script_2.sql
 $END
...

I understand that this does not work, but is there a another way to achieve
what I am trying to do here?

Any suggestions? Thanks.

Other related posts: