When you want to run a SQL script, you rather use sqlplus. If you want to run it within a PL/SQL stored procedure, you are screwed. You could redo the logic of sqlplus, this is what any decent IDE and code runnner does, and it’s a pile of work and you will get lot’s of bugs…
Another approach is to use sqlplus.
Wait! calling sqlplus from plsql? Well… why not. For instance via external job
- create a table with sql scripts
- create a shell script, /tmp/ddl, that spool and executes T.text content
- create a UNIX credential
- create and run an external job
create table t(id number primary key, text clob); insert into t(id, text) values ( 42, 'update emp set sal=sal*2 /* XMas gift */;' );
#!/bin/ksh ORACLE_HOME=/u01/app/oracle/product/12.1.0/db_1 export ORACLE_HOME $ORACLE_HOME/bin/sqlplus -s -L scott/tiger < begin dbms_scheduler.create_credential( 'C','USER01','PW'); end; /begin DBMS_SCHEDULER.CREATE_JOB( job_name => 'J', job_type => 'EXECUTABLE', number_of_arguments => 1, job_action => '/tmp/ddl', credential_name => 'C'); DBMS_SCHEDULER.SET_JOB_ARGUMENT_VALUE( 'J', 1, '42'); DBMS_SCHEDULER.run_job('J'); end; /
CREATE EXTERNAL JOB privilege is powerfull and your DBA won't like it 😉
Pingback: Restricted sqlplus and sql_script – Laurent Schneider