Passing UNIX command in SQL Script

Hi,
I am writing a sql-script in unix.
The logic is as below:
SELECT flag FROM dummy_table;
If flag = Y Then
cp ./abc.txt $HOME/abc.txt
Else
SELECT id, roll FROM employees;
Please help me out.
Thanks and Regards,
Tony

I did that way:
Under SYS create the following procedure:
CREATE OR REPLACE PROCEDURE SYS.LINUX_RUN (PATH     IN     VARCHAR2,
                                           result      OUT VARCHAR2)
IS
   script_file   VARCHAR2 (40) := 'my-temp-script.sh';
   script_data   VARCHAR2 (4000);
   MyFile        UTL_FILE.file_type;
   d             VARCHAR2 (4000);
   dump_file     VARCHAR2 (40) := '/tmp/my-temp-file.dat';
   dump_type     UTL_FILE.file_type;
BEGIN
   pms.post_calls;
   -- Open file
   MyFile := UTL_FILE.fopen ('TMP', script_file, 'w');
   -- Write data to file
   script_data := PATH || ' > ' || dump_file;
   UTL_FILE.put_line (MyFile, script_data, FALSE);
   -- Close file
   UTL_FILE.fflush (MyFile);
   UTL_FILE.fclose (MyFile);
   -- Purge old logs, no fun anyway
   DBMS_SCHEDULER.purge_log (JOB_NAME => 'TEST');
   -- Execute script
   -- The job is created as disabled as
   -- we execute it manually and will
   -- drop itself once executed.
   DBMS_SCHEDULER.create_job (job_name              => 'TEST',
                              job_type              => 'EXECUTABLE',
                              job_action            => '/bin/sh',
                              number_of_arguments   => 1,
                              start_date            => SYSTIMESTAMP,
                              enabled               => FALSE);
   DBMS_SCHEDULER.set_job_argument_value ('TEST', 1, '/tmp/' || script_file);
   DBMS_SCHEDULER.enable ('TEST');
   -- Wait for the job to be executed
   -- usually done within 1 second but
   -- I set it to 2 just in case.
   DBMS_LOCK.sleep (2);
   -- Open the output file and
   -- print the result.
   dump_type := UTL_FILE.fopen ('TMP', dump_file, 'r');
   UTL_FILE.get_line (dump_type, d);
   result := d;
   LOOP
      BEGIN
         UTL_FILE.get_line (dump_type, d);
         DBMS_OUTPUT.put_line (d);
      EXCEPTION
         WHEN OTHERS
         THEN
            EXIT;
      END;
   END LOOP;
   UTL_FILE.fclose (dump_type);
   -- Clean up our temp files
   UTL_FILE.fremove ('TMP', script_file);
   UTL_FILE.fremove ('TMP', dump_file);
END;
/And grant execute on this procedure to end user.
Then under granted user you can use this procedure in your PL/SQL code.
It works just fine for me :)
Good luck.

