Guidance needed for a typical shell script with sql query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Guidance needed for a typical shell script with sql query
# 15  
Old 10-17-2011
It seems correct, isn't it? And what happens when you execute this command in sqlplus?

Code:
select
   *
 from
   bus_event
 where
   bus_event_seq_nbr in ( '3969495','3969503','3969511','3969491','3969499' );

Could also please post the content of the output file (the error message)?
# 16  
Old 10-17-2011
[QUOTE=radoulov;302565161]It seems correct, isn't it? And what happens when you execute this command in sqlplus?
I have redirected the output to a file called output.txt
but the content of output did not contain the expected result.

Code:
set -xv
{
 printf 'set pages 0 feed off ver off head off echo off\n'
 printf 'select
   *
 from
   bus_event
 where
   bus_event_seq_nbr in ( %s );\n' $(
     printf "'%s'\n" $(<./seqnbr.txt) |
       paste -sd, -
       )
   }
set +xv 
sqlplus -s "$db_user"/"$db_pwd"@"$db_sid" > ./output.txt

[QUOTE]
The output file content is like :
Code:
mingle% more output.txt
SQL*Plus: Release 10.2.0.4.0 - Production
Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.
Usage 1: sqlplus -H | -V
    -H             Displays the SQL*Plus version and the
                   usage help.
    -V             Displays the SQL*Plus version.
Usage 2: sqlplus [ [<option>] [<logon>] [<start>] ]
  <option> is: [-C <version>] [-L] [-M "<options>"] [-R <level>] [-S]
    -C <version>   Sets the compatibility of affected commands to the
                   version specified by <version>.  The version has
                   the form "x.y[.z]".  For example, -C 10.2.0
    -L             Attempts to log on just once, instead of
                   reprompting on error.
    -M "<options>" Sets automatic HTML markup of output.  The options
                   have the form:
                   HTML [ON|OFF] [HEAD text] [BODY text] [TABLE text]
                   [ENTMAP {ON|OFF}] [SPOOL {ON|OFF}] [PRE[FORMAT] {ON|OFF}]
    -R <level>     Sets restricted mode to disable SQL*Plus commands
                   that interact with the file system.  The level can
                   be 1, 2 or 3.  The most restrictive is -R 3 which
                   disables all user commands interacting with the
                   file system.
    -S             Sets silent mode which suppresses the display of
                   the SQL*Plus banner, prompts, and echoing of
                   commands.
  <logon> is: (<username>[/<password>][@<connect_identifier>] | /)
              [AS SYSDBA | AS SYSOPER] | /NOLOG
    Specifies the database account username, password and connect
    identifier for the database connection.  Without a connect
    identifier, SQL*Plus connects to the default database.
    The AS SYSDBA and AS SYSOPER options are database administration
    privileges.
    The /NOLOG option starts SQL*Plus without connecting to a
    database.
  <start> is: @<URL>|<filename>[.<ext>] [<parameter> ...]
    Runs the specified SQL*Plus script from a web server (URL) or the
    local file system (filename.ext) with specified parameters that
    will be assigned to substitution variables in the script.
When SQL*Plus starts, and after CONNECT commands, the site profile
(e.g. $ORACLE_HOME/sqlplus/admin/glogin.sql) and the user profile
(e.g. login.sql in the working directory) are run.  The files may
contain SQL*Plus commands.
Refer to the SQL*Plus User's Guide and Reference for more information.

# 17  
Old 10-17-2011
This is not correct, it should be:

Code:
set -xv
{
 printf 'set pages 0 feed off ver off head off echo off\n'
 printf 'select
   *
 from
   bus_event
 where
   bus_event_seq_nbr in ( %s );\n' $(
     printf "'%s'\n" $(<./seqnbr.txt) |
       paste -sd, -
       )
   } | 
     sqlplus -s "$db_user"/"$db_pwd"@"$db_sid" > ./output.txt
set +xv

Note that there is a pipe after the closing brace.
# 18  
Old 10-17-2011
Quote:
still the same output :
Am pasting here the output of file I have executed and its output:

Code:
 
mingle% more new.ksh
#!/bin/ksh
set -xv
{
 printf 'set pages 0 feed off ver off head off echo off\n'
 printf 'select
   *
 from
   bus_event
 where
   bus_event_seq_nbr in ( %s );\n' $(
     printf "'%s'\n" $(<./seqnbr.txt) |
       paste -sd, -
       )
   } | 
     sqlplus -s "$db_user"/"$db_pwd"@"$db_sid" > ./output.txt
set +xv
mingle% new.ksh
{
 printf 'set pages 0 feed off ver off head off echo off\n'
 printf 'select
   *
 from
   bus_event
 where
   bus_event_seq_nbr in ( %s );\n' $(
     printf "'%s'\n" $(<./seqnbr.txt) |
       paste -sd, -
       )
   } | 
     sqlplus -s "$db_user"/"$db_pwd"@"$db_sid" > ./output.txt
+ sqlplus -s /@
+ printf set pages 0 feed off ver off head off echo off\n
+ 1> ./output.txt
+ paste -sd, -
+ 0< ./seqnbr.txt
+ printf '%s'\n 3969495 3969503 3969511 3969491 3969499
+ printf select
   *
 from
   bus_event
 where
   bus_event_seq_nbr in ( %s );\n '3969495','3969503','3969511','3969491','3969499'
set +xv
mingle% more output.txt
SQL*Plus: Release 10.2.0.4.0 - Production
Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.
Usage 1: sqlplus -H | -V
    -H             Displays the SQL*Plus version and the
                   usage help.
    -V             Displays the SQL*Plus version.
