Pages

Thursday, July 19, 2018

if condition in sqlplus by Example

=================================
General
=================================
sqlplus is a non procedural language.
Thus implementing "if" condition in sql script is not straight forward.
This example, is using logic to determine weather the installation is running on EE or SE Oracle Edition, and per returned result, installing tables in Partitioned, or non Partitioned mode, or, in case table was already installed, skip the installation all together.

This is done via spooling a name of the file to be executed into generated gen_install_rep_online.sql file

Then executing the gen_install_rep_online.sql file.
=================================
Code Example, "if in sqlplus"
=================================
@../set_connection.sql
connect &&user/&&pass@&&connectstr

set serverout on
exec dbms_output.enable(100000); 
set heading off linesize 132 pagesize 1000  heading off feedback off

spool gen_install_rep_online.sql

DECLARE
  v_install_type     NUMBER;
  v_install_type_str VARCHAR2(100);
BEGIN  
  SELECT TO_CHAR(is_installed||is_ee) AS install_type 
   INTO v_install_type 
   FROM
  (
  SELECT (SELECT COUNT(*) FROM USER_TABLES WHERE table_name = 'REP_DAILY_DATA') AS is_installed,
         (SELECT COUNT(*) FROM V$VERSION WHERE UPPER(banner) LIKE '%ENTERPRISE EDITION%') as is_ee
  FROM DUAL
  );

  IF v_install_type = '01' THEN
    dbms_output.put_line('@./install_partitioned_rep_online.sql');
  ELSIF v_install_type = '00' THEN    dbms_output.put_line('@./install_non_partitioned_rep_online.sql');  
  ELSE
    dbms_output.put_line('@./do_nothing.sql');  
  END IF;
END;
/

spool off  

set heading on serverout off verify on define on
@../set_connection.sql
@./gen_install_rep_online.sql

No comments:

Post a Comment