How to execute sql-queries through shell scripting in linux?

How to execute sql-queries through shell scripting in linux?

http://www.oracle.com/technology/pub/articles/saternos_scripting.html
Two simple examples:
#!/usr/bin/env bash
set_orafra () {
   orafra=`echo 'set heading off
   select name from v$recovery_file_dest;
   exit' | sqlplus -s / as sysdba`
set_orafra
echo $orafra
#!/usr/bin/env bash
export ORACLE_SID=instance_name
export ORACLE_HOME=/path_to_oracle_home_directory
export LD_LIBRARY_PATH=$ORACLE_HOME/lib
export PATH=/$ORACLE_HOME/bin/$PATH
$ORACLE_HOME/bin/sqlplus -s <<EOF
connect scott/tiger@my_instance_name
INSERT INTO table VALUES (sysdate);
exit
EOFEdited by: Markus Waldorf on Sep 17, 2010 12:19 AM

Similar Messages

  • How To Execute SQL Statements From Shell Scripts?

    I need to extecute some SELECT statements from a shell scripts. Could anybody kindly tell me how to do that, or which document i should refer to ? Thank you very much!

    You can execute SQLPlus with the SQL in a file (sqlplus -s @<sql-script>).

  • How to execute SQL queris in BPEL using JDeveloper?????..Please suggest

    Hi,
    I am very new to JDeveloper. Curently i am tryin to execute an SQL query from the BPEL process, there is an query in the source field and the output of the query is to be mapped to a variable field in the target xsd file. the query is fairly simple, like
    SELECT emp
    FROM emp_table
    WHERE emp_id=123
    The target field, namely "employee", is an element from the xsd file. I tried using Java embedding activity to connect to the db and execute the query through a piece of Java code, but couldn't find a way to assign the output of the query to the associated target field. however lately i also discovered the Database Adapter services which helps me to create a database connection, but still i am not sure as of how to handle and map the output of the query to the variable field. Is there any other way to execute database queries in Jdeveloper using BPEL process????????? Please suggest.
    Can somebody please help me in resolving the issue either through Java Embed activity or Database Adapter services??
    Thanks in advance
    Prabha

    http://download.oracle.com/docs/cd/B31017_01/integrate.1013/b28994/adptr_db.htm
    maybe this one is helpful for you.
    best is to use the db adapter for this.
    if you just execute it you will see the format of the output in your audittrial (so some kind of list of employees). In bpel when you doubleclick the invoke-activity you will see the outputvariable.
    With use of xpath you coud loop over this 'list' (collection/array/etc) of employees and do with it whatever you want.
    Too see how you need to loop over the array you could check this :
    http://clemensblog.blogspot.com/2006/03/bpel-looping-over-arrays-collections.html

  • How to Call Multiple Sessions through Shell scripting

    Dear Members,
    I have a scenario where I load data into target table from 20 different source tables.
    For this I have written three procedures where there exists an bulk insert executed on a same table in every procedure. Now I have to call these procedures simultaneously so that
    all three bulk loads are executed at once in different sessions.
    For this I have to write a shell script which opens 3 different sessions and execute all these packages at once.
    Can you guide me through.

    When you call sqlplus in your shell script, put a & at the end so that it runs the SQL scripts in the background. For instance:
    #!/bin/bash
    ...oracle home stuff
    $ORACLE_HOME/bin/sqlplus user/pass @script.sql &
    $ORACLE_HOME/bin/sqlplus user/pass @script2.sql &
    $ORACLE_HOME/bin/sqlplus user/pass @script3.sql &

  • Calling sql through shell script

    How do i call a sql file through shell script in unix ?

    Rahul India wrote:
    export ORACLE_HOME=$ORACLE_BASE/product/10g
    cd $ORACLE_HOME
    set `sqlplus -s <SCHEMA_NAME>/<SCHEMA_PASSWORD> <<EOF
    @r_100.sql
    exit;   
    EOF`THIS SCRIPT IS OK?You'll also need to set ORACLE_SID
    And I don't know why everyone is putting the reference to sqlplus inside a "set" valuation, nor why they are putting the reference to the script in a redirected input stream. All you need is
    sqlplus -s user/password  @r_100.sqlAnd to insure the invironment is correctly set:
    export ORACLE_SID=whatever
    export ORAENV_ASK=NO
    . oraenv
    unset ORAENV_ASK=NO
    sqlplus -s user/password  @r_100.sql

  • How to execute sql scripts from Powershell across multiple databases

    Re: How to execute sql scripts from Powershell across multiple databases
    I have an tsql script that I want to run across a list of databases. How is the best way to do this in Powershell? Thanks.

    My example below, using just the SMO and not breaking up the batches, the ExecuteWithResults give the following error when the .sql file contains a GO. My script files are as simple as a DECLARE and then a GO.
    WARNING: SQL Script Failed
    The object of type "Microsoft.PowerShell.Commands.Internal.Format.FormatStartData" is not valid or not in the correct sequence. This is likely caused by a user-specified "format-list" comm
    and which is conflicting with the default formatting.
        + CategoryInfo          : InvalidData: (:) [out-lineoutput], InvalidOperationException
        + FullyQualifiedErrorId : ConsoleLineOutputOutOfSequencePacket,Microsoft.PowerShell.Commands.OutLineOutputCommand
    Also, when executing from the ISE, is there a way to force the ISE to release the files. I am having to close the ISE and reopen my script every time I want to make a testing change to the .sql file.
    [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
    $scriptspath = "C:\temp\psExecuteSQLScripts\scripts"
    $SQLServer = "fidevc10"
    $SQLDB = "Bank03"
    # Create SMO Server Object
    $Server = New-Object ('Microsoft.SQLServer.Management.Smo.Server') $SQLServer
    # Get SMO DB Object
    $db = $Server.Databases[$SQLDB]
    # Load All SQL Scripts in Directory
    $scripts = Get-ChildItem -Path (Join-Path $scriptspath "*") -Include "*.sql" -Recurse
    # Loop through each script and execute
    foreach ($SQLScript in $scripts)
    $fullpath = $SQLScript.FullName
    # Read the Script File into Powershell Memory
    $reader = New-Object System.IO.StreamReader($fullpath)
    $script = $reader.ReadToEnd()
    # Execute SQL
    Write-Host "Executing $SQLScript on $SQLDB...."
    try
    $ds = $db.ExecuteWithResults($script)
    Foreach ($t in $ds.Tables)
    Foreach ($r in $t.Rows)
    Foreach ($c in $t.Columns)
    Write-Host $c.ColumnName "=" $r.Item($c)
    Write-Host "Complete"
    catch [Exception]
    Write-Warning "SQL Script Failed"
    echo $_.Exception|format-list -force
    Write-Host " " -BackgroundColor DarkCyan

  • How to prepare for Converting UNIX shell scripts to PL/SQL

    Hi All
    I was said, that i may have to convert a lot of unix shell script to PL/SQL, what are the concepts i need to know to do it efficently,
    what are the options PL/SQL is having to best do that.
    I know the question is little unclear, but I too dont have much inputs about that i'm sorry for that, just its a question of how
    to prepare myself to do it the best way. What are the concepts i have to be familiar with.
    Many Thanks
    MJ

    Just how much work is involved, is hard to say. Many years ago I also wrote (more than once) a complete ETL system using a combination of shell scripts, SQL*Plus and PL/SQL.
    If the PL/SQL code is fairly clean, uses bind variables and not substitution variables, then it should be relatively easy to convert that PL/SQL code in the script to a formal stored procedure in the database.
    There is however bits and pieces that will be difficult to move into the PL/SQL layer as it requires new software - like for example FTP'ing a file from the production server to the ETL server. This can be done using external o/s calls from within PL/SQL. Or, you can install a FTP API library in PL/SQL and FTP that file directly into a CLOB and parse and process the CLOB.
    Think of Oracle as an o/s in its own right. In Oracle we have a mail client, a web browser, IPC methods like pipes and messages queues, cron, file systems, web servers and services, etc. And PL/SQL is the "shell scripting" (times a thousand) language of this Oracle o/s .
    In some cases you will find it fairly easy to map a Unix o/s feature or command to one in Oracle. For example, a Unix wget to fetch a HTML CSV file can easily be replaced in Oracle using a UTL_HTTP call.
    On the other hand, techniques used in Unix like creating a pipe to process data, grep for certain stuff and awk certain tokens for sed to process further... in Oracle this will look and work a lot different and use SQL.

  • How to use database control to execute sql queries which change at run time

    Hi all,
    I need to execute sql queries using database controls , where the sql changes
    at run time
    based on some condition. For eg. based on the condition , I can add some where
    condition.
    Eg. sql = select id,name from emp where id = ?.
    based on some condition , I can add the following condition .
    and location = ?.
    Have anybody had this kind of situation.
    thanks,
    sathish

    From the perspective of the database control, you've got two options:
    1) use the sql: keyword to do parameter substitution. Your observation
    about {foo} style sbustitution is correct -- this is like using a
    PreparedStatement. To do substitution into the rest of the SQL
    statement, you can use the {sql: foo} substitution syntax which was
    undocumented in GA but is documented in SP2. Then, you can build up
    the filter clause String yourself in a JPF / JWS / etc and pass it into
    the DB control.
    For example:
    * @jc:sql statement="select * from product {sql: filter}"
    public Product[] getProducts(String filter) throws SQLException;
    This will substitute the String filter directly into the statement that
    is executed. The filter string could be null, "", "WHERE ID=12345", etc.
    2) you can use the DatabaseFilter object to build up a set of custom
    sorts and filters and pass that object into the DB control method.
    There have been other posts here about doing this, look for the subject
    "DatabaseFilter example".
    Hope that helps...
    Eddie
    Dan Hayes wrote:
    "Sathish Venkatesan" <[email protected]> wrote:
    Hi Maruthi,
    The parameter substituion , I guess is used like setting the values for
    prepared
    statements.
    What I'm trying to do , is change the sql at run time based on some condition.
    For example ,
    consider the following query :
    select col1,col2 from table t where t.col3 > 1
    At run time , based on some condition , I need to add one more and condition.
    i.e. select col1,col2 from table t where t.col3 > 1 and t.col4 < 10.
    This MAY not address your issue but if you are trying to add "optional" parameters
    you may try including ALL the possible parameters in the SQL but send in null
    for those params that you don't want to filter on in any particular case. Then,
    if you word your query
    as follows:
    select col1, col2 from table t where t.col3 > 1 and (t.col4 = {col4param} or
    {col4param} is null) and (t.col5 = {col5param} or {col5param} is null) ...
    you will get "dynamic" filters. In other words, col4 and col5 will only be
    filtered if you send in non-null parameters for those arguments.
    I have not tried this in a WL Workshop database control but I've used
    this strategy dozens of times in stored procedures or jdbc prepared statements.
    Good luck,
    Dan

  • Pl/sql procedure with shell script

    Hi Guys,
    I will be updating some of the columns in the database thru SQL UPDATE stament. I want to make this process automatic. I.e instead of running manually this uodate process, i want to write a unix script which run on cron job. In the update stament I have to compare date like e_create_date > to_date (........, 'yymmdd') and date should be 2 days previous then to date and I would ike to create the spool file which I would like to send through mail.the script will run automatically.
    I am confused how to write shell script & sql procedure and how to call it inside the shell script. How can this be done.
    Help Appreciated.
    Thanks
    sonu

    save the Store procedure as a create_SP.sql in OS
    save another testrun.sql
    as follows
    begin
    sp_test('&p_test');
    end;
    If you are on a Unix or Solaris OS then
    open a Shell Script as follows :
    and name as test.sh or test.ksh
    var='SYSTEM'
    export var
    sqlplus username/password
    @create_SP.sql
    -- this created a store procedure on the database
    @testrun.sql $var
    This will execute the SP with parameter from OS Variable.

  • Advice executing oracle package using shell script.

    Hi,
    I am writing one shell script that accepts several parameters as an input. These parameters are then passed while executing Oracle package procedure. However this procedure has an OUT parameter and I am not aware of how to receive that parameter in shell script.
    Any advice /suggestion is really appreciated.
    Below is the part of code I am using ... (P_OUT is suppose to be an out parameter for package)
    sqlplus -s apps/apps <<endsql
    whenever sqlerror exit failure
    set serveroutput on size 10000
    execute TEST_PKG.GET_COC_REQ ('$P_FROM_DATE','$P_TO_DATE','$P_MONTH_YY', '$P_OUT');
    endsql

    There is more than one way. Here an example :
    SQL> select ename,sal from emp where empno=7902;
    ENAME             SAL
    FORD             3000
    SQL> create or replace procedure extsal (
      2     empnum  in      number,
      3     salout  out     number)
      4  is
      5  begin
      6     select sal into salout
      7     from emp
      8     where empno = empnum;
      9* end;
    SQL> /
    Procedure created.
    SQL> exit
    Disconnected from Oracle9i Enterprise Edition Release 9.2.0.4.0 - Production
    With the Partitioning, OLAP and Oracle Data Mining options
    JServer Release 9.2.0.4.0 - Production
    $ EMP=7902
    $ SAL=`sqlplus -s scott/tiger << EOF
    set head off
    var salout number;
    exec extsal ($EMP, :salout);
    print salout
    exit
    EOF
    `$ echo $SAL | awk -F. '{print $2}'
    3000
    $

  • How to execute multiple queries in one stored procedure.

    Hi,
    I am Kumar,
    How to execute multiple queries in one stored procedure.
    here is the my requirements,
    1. get the max value from one table and sum of the that value.
    2. insert the values and also sum of the max value.
    using stored procedure
    I am using SQL server 2000 database.
    Please help me.
    Advance thanks
    by,
    Kumar

    This is not a java question and it is not even a problem: your only problem is
    1) lack of knowledge
    2) lack of interest to find a manual
    But you are going to have to change both by actually reading a book or a manual that explains the stored procedure language of SQL Server. It is the same as Sybase I think, so you could also look for a manual for that DBMS.

  • SQL Query in Shell script

    Hi everyone,
    I am facing a difficulty in writing a query for a table REF_PRODUCT having some columns like OFFER, PROVIDERID.
    The problem statement is
    if the OFFER not in ('A','B','C') then x='D'
    if x is empty then
    if providerid in (some values) then x='A'
    else if providerid in (some other values) then x='B'
    else x='C'
    where x is a variable in shell script.
    I want to execute the above in shell script. how to manage this?
    Please help..
    Regards
    Abhi

    user8744860 wrote:
    I am facing a difficulty . . . Etc . . .
    I want to execute the above in shell script. how to manage this?
    Please help..
    1) What you posted is not a shell script.
    2) In order for us to help you with your question, you need to post a working test case: create table and insert statements
    as well as the expected result from that data. Provide also an explanation of the rules that lead to this result.
    3) Also provide any code you have written.
    :p

  • PL/SQL function - unix shell script

    Hello,
    I have an application that calls a unix shell script This unix shell script calls a oracle function in the PL/SQL package. For example,
    In package pkg_test, there is function f_test. This function returns pls_interger.
    How can I write a unix shell script to return the value from the oracle function, so that the application can get the value from the unix shell script?
    Many thanks.

    Thank you for your response.
    The Oracle functuion pkg_name.proc_name will return different values, 0, 1, 2, 3 in diffefrent conditions.
    The unix script, myscript.ksh calls pkg_name.proc_name.
    The other application will call myscript.ksh and this application will need to get the values 0, 1, 2, 3 from myscript.ksh.
    Can we make it without using the unix shelll function?
    Thanks.
    Edited by: slsam01 on Jun 2, 2009 9:21 PM

  • Reading contents of asm through shell script

    How to read contents of asm through shell script?
    Please paste one small example also.
    Thanks
    Bala

    Hello Bala,
    The ASM contents is not visible from the operating system or its utilities. Only visible to database instances, RMAN, and other Oracle-supplied tools.
    Please see http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/glossary.htm#sthref4105
    Regards,
    Rodrigo Mufalani
    http://mufalani.blogspot.com

  • How to place tif file through a script in an indesign document?

    how to place tif file through a script in an indesign document?

    Emanuele:
    it works if i just run a script....
    but since i have been using a gui to do some functions, this  place command is not able to place the output tif file back into the  document

Maybe you are looking for

  • How to add 100 line item in sales order at one time

    Hello Guru's My requirement is to add 100 line items in sales order at once. what can be good approach to overcome this situation.Is there some kind of tool which can be useful? Let me know your suggestions Thanks a lot in Advance

  • OB52-Posting related Problem

    Hi Gurus, Can any one tell me how to do the setting in OB52 . Because my company code is PT00 but i am not finding any link related to this in OB52. And also dont know how to select varriant and date etc.. Please give your valuable input to resolve t

  • Siebel Marketing ESD not sending Email

    I have installed Siebel Marketing and configured it to send email through launching campaigns. Without intervention of ESD, my emails through F9 (Menu bar->File->Send Email) goes fine but when i send it through a campaign it is not received at the ot

  • 10.5.7 breaks CheckPoint SecureClient VPN

    After upgrading to 10.5.7, my CheckPoint VPN client stopped working. I tried uninstalling and reinstalling the VPN client, however, re-installation does not work either. Has anyone had a similar experience or a solution?

  • Spry Elements

    Is it possible to combine several different Spry elements on a single page, specifically a Spry menu and a Spry collapsible panel(s)?