Usage 2: sqlplus [ [<option>] [<logon>] [<start>] ]
  <option> is: [-C <version>] [-L] [-M "<options>"] [-R <level>] [-S]
    -C <version>   Sets the compatibility of affected commands to the
                   version specified by <version>.  The version has
                   the form "x.y[.z]".  For example, -C 10.2.0
    -L             Attempts to log on just once, instead of
                   reprompting on error.
    -M "<options>" Sets automatic HTML markup of output.  The options
                   have the form:
                   HTML [ON|OFF] [HEAD text] [BODY text] [TABLE text]
                   [ENTMAP {ON|OFF}] [SPOOL {ON|OFF}] [PRE[FORMAT] {ON|OFF}]
    -R <level>     Sets restricted mode to disable SQL*Plus commands
                   that interact with the file system.  The level can
                   be 1, 2 or 3.  The most restrictive is -R 3 which
                   disables all user commands interacting with the
--More--(47%)

# 19  
Old 10-17-2011
Quote:
+ sqlplus -s /@
You need to assign values to these variables:

Code:
db_user
db_pwd
db_sid

This User Gave Thanks to radoulov For This Post:
# 20  
Old 10-17-2011
Quote:
Originally Posted by radoulov
You need to assign values to these variables:

Code:
db_user
db_pwd
db_sid

Thankyou verymuch it worked..
But can it be modified to not to display anything on terminal when ran..
becoz am getting lot of content displayed over terminal when i run the script.
Code:
mingle% new.ksh
{
 printf 'set pages 0 feed off ver off head off echo off linesize 2000\n'
 printf 'select
   *
 from
   bus_event
 where
   bus_event_seq_nbr in ( %s );\n' $(
     printf "'%s'\n" $(<./seqnbr.txt) |
       paste -sd, -
       )
   } | 
     sqlplus -s "$db_user"/"$db_pwd"@"$db_sid" > ./output.txt
+ sqlplus -s atlvivd/vivdev1@ltd119a
+ printf set pages 0 feed off ver off head off echo off linesize 2000\n
+ 1> ./output.txt
+ paste -sd, -
+ 0< ./seqnbr.txt
+ printf '%s'\n 3969495 3969503 3969511 3969491 3969499
+ printf select
   *
 from
   bus_event
 where
   bus_event_seq_nbr in ( %s );\n '3969495','3969503','3969511','3969491','3969499'
set +xv

# 21  
Old 10-17-2011
Yes, now that it works, you don't need the trace information no more.
Just remove set -xv from the beginning.
This User Gave Thanks to radoulov For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to execute sql query.

Hi Experts, Need your support. Not able to use sql query alias in shell script. Could you please help me in finding right way to use alias with sql query in shell script. Below is the code i am using. #!/bin/bash sqlplus -s abc/abc@abc << EOF> bcd.csv set trimspool on select zone_id... (4 Replies)
Discussion started by: as7951
4 Replies

2. Shell Programming and Scripting

How do I read sql query into shell script?

Hello All, I'm trying to put together a shell script that will: 1. connect to an oracle database 2. execute a query 3. save the output to a csv file I know that I can execute the sqlplus -s user/pass @dbsid and get logged in. What I would like to do is have my query in a separate text... (9 Replies)
Discussion started by: bbbngowc
9 Replies

3. Shell Programming and Scripting

How to embed sql query into our shell script?

Hi I would like to embed a sql query in my shell script. Also, before any the sql query is executed, i would like to validate username and password. (1 Reply)
Discussion started by: arghadeep adity
1 Replies

4. Red Hat

Sql query through shell script

hey , i am using this code to store value of a sql query and and then use it in other query but after some time , but it is not working. please help #!/bin/bash val_1=$( sqlplus -s rte/rted2@rel76d2 << EOF setting heading off select max(stat_id) from cvt_stats; exit EOF ) nohup... (5 Replies)
Discussion started by: ramsavi
5 Replies

5. Shell Programming and Scripting

$ symbol in sql query in shell script

Hi Team, Can you please help me to resolve this issue. Am unable to use this $ symbol in sql query in the shell script. For Example: # !/bin/sh export USER_NAME=XXX export PASSWORD=YYY export ORACLE_SID=xamdb echo $ORACLE_SID echo " Session Details ..." ... (1 Reply)
Discussion started by: indira_s
1 Replies

6. Shell Programming and Scripting

query sql using shell script

query sql using shell script, is it possible? my friend told me to do a file.sql and link to my shell script, but can i query sql using shell script? thanks in advance! (2 Replies)
Discussion started by: kingpeejay
2 Replies

7. Shell Programming and Scripting

executing a SQL query in shell script

Hi ALL, I need an help in connecting to oracle database, executing a select query and printing it on the screen. Can any one please write a simple code or psuedo code and let me know. select query returns multiple values( say select name from emp) Thanks in advance LM (1 Reply)
Discussion started by: lijju.mathew
1 Replies

8. Shell Programming and Scripting

Executing Sql Query Using Shell Script

HI ALL i have a requirement like this. i have to write a shell script to run a sql query. DB is oracle. once the query is run, the results of the query has to be published in a data file. can you please advice me how to go about it. i am absolutely new to shell scripts and this is a part of my job. (14 Replies)
Discussion started by: ragha81
14 Replies

9. UNIX for Advanced & Expert Users

Connecting DB in the Shell Script to do SQL Query

Any link or example to write shell script for the Connecting Oracle for Quering through SQL thanks in advance ... Cheers !! Mehul Doshi (3 Replies)
Discussion started by: mehuldoshi
3 Replies

10. UNIX for Dummies Questions & Answers

Executing a SQL query from a shell script

I cannot figure out how to run a SQL script, or just a sqlplus query, from a shell script (bash or ksh). Basically, I need to su - oracle from root and run a query, then test the exit status. (3 Replies)
Discussion started by: 98_1LE
3 Replies
Login or Register to Ask a Question