SQLplus decode to execute scripts

前端 未结 2 402
后悔当初
后悔当初 2020-12-21 13:21

I am writing a script to be run in sqlplus 11. I have a user defined variable called compression. If this is true then I want to run the script CreateTablesCompression, othe

相关标签:
2条回答
  • 2020-12-21 14:05
    SQL> host cat foo.sql
    set scan on
    define compression=&1
    col scr new_value script
    set term off
    select decode('&compression', 'true', 'CreateTablesCompression', 'CreateTables') scr from dual;
    set term on
    @@&script
    
    SQL> @foo true
    run CreateTablesCompression.sql
    SQL> @foo false
    run CreateTables.sql
    
    0 讨论(0)
  • 2020-12-21 14:09

    Decode is not a SQL*PLUS command, you cannot use it directly in sql*plus only inside a pl/sql block or a query. So here is an example of how a conditional branching can be done: We declare a variable flag which going to regulate which one of two available scripts to run.

    SQL> variable flag varchar2(7);
    SQL> exec :flag := 'true';
    
    PL/SQL procedure successfully completed.
    
    SQL> column our_script new_value script noprint;
    SQL> select decode(:flag, 'true', 
      2                'c:\sqlplus\script1.sql', 
      3                'c:\sqlplus\script2.sql'
      4                ) our_script
      5  from dual;
    
    
    
    
    SQL> @&script;
    
    SCRIPT                                                                          
    --------                                                                        
    script_1                                                                        
    
    0 讨论(0)
提交回复
热议问题