Pipe SQL select statement results to script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Pipe SQL select statement results to script
# 1  
Old 09-24-2003
Pipe SQL select statement results to script

Hello

I would like to perform a select from a oracle table and return those values to my shell script

For example:

site=head -1 $infile | cut -c1-15 | awk '{printf "s%", $0}
sqlplus -s /nolog |& #Open pipe to sql

select col1, col2, col3, col4
from oracle_table
where col5 = $site

exit # return to UNIX Script

perform various operations using the retuned values col1, col2, col3 & col4 from the select statement

Thank you

Pat Houtakker
# 2  
Old 09-24-2003
I don't see a question in your post. I don't want to sound like Alex Trebek, but a question would help... As it is, I must first guess at your question and then provide an answer.

My guess: How to use a ksh coprocess? I don't know mysql, so I'll use a program that I do know: adb. Here is an interactive adb session on an HP-UX 11.0 system...
Code:
# adb -k /stand/vmunix /dev/kmem
maxuprc/D
maxuprc:
maxuprc:        200
nproc/D
nproc:
nproc:          1264
$q
#

The stuff in bold is what I typed. $q looks odd, but that is an adb command to quit. Notice that I get two lines of output with the first line being rather useless. My script will need to eat that line. I must take the output as it comes. So here is the script...
Code:
#! /usr/bin/ksh

adb -k /stand/vmunix /dev/kmem |&

print -p 'maxuprc/D'
read -p garbage
read -p garbage maxuprc
echo maxuprc = $maxuprc

print -p 'nproc/D'
read -p garbage
read -p garbage nproc
echo nproc = $nproc

print -p '$q'
wait
exit 0

So "print -p" sends stuff to the coprocess. And "read -p" gets the results. I don't need to "get back" to the script...I never go anywhere. I don't really need to wait for the coprocess to exit, but I always do...just habit.

I hope that you weren't actually asking an sql question. Smilie
# 3  
Old 09-24-2003
To all:

sorry - my question must not have been clearly stated - my problem is how do I assign the results of the sql select statement to variables that I can use in my shell script - i.e. connect to Oracle, execute the select statement and return to UNIX and have the ability to use the results of the select statement

thank you
# 4  
Old 09-25-2003
You can try this

#!/bin/sh
var1=`sqlplus -s scott/tiger <<EOF
set head off;
select something from table;
exit;
EOF`
echo $var1

Last edited by sssow; 09-25-2003 at 11:29 AM..
# 5  
Old 09-25-2003
From a previous post thread

OUT PARAMETER FROM ORACLE

Ever wanted to execute an
Oracle Procedure which contains
an out parameter, and use the
value returned from the out
parameter in the shell variable.

Try out this. The procedure
test1 adds two nos. and returns
output into the out parameter. Here is the

export ORACLE_HOME=< type in your Oracle Home Path >
export ORACLE_SID=< Mention the Oracle SID >
export PATH=$ORACLE_HOME:$ORACLE_HOME/bin:$PATH # This Path
is set to access the sqlplus executable
dummyvar=`sqlplus -s tcon4iqalib/tcon4iqalib <<end
set pagesize 0 feedback off ver off heading off echo off
serverout on
variable verr_mesg number
exec test1(4,5,:VERR_MESG)
print verr_mesg
exit;
end`
echo " Error is " $dummyvar
echo " Result is " $dummyvar
echo $dummyvar
#end of shell script
# 6  
Old 10-31-2003
Thanks for the replies - sorry for the slow post, but this is what I actually ended up using.


fields=`sqlplus -s / <<END
set pagesize 0 feedback off verify off heading off echo off
select col1 || ':' || col2 || ':' ||
col3 || ':' || col4|| ':' || col5|| ':' || col6 || ':'
from table_name
where col4 = '$siteid_upper';
exit;
END`

col1=`echo $fields | awk -F: '{printf "%s", $1}'`
col2=`echo $fields | awk -F: '{printf "%s", $2}'`
col3=`echo $fields | awk -F: '{printf "%s", $3}'`
col4=`echo $fields | awk -F: '{printf "%s", $4}'`
col5=`echo $fields | awk -F: '{printf "%d", $5}'`
col6=`echo $fields | awk -F: '{printf "%d", $6}'`
# 7  
Old 10-31-2003
Wow, someone actually revisited the boards to post their solution! lol Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Emailing results of a pl sql procedure from UNIX shell script

