Run SCRIPT from PL/SQL Block

后端 未结 5 1054
执笔经年
执笔经年 2020-12-03 20:17

How to use \"START SCRIPT\" in pl/sql block ?

I want to use something like this

declare
begin
   proc(para1,para2);
   execute immediate \'start prom         


        
相关标签:
5条回答
  • 2020-12-03 20:36

    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>
    
    0 讨论(0)
  • 2020-12-03 20:37

    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
    
    0 讨论(0)
  • 2020-12-03 20:40

    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

    0 讨论(0)
  • 2020-12-03 20:42

    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

    0 讨论(0)
  • 2020-12-03 20:56

    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;
        /
    
    0 讨论(0)
提交回复
热议问题