Re: Conditional logic in a main SQLPLUS script file

  • From: Stephane Faroult <sfaroult@xxxxxxxxxxxx>
  • To: dylan.farre@xxxxxxxxx
  • Date: Wed, 05 Jan 2011 12:23:18 +0100

The simples thing that comes to my mind is to add one script layer -
generate your script from a script that includes some PL/SQL

Something such as

set feedback off
set trimspool on
set recsep off
set serveroutput on
spool main_script.sql
begin
   dbms_output.put_line('@scrip_0.sql');
   if packageA.option_1 = 1
   then
     dbms_output.put_line('@script_1.sql');
  else
     dbms_output.put_line('@script_2.sql');
  end if;
end;
/
spool off
@main_script.sql


HTH

Stephane Faroult
RoughSea Ltd <http://www.roughsea.com>
Konagora <http://www.konagora.com>
RoughSea Channel on Youtube <http://www.youtube.com/user/roughsealtd>

On 01/05/2011 12:12 PM, Dylan Farre wrote:
> 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: