Run SCRIPT from PL/SQL Block

℡╲_俬逩灬. 提交于 2019-11-29 08:05:06

It is 2012 2017. Scripts are a clunky and brittle hangover from the last millennium. Oracle has a fantastic range of functionality we can execute in PL/SQL, plus there's Java Stored Procedures, and there's scheduling for starting jobs. Other than running DDL to create or amend schemas there is hardly any need for scripts in an Oracle database environment; even DDL scripts should be triggered from an external client, probably a build tool such as TeamCity.

In particular I would regard attempting to run a SQL script from a PL/SQL program as an architectural failure. What are you doing with the script which you cannot do with a stored procedure?

As for passing input to a stored procedure, that's what parameters are for. PL/SQL isn't interactive, we need a client to enter the values. Depending on the scenario this can be done asynchronously (values in a file or a table) or synchronously (calling the stored procedure from SQL*Plus, SQL Developer or a bespoke front end).


Having said all that, in the real world we work with messy architectures with inter-dependencies between the database and the external OS. So what can we do?

  1. We can write a Java Stored Procedure to execute shell commands. This is the venerable solution, having been around since Oracle 8i. Find out more.
  2. In 10g Oracle replace DBMS_JOB with DBMS_SCHEDULER. Once of the enhancements of this tool is its ability to run external jobs i.e. shell scripts. Find out more.
  3. Since Oracle 11g R1 external tables support pre-processor scripts, which run shell commands before querying the table. Find out more.

Note that all these options demand elevated access (grants on DIRECTORY objects, security credentials, etc). These can only be granted by privileged users (i.e. DBAs). Unless our database has an astonishingly lax security configuration there is no way for us to run an arbitrary shell script from PL/SQL.


Finally, it is not clear what benefit you expect from running a SQL script in PL/SQL. Remember that PL/SQL runs on the database server, so it can't see scripts on the client machine. This seems relevant in the light of the requirement to accept user input.

Perhaps the simplest solution is reconfiguration of the original script. Split out the necessary PL/SQL call into a block and then just call the named script:

begin
   proc(para1,para2);
end;
/   
@prompt1.sql

You can write a pl/sql block in SqlPlus to check for a parameter from a table then execute a script. In the script to be executed (MyScript.sql below), the statement terminators must be ";" instead of "/"

    declare
    vMyParameter number := 0;  
    begin

    select count(*) into vMyParameter
    from MyTable
    where MyCheckValue = 'Y';

    if vMyParameter = 1 then
    @MyFolder/MyScript.sql;

    end if;
    end;
    /

If you're using sql*plus (or a tool that is using it) then you can do something like this:

set serveroutput on
variable a number;
begin
:a := &promt;
dbms_output.put_line(:a);
end;
/

If it runs in batch then you can do:

variable a number;
begin
:a := &1;
dbms_output.put_line(:a);
end;

and get the value for :a as a parameter-

sqlplus sdad/fdsfd@fdggd @<your_script.sql> <val_for_a>
victor

Another practice is to execute on one *.bat with parameters, like:

Example c:/oracle/bin/sqlplus.exe -w @c:/name

sql %1 %2 @c:/output.sql

execute immediate 'start prompt1' ;

Execute immediate is to execute SQL statements , not arbitrary commands.

can i get a value from prompt1 into my PL/SQL block where am calling the script

You can run a run script - but I doubt you can capture input from an SQL script, esp within a PL/SQL block

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!