Running stored procedure from unix shell

Hi
I have a stored procedure proc1 stored in a file, code1.txt in my home directory /home/user. How do i execute this file which contains the stored procedure from unix shell? I would really appreciate it if somebody gives me the complete shell script to accomplish the above task.
Thanks.

To put everything together into a single posting:
The EXEC command is a SQL*Plus macro command. It is not a SQL or PL/SQL command.
The only way to execute a stored proc from a client, is to wrap the call in an anonymous PL/SQL block. I.e you need a BEGIN and END PL/SQL wrapper around the call.
E.g.BEGIN
  -- calling a stored proc to start leave processing
  scott.StartLeaveProcessing;
END;The EXEC macro in SQL*Plus does this automatically for you. Thus less typing. But do not confuse this command with the PL/SQL language.
Second issue. Use bind variables when making calls from clients. And not just for SQL statements, but also for PL/SQL. Unfortuantely this tends to be a hack in SQL*Plus due to the way SQL*Plus itself treats its bind variable assignments. But in principle, this is what you should do when calling an Oracle stored proc from a client:
SQL> -- define a host variable
SQL> var EMPID varchar2(100)
SQL> var FROM_DEPT number
SQL> var TO_DEDPT number;
SQL>
SQL> -- assign values to these (this is where SQL*Plus hacks it)
SQL> exec :EMPID := 100;
SQL> exec :FROM_DEPT := 1;
SQL> exec :TO_DEPT := 2;
SQL>
SQL> -- now make the stored proc call for moving employee 100 from
SQL> -- department 1 to department 2
SQL> EXEC scott.EmployeeTransfer( :EMPID, :FROM_DEPT, :TO_DEPT );
SQL>To do this from a Unix shell script:
#!/bin/bash
# environment variables
# --> put environment such as ORACLE_HOME, ORACLE_SID, TWO_TASK
# etc. here <--
# redirect STDIN from TTY (keyboard typewriter device) to the input from
# this file - which means SQL*Plus will not read from the keyboard but read
# from this file its input until the EOF marker/text is encountered
sqlplus -s /nolog << EOF
connect scott/tiger
var EMPID varchar2(100)
var FROM_DEPT number
var TO_DEDPT number;
exec :EMPID := 100;
exec :FROM_DEPT := 1;
exec :TO_DEPT := 2;
exec scott.EmployeeTransfer( :EMPID, :FROM_DEPT, :TO_DEPT );
exit;
EOF
#eof

