How to pass a result of SQL query to shell script variable

Hi all,
I am trying to pass the result of a simple SQL query that only returns a single row into the shell script variable ( This particular SQL is being executed from inside the same shell script).
I want to use this value of the variable again in the same shell scirpt by opening another SQL plus session.
I just want to have some values before hand so that I dont have to do multiple joins in the actual SQL to process data.

Here an example :
SQL> select empno,ename,deptno from emp;
     EMPNO ENAME          DEPTNO
      7369 SMITH              20
      7499 ALLEN              30
      7521 WARD               30
      7566 JONES              20
      7654 MARTIN             30
      7698 BLAKE              30
      7782 CLARK              10
      7788 SCOTT              20
      7839 KING               10
      7844 TURNER             30
      7876 ADAMS              20
      7900 JAMES              30
      7902 FORD               20
      7934 MILLER             10
14 rows selected.
SQL> select * from dept;
    DEPTNO DNAME          LOC
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     BOSTON
SQL> exit
Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options
$ cat my_shell.sh
### First query #####
ENAME_DEPTNO=`sqlplus -s scott/tiger << EOF
set pages 0
select ename,deptno from emp where empno=$1;
exit
EOF`
ENAME=`echo $ENAME_DEPTNO | awk '{print $1}'`
DEPTNO=`echo $ENAME_DEPTNO | awk '{print $2}'`
echo "Ename         = "$ENAME
echo "Dept          = "$DEPTNO
### Second query #####
DNAME_LOC=`sqlplus -s scott/tiger << EOF
set pages 0
select dname,loc from dept where deptno=$DEPTNO;
exit
EOF`
DNAME=`echo $DNAME_LOC | awk '{print $1}'`
LOC=`echo $DNAME_LOC | awk '{print $2}'`
echo "Dept Name     = "$DNAME
echo "Dept Location = "$LOC
$ ./my_shell.sh 7902
Ename         = FORD
Dept          = 20
Dept Name     = RESEARCH
Dept Location = DALLAS
$                                                                           