Hello All, I am writing the below unix script to email the result of a small pl sql procedure: #!/bin/bash ORACLE_HOME=/opt/oracle/orcts/product/9.2.0; export ORACLE_HOME SQLPLUS=$ORACLE_HOME/bin/sqlplus sqlplus -s user/pass@Db_instance<<EOF set echo off set feedback off set pages 0... (9 Replies)
Discussion started by: Bunty bedi
9 Replies

2. Shell Programming and Scripting

Parse SQL text and only format first SELECT statement.

Hi Forum. Need your expertise on the following question. I have the following file which I would like to parse, find first block of SELECT statment and concatenate all input fields as 1 field (~ delimited): Old File: SELECT /*+ USE_HASH(CCOMM ICAR IMAP IAS IP IMAS IMPS IAP SPCA) */ ... (5 Replies)
Discussion started by: pchang
5 Replies

3. UNIX for Dummies Questions & Answers

SQL statement is not work on unix script

Hi, I have the following basic script. However, the statement (line 5) is not work. The output data is not able to set my request format a30. Any advise? :mad: echo " Column filename format a30"|sqlplus4 echo Input file list to check: read filelist for file in `cat $filelist.txt` do... (1 Reply)
Discussion started by: happyv
1 Replies

4. Shell Programming and Scripting

How to run a SQL select query in Oracle database through shell script?

I need to run a SQL select query in Oracle database and have to capture the list of retrieved records in shell script. Also i would like to modify the query for certain condition and need to fetch it again. How can i do this? Is there a way to have a persistent connection to oracle database... (9 Replies)
Discussion started by: vel4ever
9 Replies

5. Shell Programming and Scripting

sql select command output formatting in shell script

Hi, I need to connect to the database and retrieve two variables from the database and store them in a variable,out of these two variables I need to get lastdigit appended to the variable 1 retrieved and variable 2 with out any modification in short select var,data from usage; o/p=... (1 Reply)
Discussion started by: rkrish
1 Replies

6. Shell Programming and Scripting

Read SQL statement in Script

Hi Guys.. need some urgent help... I am stuck in something badly I need to write a script which would read a sql statement (which might be a join/inner join/select/sub select etc. ) I need to read that sql statement ... and in the output I want all the table names and columns (doesn't... (4 Replies)
Discussion started by: freakygs
4 Replies

7. Shell Programming and Scripting

using SELECT sql statement in shell script

Hi there I have a database on a remote box and i have been using shell script to insert data into it for example, i could have a script that did this SN=123456 n=server1 m=x4140 sql="UPDATE main SET hostname='$n',model='$m' WHERE serial='$SN';" echo $sql |/usr/sfw/bin/mysql -h... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

8. UNIX for Dummies Questions & Answers

sql query results in unix shell script

Hi I want to get the a field from a SQL query into unix shell script variable. the whole situation is like this. 1. Opened a cursor to a table in DB2 databse. 2. Fetching individual rows with the help of cursor. 3. Each row has 4 fields. I want each of the field in individual shell... (1 Reply)
Discussion started by: skyineyes
1 Replies

9. Shell Programming and Scripting

Supress ' quotes in a select statement inside Shell Script

Hi I have a shell script in which a string variable is saving following query. SqlQuery="select a||'|'||b||'|'||c from dual" I am trying to pass above query as a parameter to some script but its giving me error for "single quote". I tried suppressing it using \' instead of just ' but it... (1 Reply)
Discussion started by: mahabunta
1 Replies

10. Windows & DOS: Issues & Discussions

Want to use the output of Select statement in Unix script

Hi, I have a UNIX script which calls SQL Select statement: Now i want to use the output of that select statement within my UNIX script so as to call different shell script depending upon the output of the select statement. Can anyone help me in this regard. TIA Akhil Goel (4 Replies)
Discussion started by: akhilgoel9
4 Replies
Login or Register to Ask a Question