Similar Messages

  • Calling stored procedure from unix shell script

    Hello,
    I am facing a problem while calling a stored procedure from UNIX shell script. I want to return a output variable from the stored procedure to the UNIX environment.
    Here is the code-
    #!/bin/sh
    OUTPUT=`sqlplus cmag/magnum@dw <<ENDOFSQL
    set serveroutput on;
    var prd_out varchar2(100);
    exec create_pm_window(:prd_out);
    exit;
    ENDOFSQL`
    echo " output is - $OUTPUT"
    The problem is :prd_out is not getting copied to shell variable OUTPUT.
    I have a dbms_output.put_line in the stored proc create_pm_window and I can see that prd_out is getting populated.
    Any help is really appreciated.
    Thanks'
    Rakhee

    First step :
    make sure the PL/SQL works as expected.
    Does the following display the expected output executed from SQL*Plus ?
    set serverout on
    declare
    prd_out varchar2(100);
    begin
    create_pm_window(prd_out);
    dbms_output.put_line('output is '||prd_out);
    end;
    I don't have your procedure, but using a dummy procedure like :
    Scott@my10g SQL>create procedure foo(p_out in out varchar2)   
      2  is
      3  begin
      4  select 'Hello '||instance_name into p_out from v$instance;
      5  end;
      6  /
    Procedure created. and a toto.sh script as :OUTPUT=`sqlplus -s scott/tiger <<EOF
    set pages 0 lines 120 trimout on trimspool on tab off echo off verify off feed off serverout on
    var mavar varchar2(100);
    exec foo(:mavar);
    print mavar;
    exit;
    EOF`
    echo "OUT = ${OUTPUT}"
    exitIt works fine :[oracle@Nicosa-oel ~]$ ./toto.sh
    OUT = Hello my10g

  • Execute stored procedure from Unix shell script

    My current method of executing stored procedures (wpl_1 and wpl_2) from a unix shell script is as follows:
    <<wpl.sh>>
    sqlplus user/password @/home/oracle/scripts/wpl.sql
    <<wpl.sql>>
    set serveroutput on size 1000000
    set timing on
    execute wpl_1('0000010676','~')
    execute wpl_2('0000010676','~')
    execute wpl_1('0000010236','FIX')
    execute wpl_2('0000010236','FIX')
    exit
    Question: Is it possible to combine the two scripts (unix and oracle) together?

    A little rusty on this, but this may work:
    My current method of executing stored procedures
    (wpl_1 and wpl_2) from a unix shell script is as
    follows:
    <<wpl.sh>>sqlplus user/password @/home/oracle/scripts/wpl.sql << EOF
    set serveroutput on size 1000000
    set timing on
    execute wpl_1('0000010676','~')
    execute wpl_2('0000010676','~')
    execute wpl_1('0000010236','FIX')
    execute wpl_2('0000010236','FIX')
    exit
    EOF
    >
    Question: Is it possible to combine the two scripts
    (unix and oracle) together?

  • How can i call a Stored Procedure procedure from Unix shell script

    Hi All,
    I want to call a Strored PL-SQL Procedure through Unix shell script.
    Can any body help me with this.
    Regards,
    Saurabh

    I prefer a seperate script like the other poster mentioned. However, most shells can use a 'here' document as well ...
    sqlplus uid/pwd <<END
    exec myproc
    exit
    ENDRichard

  • Running stored procedures from HTML DB

    Hello.
    Is it possible to run stored procedures/functions from HTML DB? If yes, how to do it?
    Davide

    If the variable v_return_amount is a number, how can you assign a boolean to it? Are you sure your function is valid? I would write it slightly different:
    CREATE OR REPLACE FUNCTION verify_amount (
    v_invoices_pk IN NUMBER,
    v_new_amount IN NUMBER
    RETURN BOOLEAN
    IS
    CURSOR c_amount
    IS
    SELECT amount
    FROM invoices
    WHERE invoices.invoices_pk = v_invoices_pk;
    CURSOR c_dist_amount
    IS
    SELECT SUM (cost_distribution.amount)
    FROM cost_distribution
    WHERE cost_distribution.invoices_pk = v_invoices_pk;
    v_sum_amount NUMBER;
    v_invoices_amount NUMBER;
    BEGIN
    OPEN c_dist_amount;
    FETCH c_dist_amount
    INTO v_sum_amount;
    CLOSE c_dist_amount;
    OPEN c_amount;
    FETCH c_amount
    INTO v_invoices_amount;
    CLOSE c_amount;
    IF (v_invoices_amount <= v_sum_amount + v_new_amount)
    THEN
    RETURN TRUE;
    ELSE
    RETURN FALSE;
    END IF;
    RETURN v_return_amount;
    EXCEPTION
    WHEN OTHERS
    THEN
    RETURN FALSE;
    END;
    Other than that, I am not sure about your cursors but I can't judge from here. The other thing is how you use the function in the validation process. Can you try to correct and tell if it worked out?
    Denes Kubicek

  • Running Stored procedures from Forms

    I have several stored procedures (stored in the database) that I
    want to run from Forms (client), but when they're running my
    application (in Forms) is frozen until the procedure end.
    I don't know how to run that stored without lock the application,
    somebody Can Help me???
    THANKS
    null

    The second parameter of the DBMS_JOB.SUBMIT procedure needs to be
    a valid PL/SQL call, i.e. it must at least be terminated by a
    semicolon. You could also wrap it into an anonymous PL/SQL block,
    like: 'BEGIN SP_CREA_FACTURAS_PADRE; END;'
    Marisol (guest) wrote:
    : Dietmar:
    : I put this code in my application and I didn't get any response
    : DBMS_JOB.SUBMIT( iJobNumber,
    : 'SP_CREA_FACTURAS_PADRE',
    : SYSDATE,
    : 'null',
    : FALSE);
    : COMMIT;
    : When I select the option that executes this code my application
    : ends without any message...
    : You know why is this happen?
    : How can I resolve??
    : Thanks :)
    null

  • Running Stored Pocedures from UNIX

    Hi,
    Does anybody have an example of a Unix script that kicks off a series of stored procedures (after checking the status of the previous one) within a package?
    Thanks
    Raj

    hi
    use like this
    sqlplus -s bo/bopassword@wsstest @t.sql
    PL/SQL procedure successfully completed.
    contents of t.sql are like this
    exec some_proc
    exit
    once you execute the above command sqlplus -s ....
    it connects in silent mode with the specified username,password and connect string and it executes the specified script
    and the output of the commands in the script t.sql will be returned. capture the output/redirect the output of the command
    and check for the string PL/SQL procedure succesfully completed.
    then you can think that the procedure is executed succesfully if not you can take necessary action
    hope this helps
    all the best
    prakash
    [email protected]

  • How to Run Stored Procedures from Teststand

    I am trying to get teststand to run an SQL stored procedure but I always get the following error
    Error executing substep 'Post'.
    The following SQL command failed: 'EXECUTE cdt_st_test...'
    Native error code -2147217900 0x80040e14
    Microsoft OLE DB Provider for SQL Server:
    Syntax error or access violation
    It is a test stored procedure that doesn't pass any parameters.
    Which steps must I use and how do I pass parameters.

    I have sussed it now. I was setting the "Command Type" to stored procedure instead of default. I changed it back and did "EXECUTE cdt_st_test" and it works. I have also added the parameters there as well.

  • Executin oracle stored procedure from shell script

    Hi everyone,
    I want to know how to execute a orace stored procedure from a shell script not a sql file.
    Its a oracle stored procedure with in parameters.
    Can anyone send immediate reply its very urgent.
    Thanks in Advance
    with regards
    Thazul

    Hello everyone,
    Whenever i am using the previous script
    because of <<! and !>> in the beginging and the end of oracle scrip. Shell is displaying the error 'newline or ;' unxexpeted and the error no is 48.
    After that i used a different script like
    DIRRUNNER=/oracle/tvg
    count=0
    while [ 1 -eq 1 ]
    do
    for x in `ls $DIRRUNNER/RUNNER_*.TXT`
    do
    count=`grep -c $x $DIRRUNNER/runner.log`
    if [ $count -lt 1 ]
    then
    chstr=`echo $x|awk -F/ '{printf $NF }'`
    dir=$DIRRUNNER
    file=$chstr
    PATH=$PATH:/usr/local/bin:.
    export PATH
    ORACLE_SID=BBPROD
    ORAENV_ASK=NO
    . oraenv
    ORAENV_ASK=YES
    echo "exec runrace('$dir', '$file')" > runner.sql
    sqlplus -s tvgus/tvgus@bbprod @runner.sql
    echo $x >>$DIRRUNNER/runner.log
    fi
    done
    count=0
    sleep 2
    done
    The above one is working fine but after executing the procedure control is still remaing in the sqlplus environment, but i want the control back to my shell
    Can anyone help to resolve this, its pretty urgent.
    Thanks in Advance
    Thazul.
    null

  • How to execute oracle procedure from Unix

    Hi All,
    Could you please help me in Unix commands.
    we have different databases i want to run one procedure from unix for one database.
    How can i run pls give me the flow.
    Thanks,

    Oracle wrote:
    Hi All,
    Could you please help me in Unix commands.
    we have different databases i want to run one procedure from unix for one database.
    How can i run pls give me the flow.
    Thanks,you give the command to launch sqlplus, and pass it the appropriate sql or sqlplus commands.
    Like this:
    oracle> sqlplus /nolog <<EOF
    conn / as sysdba
    exec mystoredprocedure
    exit
    EOFor this
    oracle> sqlplus /nolog @somefile.sqlwhere 'somefile.sql' has the sqlplus commands needed.

  • Execute stored procedure from DOS or Unix Shell

    Hi, need ur help again,
    How to execute the stored procedure from DOS or Unix Shell?
    Thanks!

    sqlplus -s /NOLOG @your_sql_script.sql
    -- your_sql_script.sql :
    connect user/password@connect_string
    execute package.procedure (param1, param2, ...) -- for packages
    execute procedure (param1,...) -- for procedures
    exit ;this way you won't advertise your user/password on unix systems

  • Calling pl/sql stored procedure from shell script ( kshell)

    hello.,
    i have written a stored procedure. i need to call this stored procedure from a unix shell script ( korn shell).
    can anyone please help me how can i do it.actually there are 3 stored procedures. so my shell script has to call each stored procedure, execute it then go to next stored procedure execute it.
    if by chance inbetween any stored procedure do not execute then the shell script has to tell me that this stored procedure failed to execute.
    please help me in this.
    thanks
    madan

    Sorry Madan,
    Following is the complete solutiion:
    !# /usr/bin/ksh
    sqlplus -s user/pass@server << EOF > a.log
    exec proc1;
    exec proc2;
    exec proc3;EOF
    if [ $? -eq 0 ]
    then
            echo "\n Procedures completed successfully at `date`"
    else
            echo "!!! FAILED!!!"
            echo "Details can be found in a.log File"
    fi

  • Running a SQL Stored Procedure from Power Query with Dynamic Parameters

    Hi,
    I want to execute a stored procedure from Power Query with dynamic parameters.
    In normal process, query will look like below in Power Query. Here the value 'Dileep' is passed as a parameter value to SP.
        Source = Sql.Database("ABC-PC", "SAMPLEDB", [Query="EXEC DBO.spGetData 'Dileep'"]
    Now I want to pass the value dynamically taking from excel sheet. I can get the required excel cell value in a variable but unable to pass it to query.
        Name_Parameter = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
        Name_Value = Name_Parameter{0}[Value],
    I have tried like below but it is not working.
    Source = Sql.Database("ABC-PC", "SAMPLEDB", [Query="EXEC DBO.spGetData Name_Value"]
    Can anyone please help me with this issue.
    Thanks
    Dileep

    Hi,
    I got it. Below is the correct syntax.
    Source = Sql.Database("ABC-PC", "SAMPLEDB", [Query="EXEC DBO.spGetData '" & Name_Value & "'"]
    Thanks
    Dileep

  • How to run a stored procedure from sqlplus

    scehma name: aa
    package name: bb
    procedure name : cc
    I run the procedure from sql plus like this...
    execute aa.bb.cc
    I got error " not defined procedure" any one knows why? thanks!

    Based on the information you provided:
    1. You are not connected as Schema aa
    2. Schema aa has not granted permission to you to execute package bb.
    To resolve this issue:
    Connect as schema aa
    execute following statement:
    Grant execute on bb to Your_userName;connect as Your_userName
    On SQL*Plus prompt now you can execute this procedure using:
    execute aa.bb.cc;Assuming there is no parameter for procedure cc.
    You can create synoyms also to avoid specifying "aa".
    Thanks,
    Dharmesh Patel

  • Unable to pass parameter in oracle procedure through unix shell script

    Hi Experts,
    I have oracle procedure where in I have to pass the value of procedure parameter through unix script.
    Follwoing is the sample procedure which i tried to exceute from the unix.
    Procedure:
    create or replace procedure OWNER.PRC_TESTING_OWNER(OWNER IN VARCHAR2) AS
    sql_stmt varchar2(1000) := NULL;
    v_count number := 0;
    v_owner varchar2(100) := owner;
    begin
    sql_stmt:='select count(1) from '||v_owner||'.EMP@infodb where rownum<=10';
    execute immediate sql_stmt into v_count;
    DBMS_OUTPUT.PUT_LINE(sql_stmt);
    DBMS_OUTPUT.PUT_LINE(v_count);
    END;The script which I used is:
    Unix
    #!/bin/ksh
    parm=$1
    echo start
    sqlplus -s scott@DEV/tiger <<EOF >>result_1.txt
    set serveroutput on;
    select '$parm' from dual;
    exec owner.PRC_TESTING_OWNER('$parm');
    EOFThe script is working fine that is i am able to pass to parameter value through unix shell script. :)
    But if I want to pass the value of the owner in cursor , I am unable to pass this value through unix.
    Following the procedure which i am trying to implement.
    create or replace procedure OWNER.PRC_TESTING_OWNER(OWNER IN VARCHAR2) IS
    v_owner varchar2(100) := owner;
    CURSOR main_cur IS 
      select
      i.ROWID  rid ,
      emp_name,
      deptid
      from v_owner.employee;
    CURSOR subset_cur(c_deptid NUMBER ) IS
        SELECT *
          FROM v_owner.DEPT d
          where  d.dept_id=c_deptid;
    --##main loop     
    FOR i IN main_cur LOOP
          FOR j IN subset_cur(i.deptid) LOOP     
    BEGIN
    insert into v_owner.RESULT_TABLE
    END;
    END LOOP;
    END LOOP;How can i pass parameter value of the stored procedure through unix script(that is "owner" in this case), when these parameter value is
    used in cursor? :(
    Can anybody help me regarding the same?
    Thanks in Advance !! :D

    It's not the parameter in the cursor that is the problem, it's that you are trying to use static SQL for something that won't be known until run time (the owner of the table).
    You would need to use something like ...
    declare
       l_owner        varchar2(30) := 'SCOTT';
       l_ref_cursor   sys_refcursor;  
       type l_ename_tab is table of scott.emp.ename%type;
       l_ename_array  l_ename_tab;
    begin
       open l_ref_cursor for
          'select ename
          from ' || l_owner || '.emp';
       loop
          fetch l_ref_cursor bulk collect into l_ename_array limit 10;
          exit when l_ename_array.COUNT = 0;
          for x in 1 .. l_ename_array.count
          loop
             dbms_output.put_line(l_ename_array(x));
          end loop;
       end loop;
       close l_ref_cursor;
    end;
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    SCOTT
    KING
    TURNER
    ADAMS
    JAMES
    FORD
    MILLER
    PL/SQL procedure successfully completed.
    Elapsed: 00:00:00.01

Maybe you are looking for