Similar Messages

  • How to achieve this result using sql query?

    hello gurus,
    i have a table like this
    id    name
    1       a
    2       b
    3       c
    4       d
    5       e
    6       f
    7       g
    8       h
    9       i
    10     j
    11     k
    12     l
    13     m now my result should be like this
    id    name  id   name   id   name
    1       a     6       f      11     k
    2       b     7       g     12     l
    3       c     8       h     13     m
    4       d     9       i
    5       e     10      jhow to achieve it by sql query ?
    thanks and regards,
    friend
    Edited by: most wanted!!!! on Feb 22, 2012 5:55 AM

    hi,
    Did you mean this:
    with a as
    (select 1 id ,'a' name from dual
    union all select 2 id ,'b' name from dual
    union all select 3 id ,'c' name from dual
    union all select 4 id ,'d' name from dual
    union all select 5 id ,'e' name from dual
    union all select 6 id ,'f' name from dual
    union all select 7 id ,'g' name from dual
    union all select 8 id ,'h' name from dual
    union all select 9 id ,'i' name from dual
    union all select 10 id ,'j' name from dual
    union all select 11 id ,'k' name from dual
    union all select 12 id ,'l' name from dual
    union all select 13 id ,'m' name from dual
    select
      id_1
      ,name_1
      ,id_2
      ,name_2
      ,id_3
      ,name_3
    from
      select
        id id_1
        ,name name_1
        ,lead(id,5) over (order by id) id_2
        ,lead(name,5) over (order by id) name_2
        ,lead(id,10) over (order by id)  id_3
        ,lead(name,10) over (order by id) name_3
        ,rownum r
      from
        a
    where
      r <=5
    D_1                   NAME_1 ID_2                   NAME_2 ID_3                   NAME_3
    1                      a      6                      f      11                     k     
    2                      b      7                      g      12                     l     
    3                      c      8                      h      13                     m     
    4                      d      9                      i                                   
    5                      e      10                     j Regards,
    Peter

  • How to pass two parameters to sql query

    I try to create a sql script to update two columns in one table. I want to set it as parameter. When people execute this sql script, they need to pass parameter into sql query, then query will be executed successfully. The problem is I am only able to pass one parameter. If set two parameters in one line code, it will get ORA-00933 errors. Please advice me where I was wrong. The code is simple and like this:
    update MY_TABLE set year = &year AND month = &month where application_type = 'xxxx';
    If I only have: update MY_TABLE set year = &year where application_type = 'xxxx'; It works. If I set two parameters to pass value. It will get error.

    Hi,
    When you UPDATE two or more columns in the same statement, use ',' instead of 'AND' to separate them:
    update  MY_TABLE
    set     year = &year
    ,       month = &month
    where   application_type = 'xxxx';The correct syntax for all SQL statements, including UPDATE, can be found in the [SQL Language manual|http://download.oracle.com/docs/cd/B28359_01/server.111/b28286/statements_10008.htm#sthref9598].

  • 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

  • How to pass parameters to a SQL query or Data template

    Hi,
    The requirement is that my final report will take 'date' as input from the user and print only those records which have date later than the user-supplied 'date'. ('Date' is a field in the database).
    I know the way to achieve this is to use user-defined parameters.
    I have read the user guide, and have created user parameters. From the user guide: "The Report Editor allows you to define your query, define
    the parameters that you want users to pass to the query,..."
    However there is no example given. (I could not find).
    Any guidance?

    Are you using Enterprise Server environment ? If so, you can find a sample report in the Shared Folders under HR Manager - Employee Salary Report. Please revert if you still have confusion.

  • How to pass a file with parameters to a shell script from java

    I have a sh script running on linux. This script prompts when started prompts for 4 questions, for which the answers are 1, 1, 1, Y. So now I need to invoke this from a java program and pass the parameters in a file from jave to the script. Can anyone tell me how to code this in java. The script works fine from command line.
    Thanks in advance.

    Here is the standard answer to all questions involving Runtime.exec(), enjoy:
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • How to pass password to an lftp connection using shell script

    Hi
    I need to transfer a file to server which support FTPS protocol. I am using lftp utility for this purpose. User credentials used to establish the connection expires after a period(eg: 45 days/3 months) . Can anyone guide me with me an approach to use the password in the shell script which transfer the file to the remote server other than hard coding the password in the script.
    Thanks
    Ramya

    SSH is a better option but unfortunately it's not always available. If you worry about security, you could use the bookmark lftp feature:
    $ lftp ftp://username@server
    Password:
    lftp username@server:~>  set bmk:save-passwords true
    lftp username@server:~> bookmark add yourserver
    lftp username@server:~> bookmark list
    lftp username@server:~> quit
    $ lftp yourserver &
    $ ps -aux | grep lftp
    The password is stored in ~/.lftp/bookmarks (not encrypted) but you can protect the file with the right permissions as you would do with your certificates with SSH.

  • How to compare result from sql query with data writen in html input tag?

    how to compare result
    from sql query with data
    writen in html input tag?
    I need to compare
    user and password in html form
    with all user and password in database
    how to do this?
    or put the resulr from sql query
    in array
    please help me?

    Hi dejani
    first get the user name and password enter by the user
    using
    String sUsername=request.getParameter("name of the textfield");
    String sPassword=request.getParameter("name of the textfield");
    after executeQuery() statement
    int exist=0;
    while(rs.next())
    String sUserId= rs.getString("username");
    String sPass_wd= rs.getString("password");
    if(sUserId.equals(sUsername) && sPass_wd.equals(sPassword))
    exist=1;
    if(exist==1)
    out.println("user exist");
    else
    out.println("not exist");

  • How to get this output using sql query?

    Hi,
      How to get this output using sql query?
    Sno Name Age ADD Result
    1 Anil 23 delhi Pass
    2 Shruti 25 bangalor Pass
    3 Arun 21 delhi fail
    4 Sonu 23 pune Pass
    5 Roji 26 hydrabad fail
    6 Anil 28 delhi pass
    Output
    Sno Name Age ADD Result
    1 Anil 23 delhi pass
    28 delhi pass

    Hi Vamshi,
    Your query is not pretty clear.
    write the select query using Name = 'ANIL' in where condition and display the ouput using Control-break statements.
    Regards,
    Kannan

  • How to pass runtime parameters to MySQL Query from xMII server

    Please can anybody help in How to pass runtime parameters to Orcle Query from xMII server

    The answer is the same as for your other thread.  The mechanism is the same regardless of the end database.  The SQL  syntax will be different for each database vendor depending upon which functions you are invoking.  The main areas of difference between SQL Server, Oracle, DB2, etc. deal with dates (times also) and strings, but there are others as well.
    Regards,
    Mike

  • How to add a parameter to sql query in report

    Hi
    How to add a parameter to sql query in report.
    Parameter is from Visual studio
    example:
    select * from tab1 where dl=parameter???
    I have VS 2008 prof CR XI R2, mysql

    Hello,
    If you have this API available then you can modify the record selection formulae in code to add filtering:
              string recordSelectionFormula = "{T_INV_RPT_ADDR.IND_PROMUS} = {?P_PROMUS?} AND {T_INV_RPT_POINT.INVOICE_DATE} = DATE(2008, 05, 31) AND {T_INV_RPT_POINT.CHECKOUT_DATE} = date(2008, 04,29)";
                CrystalDecisions.CrystalReports.Engine.ReportDocument.RecordSelectionFormula = recordSelectionFormula;
    You have to format and follow the rules as in the Designer so not too much work to get this to work.
    CR for .NET may not have the ability so you will need to upgrade to a Developer version of Crystal Reports.
    Thank you
    Don

  • How to pass runtime parameters to Oracle Query from xMII server

    Please can anybody  help me that how to pass runtime parameter to Orcle Query  from xMII server.

    It works the same way as I described in this thread [How to pass runtime parameters to MySQL Query from xMII server].  It does not matter the datasource MII will work the same for all queries, at least for passing in parameters.  How to write those queries and their datatypes will be the differences.

  • How to hide repeated details using SQL Query?

    How to hide repeated details using SQL Query?
    For Ex:
    ------------------------+
    DEPTNO| ENAME | JOB |
    ------|-------| --------|
    10 | JAMES | MANAGER |
    10 | BLAKE | CLERK |
    10 | FORD | SALESMAN|
    20 | SCOTT | MANAGER |
    20 | ADAMS | CLERK |
    20 | KING | SALESMAN|
    ------------------------+
    How we can display the above details in the following way?
    ------------------------+
    DEPTNO| ENAME | JOB |
    ------|-------| --------|
    10 | JAMES | MANAGER |
    | BLAKE | CLERK |
    | FORD | SALESMAN|
    20 | SCOTT | MANAGER |
    | ADAMS | CLERK |
    | KING | SALESMAN|
    ------------------------+
    Thanks Advance

    Hi,
    you can use BREAK ON DEPTNO in SQL*Plus or use LAG.
    SQL> ed
    Wrote file afiedt.buf
      1  select nullif(department_id
      2                , lag(department_id) over (partition by department_id order by last_name)
      3         ) dept_id
      4  , last_name, job_id
      5*  from employees where department_id in (30,50) and rownum <=10
    SQL> /
       DEPT_ID LAST_NAME                 JOB_ID
            30 Baida                     PU_CLERK
               Colmenares                PU_CLERK
               Himuro                    PU_CLERK
               Khoo                      PU_CLERK
               Raphaely                  PU_MAN
               Tobias                    PU_CLERK
            50 Fripp                     ST_MAN
               Kaufling                  ST_MAN
               Vollman                   ST_MAN
               Weiss                     ST_MAN
    10 rows selected.

  • How can i pass parameter values from html to a shell script

    Hi Guys...
    I had a requirement where i need to execute a sql statement and print the output in HTML page. This report has parameters to enter. So i created a HTML form which accepts parameters. When the submit button is pressed, the action tag in the form invokes unix shell script file. It will open sqlplus and run the sql script file .sql and print the output in the HTML page.
    sql script contains the query and some set options which prints the output in HTML page. Like "SET MARKUP HTML ON"... The query has some parameters like "select * from emp where empno = &&empnumber. I will use the same name "empnumber" while created the HTML parameter form like " <input type = "text" name="empnumber" size="10" align="left">.
    user sees this parameter form and enters some value in to that empno text box.
    My question is how can i catch these parameter values in a shell script and pass it to the sql script to execute it.
    Help Appreciated
    Thanx

    This is a A Bad Idea (tm). This type of CGI processing is old and were (and still is) full of security holes. Very easy to inject stuff (Unix commands and SQL) into it.. To get those parameters into SQL*Plus requires using Unix shell commands to process it - and something like a backquote allows all kinds of nasty stuff to be injected. The Unix shell was never designed to be used as a secure CGI environment.
    There are far better and far superior alternatives. Perl (with Perl_DBI) and PHP (using Zend Core for Oracle) come to mind as web scripting languages.
    Even easier is using HTMLDB. Very few moving parts. Is free. Supports Oracle 9.2 and 10G.

  • Passing params from SQL file to Shell Script and then from Shell to SQL Fil

    Afternoon guys,
    Have a fun question for all you gurus in shell scripting out there. I have a shell script that is calling 2
    different SQL programs. My objective is to pass a variable called request_number from one sql program
    to the shell script and then from the shell script back to another SQL program. I will explain why I
    need this to happen.
    Here is what the shell script looks like which calls sql programs student_load_a.sql and
    student_load_b.sql. Student_load_a.sql basically creates the control file (.ctl) which is needed for the
    SQL*Loader and then student_load_b.sql reads the table that was just loaded and does the main
    processing. My main objective here is to be passing the request_number which is being generated
    using an Oracle Sequence in student_load_a.sql and using this generated number in my main
    processing in student_load_b.sql to select records from the table based on request_number.
    Any ideas ?Any help or recommendations is welcome and appreciated.
    *1. Shell Script*
    # Accept system input parameters
    p_user_id=$1
    p_job_id=$2
    # Create control files for sqlload
    sqlplus.exe -s $p_user_id @$STUDENT_PATH/student_load_a.sql $p_job_id
    exit_status=$?
    # Do sqlloads
    sdesqlldr.exe userid=$p_user_id control=student_load-$p_job_id.ctl \
                                                 log=student_load-$p_job_id.log \
                                                 bad=student_load-$p_job_id.bad
    exit_status=$?
    # Main processing
    # sqlplus.exe -s $p_user_id @$STUDENT_PATH/student_load_b.sql $p_user_id $p_job_id $p_request_number
    sqlplus.exe -s $p_user_id @$STUDENT_PATH/student_load_b.sql $p_user_id $p_job_id
    exit_status=$?
    exit 0*2. student_load_a.sql (Would like to pass back the Sequence Number back to shell script and then use in student_load_b.sql*
    -- Accept system input parameters
    define p_job_id = &1
    spool student_load-$p_job_id.ctl
    select
    'append into table TMP_STUDENT_LOAD
    FIELDS TERMINATED BY '','' optionally enclosed by ''"''
    trailing nullcols
    (request_number CONSTANT ' || '''' || request_number_seq.nextval || ''',
    student_id)'
    from   dual
    spool off;
    exit 0;
    {code}
    *3. student_load_b.sql (This is a big file so I am only adding code that is relevant for the SQL)*
    {code}
    declare
      v_request_number    number(6);
      v_student_id                  number(7);
      cursor cur_student_load is
        select  student_id
        from   TMP_STUDENT_LOAD
        where  request_number = v_request_number
        order by 1;
    begin
        v_user_id := '&1';
        v_job_id := &2;
        -- This is the variable I would like to be be passing from shell script to student_load_b.sql
        -- v_request_number = '&3';
         open  cur_student_load;
         fetch cur_student_load into v_student_id;
          exit when cur_student_load%notfound;
          .... more logic of if then else in here
         close cur_student_load;
    end;
    {code}
    Edited by: RDonASnowyDay on Jan 29, 2010 4:03 PM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

    How come you are mixing WinDoze script (*.exe) with Unix?
    You are aware that you will be passing the password along with the user id to the second sql script?
    I will assume Unix ksh:
    # Accept system input parameters
    p_user_id=$1
    p_job_id=$2
    # Create control files for sqlload
    p_seqno=`sqlplus -s $p_user_id @$STUDENT_PATH/student_load_a.sql $p_job_id`
    exit_status=$?
    # Do sqlloads
    sqlldr userid=$p_user_id control=student_load-$p_job_id.ctl \
           log=student_load-$p_job_id.log \
           bad=student_load-$p_job_id.bad
    exit_status=$?
    # Main processing
    # sqlplus -s $p_user_id @$STUDENT_PATH/student_load_b.sql $p_user_id $p_job_id $p_request_number
    sqlplus -s $p_user_id @$STUDENT_PATH/student_load_b.sql \
               $p_user_id $p_job_id $p_seqno
    exit_status=$?
    exit 0And the first sql script would look like this:
    -- student_load_a.sql
    -- Accept system input parameters
    set echo off pages 0 feed off lin 80 trims on ver off
    def p_job_id = &1
    col seqno NEW_VALUE seqno
    select request_number_seq.nextval seqno from dual;
    set term off
    spool student_load-$p_job_id.ctl
    select
    'append into table TMP_STUDENT_LOAD
    FIELDS TERMINATED BY '','' optionally enclosed by ''"''
    trailing nullcols
    (request_number CONSTANT ''&&seqno'',
    student_id)'
    from   dual
    spool off;
    exit 0;
    {code}
    :p                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Maybe you are looking for