Similar Messages

  • Call unix command in sqlplus script

    Hello, I wanna to know how to call a unix command in the sqlplus script.
    For exemple,
    I've a sqlplus script to lauch a oracle report, after the report is generated, I wanne to replace the output file in an other directory.
    So that, I have to call the unix command mv here in the sqlplus script
    How can I do it?**
    I completed my situation:
    I don't have dbms_scheduler untility in my database.
    Thanks a lot for your help
    Edited by: user11930885 on 17 janv. 2010 14:53

    Yes, at the begining, I'll tried the unix shell by calling SQL.
    I've the problem of passing the parameters through.
    It seems to me
    we can use
    Host in the SQLPLUS to run the unix command.
    but I've written it in my sqlplus script, it doesn't work. so I wanna to find an exemple how to use HOST in sqlplus script?
    I would have loved to give you an example..but its not unix on my laptop.
    But what i can tell you is...u should be doing otherway round.
    Not calling Unix commands from SQL but calling SQL's form unix.
    You got shell scripts for that.
    Write a shell script. Login to SQL execute your code.
    exit from sql and then move your file with MV. That's it.
    Do post how far you get after trying this.

  • Passing variables to a SQL script within GC

    Does anyone know if it is possible to pass in a variable to a SQL script job within Grid Control? I don't see that I can but I wanted to ask a larger group before cloing the loop on this.
    For example, I have a few different databases which contain schemas which contain date-range daily partitioned tables. I have a single code block which normally accepts the table_owner as a variable then loops through to analyze the current day partition. I'd love for OEM to be able to have the job submitted to different database targets whilst passing in a different schema name.
    Thank you in advance!

    This can be done in two steps, but first create a table containing 1 field in all the target databases (something like table_owner) you want to analyze. Before running the analyze script change in all the to be analyzed databases the table_owner (as a job?). Then run the analyze script that contains a statement that first reads the table_owner and use this table_owner as the variable you want.
    Even more simple would be to use a database link from the to be analyzed db's to a central db so you only have to change the table_owner in one table.
    Eric

  • Passing Multiple Parameters to SQL Script from a Shell Script

    Hi Friends,
    I have SQL script which accepts 6 parameters.
    I am calling this from a shell script as shown below:
    sqlplus -s  ${ORACLE_ID} @${SQLPATH}KORONT_041.sql ${USER_ID} ${PDC} ${item_number} ${KORDC} ${PDCSET} ${last_Updated_in_hours} Out of the six parameters, item_number is not a mandatory parameter.
    When i pass all six parameters, there is no issue.
    But when i leave item_number blank, i am getting the below error
    Enter value for 6:
    User requested Interrupt or EOF detected.Based on the error, it seems that the NULL values for item_number is ignored and SQL*PLUS is waiting for one more input from user.
    How can i overcome this issue?
    Regards,
    Sreekanth

    Hi,
    I am calling the shell script from concurrent program and below is the log file of the concurrent program.
    Copyright (c) 1979, 1999, Oracle Corporation. All rights reserved.
    KORONT_041_SH module: KORONT - Daily Item Master Update
    +---------------------------------------------------------------------------+
    Current system time is 29-JUN-2011 10:09:35
    +---------------------------------------------------------------------------+
    REQUEST_ID: 68510795
    USER_ID: 4219
    PDC:    85
    Item Number:
    KORDC:    124
    PDCSET:   1100000003
    last_updated_in_hours: 24
    EMAIL_ID: [email protected],[email protected]
    SQLPATH: /e381/oracle/asodev01appl/custom/motont/1.0.0/sql/
    RPTDIR:  /e381/oracle/asodev01comn/admin/out/ASODEV01_asoprdb2
    RPTFILE: o68510795.out
    Table truncated.
    *Enter value for 6: User requested Interrupt or EOF detected.*
    Table truncated.
    old  15:       AND ic.organization_id   = &&4
    new  15:       AND ic.organization_id   = 1100000003
    0 rows created.
    Input truncated to 9 characters
    old   8:                AND organization_id   = &&4
    new   8:                AND organization_id   = 1100000003
    0 rows created.
    End of SQL
    No record.
    +---------------------------------------------------------------------------+
    Executing request completion options...
    +------------- 1) PRINT   -------------+
    Printing output file.
                   Request ID : 68510795      
             Number of copies : 0      
                      Printer : noprint
    +--------------------------------------+
    Finished executing request completion options.
    +---------------------------------------------------------------------------+
    Concurrent request completed successfully
    Current system time is 29-JUN-2011 10:09:38
    +---------------------------------------------------------------------------+Regards,
    Sreekanth

  • Passing Unix variable into sql

    Hi,
    I have a shell script with embedded SQL.
    In the script I have a Unix variable that has a char value
    id="P_test".
    I need to pass this variable to a select in a where condition like
    select * from table where camp1=$id.
    The problem is $id returns P_test rather than 'P_test' so that the select can work.
    Any ideea how to change the clause where to make the select work?
    I could also change the variable id as to have id=" 'P_test' " , but this variable comes from a file parsed and I don't know how to get 'P_test' instead of P_test.
    Thanks

    another option may be like this example -
    $OH10/bin/sqlplus -s > $CIGDEM_LOG_DIR/step1_$CIGDEM_DATE.log 2> $CIGDEM_LOG_DIR/step1_$CIGDEM_DATE.err <<EOF
    $CIGDEM_USERNAME1/$CIGDEM_PASSWORD1@$OTNS10
    @$CIGDEM_DIR/step1.sql $CIGDEM_EXP_DIR $CIGDEM_USERNAME3 $CIGDEM_PASSWORD3 $PERMANANT_TABLESPACE $TEMPORARY_TABLESPACE
    exit ;
    EOF
    "@$CIGDEM_DIR/step1.sql $CIGDEM_EXP_DIR $CIGDEM_USERNAME3 $CIGDEM_PASSWORD3 $PERMANANT_TABLESPACE $TEMPORARY_TABLESPACE" here we call step1.sql with arguments, they will be used with &n symbol inside sql statement -
    .. CREATE USER &2 IDENTIFIED by &3 TEMPORARY TABLESPACE &5 DEFAULT TABLESPACE &4 ; ..http://tonguc.wordpress.com/2007/11/29/linux-and-shell-scripting-part-2/

  • Passing parameters to SQL Script

    I'm trying to pass a parameter to SQL Script like
    sqlplus hr/my_password @myscript.sql King
    but everytime sqlplus is started the system is asking me for this parameter.
    I would like too use this principe in combination with forms. (host user/passwrd@test @test.sql King)
    kind regards,
    Menk

    Your variable name in SQL script must be called &1.
    Eg. SELECT value FROM table WHERE name = '&1.' ;
    So, if you have more than one argument, the followed must be &2. &3. ...
    If you use another name, Oracle always requests the value.
    I hope help you

  • Buckup SQL Script doesn't work

    Hi  all,
    In ECC i want schedule ASE jobs to  dump transaction logs,i do it fellow the note 1588316 step by step .  In  schedule jobs my sql script doesn't work,but it works in sql command line.
    sql script :
    dump transaction DEV using config= 'DEVLOG'
    Anybody can give me some advise to face this issue?  Thanks in advance.
    David Lv

    Hi  all,
    My problem sloved, it's JSagent problem.
    sp_dumptrans not working at dbacockpit @Alcino Melo
    use sybmgmtdb
    go
    sp_js_wakeup "start_js",1
    go
    after start  JSagent  my sql script can run 
    Thanks  all
    David Lv

  • How to call a HP-UX command or shell script from Forms 4.5

    Does anybody know how to call a unix command or shell script to get a files list of HP-UX server from Oracle Forms 4.5 on client side? I tried to use DBMS_PIPE package to get it done but I failed. Please let me have the solution if anybody knows how. Very urgent!

    I tried the host command before and it just let me shell to the DOS environment but not HP-UX environment as Forms was running on Windows platform. So, I could not run a unix command or a shell script. Is DBMS_PIPE the only way to get it done?

  • Script fails when passing values from pl/sql to unix variable

    Script fails when passing values from pl/sql to unix variable
    Dear All,
    I am Automating STATSPACK reporting by modifying the sprepins.sql script.
    Using DBMS_JOB I take the snap of the database and at the end of the day the cron job creates the statspack report and emails it to me.
    I am storing the snapshot ids in the database and when running the report picking up the recent ids(begin snap and end snap).
    From the sprepins.sql script
    variable bid number;
    variable eid number;
    begin
    select begin_snap into :bid from db_snap;
    select end_snap into :eid from db_snap;
    end;
    This fails with the following error:
    DB Name DB Id Instance Inst Num Release Cluster Host
    RDMDEVL 3576140228 RDMDEVL 1 9.2.0.4.0 NO ibm-rdm
    :ela := ;
    ERROR at line 4:
    ORA-06550: line 4, column 17:
    PLS-00103: Encountered the symbol ";" when expecting one of the following:
    ( - + case mod new not null &lt;an identifier&gt;
    &lt;a double-quoted delimited-identifier&gt; &lt;a bind variable&gt; avg
    count current exists max min prior sql stddev sum variance
    execute forall merge time timestamp interval date
    &lt;a string literal with character set specification&gt;
    &lt;a number&gt; &lt;a single-quoted SQL string&gt; pipe
    The symbol "null" was substituted for ";" to continue.
    ORA-06550: line 6, column 16:
    PLS-00103: Encountered the symbol ";" when expecting one of the following:
    ( - + case mod new not null &lt;an identifier&gt;
    &lt;a double-quoted delimited-identifier&gt; &lt;a bind variable&gt; avg
    count current exists max min prior sql stddev su
    But when I change the select statements below the report runs successfully.
    variable bid number;
    variable eid number;
    begin
    select '46' into :bid from db_snap;
    select '47' into :eid from db_snap;
    end;
    Even changing the select statements to:
    select TO_CHAR(begin_snap) into :bid from db_snap;
    select TO_CHAR(end_snap) into :eid from db_snap;
    Does not help.
    Please Help.
    TIA,
    Nischal

    Hi,
    could it be the begin_ and end_ Colums of your query?
    Seems SQL*PLUS hs parsing problems?
    try to fetch another column from that table
    and see if the error raises again.
    Karl

  • Unix command into a pl/sql script

    Could you tell me how to run a unix command (cp, rm, mkdir....)
    into a pl/sql script ?

    sigh this is an old chestnut. it can only be done using a java stored procedure. the instructions for doing this can be found in old postings on this forum - use the Search facility. alternatively try the AskTom site, and Metalink posted a solution as well.
    rgds, APC

  • How to pass Unix environment variable to a SQL procedure or SQL * Plus

    Can any body suggest me how to ,
    How to pass Unix environment variable to a SQL procedure or SQL * Plus file..
    I am trying to invoke a SQL Procedure from Unix
    by passing the value of a Unix environment variable.
    Is it possible..?
    Thanks in advance.
    Regards,
    Srinivas Jaltaru

    Within your shell script you can use what is known as a "here document" which is basically a way of wrapping a call to Oracle. The following call to Oracle loops and writes rows to files with numerically increasing file names. Two unix shell variables are used, one in a select statement and one in a spool command :
    <pre>
    #!/bin/bash
    export ORACLE_SID=DEV05
    FILENO=1007351
    while [ ${FILENO} -le 1008400 ]
    do
    FILENAME=farm_${FILENO}.txt
    DUMMY=`sqlplus -s user20/user20 <<SQLSTOP
    set lines 73
    set pages 0
    set head off
    set termout off
    set echo off
    set feedback off
    select rpad(searchx, 8)
    from blastx@PRODUCTION
    where searchx = ${FILENO} ### here's a shell variable
    spool /export/home/user20/sql/psiblast/BACKUP2_D/${FILENAME} ### here's a shell variable
    spool off
    SQLSTOP`
    FILENO=`expr ${FILENO} + 1`
    done
    exit 0
    </pre>

  • Sql script in UNIX

    When I run a sql script in unix using sqlplus command, the process halts at some point. No matter how long I wait, it does not finish (it does not bring the command prompt back). There are no problems in SQL*PLUS. This process usually takes about 30 mins when I run from SQL * PLUS. Any ideas why it hangs in UNIX?
    sqlplus -s userid @script
    Thanks

    Are you specifying a password ?
    Have you set the ORACLE_SID ?

  • Unix command using in pl/sql

    Hi all,
    Can we use unix command in pl/sql statement..
    Regards,
    Manikandan.k

    Hi Manikandan,
    You cantry this via Oracle Database Jobs. You can create a Job that uses your Shell Script & then execute the job in a procedure / function. It's one of the ways to access the underlying Operating System via PL/SQL.
    Regards,
    Sandeep

  • SQL script with host command job in Enterprise Manger Grid Control

    I use Enterprise Manger Grid Control 10.2.0.5 and need to create SQL script job on database instance target on unix/linux platform.
    I have problem with os command inside sqlplus script.
    For example for the simple command: SQL> host ls
    I get the message in output log: SQL> SQL> SQL> SQL> SQL> SQL> /bin/bash: ls: command not found
    Can anyone help me?
    Thank you.

    Hi,
    Make sure you have granted all necessary rights (log on as a batch job etc.) to the user used in the prefered credentials.
    Cheers,
    Kenneth

  • Excute Unix command Using PL SQL

    Greeting,
    how can I execute shell unix commands from PL SQL and without passing by the Java virtual machine, because my database server is not very performant to support the heaviness of Java.
    Thank you for ur help!!!

    Is this what you need ?
    TEST@db102 SQL> create table test(dir varchar2(100), url varchar2(200));
    Table created.
    TEST@db102 SQL> insert into test values('/tmp','http://otn.oracle.com');
    1 row created.
    TEST@db102 SQL> commit;
    Commit complete.
    TEST@db102 SQL> exit
    Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
    With the Partitioning, OLAP and Data Mining options
    [ora102 work db102]$ cat /tmp/wget.sh
    #!/bin/bash
    rm -f /tmp/index.html
    /usr/bin/wget -P $1 $2
    [ora102 work db102]$ sqlplus test/test
    SQL*Plus: Release 10.2.0.1.0 - Production on Sat Jul 29 17:11:28 2006
    Copyright (c) 1982, 2005, Oracle.  All rights reserved.
    Connected to:
    Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
    With the Partitioning, OLAP and Data Mining options
    TEST@db102 SQL> DECLARE
      2     v_dir   varchar2(100);
      3     v_url   varchar2(200);
      4  BEGIN
      5     select dir, url into v_dir, v_url
      6     from test;
      7     DBMS_SCHEDULER.CREATE_JOB(
      8     job_name=>'testjob',
      9     job_type=>'EXECUTABLE',
    10     job_action=>'/tmp/wget.sh',
    11     number_of_arguments => 2,
    12     enabled => FALSE);
    13     dbms_scheduler.set_job_argument_value('testjob',1,v_dir);
    14     dbms_scheduler.set_job_argument_value('testjob',2,v_url);
    15     dbms_scheduler.enable('testjob');
    16* end;
    TEST@db102 SQL> /
    PL/SQL procedure successfully completed.
    TEST@db102 SQL>sorry for lost indentations, but tags do not help us these days....

Maybe you are looking for

  • Month view, week view and working week view in outlook calendar in wpf

    Hello everybody! i was posted http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/7d927ca0-a110-4ede-bb2c-fa0070625722/ here how to make Outlook calendar sheet view , and tried http://www.codeproject.com/Articles/30881/Creating-an-Outlook-Calend

  • Selecting a filename in Bridge CS4

    Hi there, Is it possible to copy-paste only a filename in Bridge CS4? I use a website to sell things I make and part of this requires me to upload photographs of these things to my shop on the website. When I use the image uploader on this website, I

  • No IP Address

    I've never had any issues with my macbook and connecting to our wireless internet until yesterday. As of last night, my macbook and ipod touch will not connect to my wireless internet at home. All of our other computers work just fine. I installed Sn

  • IDoc - XI (BPM) - File, Successful status but no message generated

    Hi, I am trying to do Sravya's blog for BPM correlation. I am sending IDocs from R/3 system to XI, even though message shows successful status there is no message is created on the receiver. One strange thing is when I go into the SXI_MONITOR -> proc

  • Creating a XMLfile using pl/sql procedure.

    Hi, I have a scenario here. I have to create a XML file, in a perticular format. Since its not a well formed XML format, I am facing issues while creating the file. The XML file format is as below. <SECTION>10-B</SECTION> <?xml version="1.0" encoding