How do I use SQL to query based off file data?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How do I use SQL to query based off file data?
# 1  
Old 06-26-2009
Power How do I use SQL to query based off file data?

This is basically what I want to do:

I have a file that contains single lines of IDs.
I want to query the oracle database using these IDs
to get a count of which ones match a certain condition.
the basic idea is:

cat myfile |
while read id
do
$id in select count(PC.ptcpnt_id)
from ptcpnt_cmpsit PC
where
PC.gndr_type = 'F'
;
done

Hopefully, you get the gist.
# 2  
Old 06-27-2009
so you want to use that ID in sql query??
is so
Code:
while read ID ; do 
sqlplus -s username/passwd << EOF
select count(PC.ptcpnt_$ID)
from ...
..
..;
exit;
EOF
done < myfile

# 3  
Old 06-29-2009
Power

Thanks! -- I will try this.

---------- Post updated at 10:56 AM ---------- Previous update was at 08:27 AM ----------

OK -- this is what I did:

cat *.sum >> recsum
sort -u -o recsum.sort recsum
print "** Total Records"| tee -a ${logfile}
while read id ; do
sqlplus -s ${lgn} << EOF | tee -a ${logfile}
select count(PC.tran_nbr)
from ptcpnt_cmpsit PC
where
PC.ptcpnt_id = $id
and PC.gndr_type = 'F'
;
exit;
EOF
done < recsum.sort
print "\n${sn}: COMPLETE [$(date)]:
Output appended to log file [${logfile}]
" | tee -a ${logfile}
echo > recsum

Assume I have a couple of files named *.sum in my directory.
My output is going to the logfile, but it is also going to the screen:
COUNT(PC.PTCPNT_ID)
----------------------
0
1 row selected.

COUNT(PC.PTCPNT_ID)
----------------------
1
1 row selected.

COUNT(PC.PTCPNT_ID)
----------------------
0
1 row selected.

I don't want it to go to the screen. It also seems to be in an infinite loop. It never ends. This is an example of a couple of rows in my file (total rows around 5000):

600010001
600010060
600010065
600010070
600010090
600010120
# 4  
Old 06-29-2009
Quote:
My output is going to the logfile, but it is also going to the screen:
thats because you are using tee command(use only >>)
read man page of tee.. tee command will display the o/p on screen and redirect it to file also..
# 5  
Old 06-30-2009
Power

Thanks -- that took care of it. The while loop is working. It is just
verrrrrrrrrrrrrrrrrrrrrrrrrrrrrry slow. I'll have to figure out a better way to
do this.
Thanks for your help.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Need sql query to string split and normalize data

Hello gurus, I have data in one of the oracle tables as as below: Column 1 Column 2 1 NY,NJ,CA 2 US,UK, 3 AS,EU,NA fyi, Column 2 above has data delimited with a comma as shown. I need a sql query the produce the below output in two columns... (5 Replies)
Discussion started by: calredd
5 Replies

2. Shell Programming and Scripting

Run sql query in shell script and output data save as delimited text

I want to run sql query in shell script and output data save as delimited text (delimited text would be comma) Code: SPOOL_FILE=/pgedw/dan.txt SQL=/pgedw/dan.sql sqlplus -s username/password@myhost:port/servicename <<EOF set head on set COLSEP , set linesize 32767 SET TRIMSPOOL ON SET... (8 Replies)
Discussion started by: Jaganjag
8 Replies

3. Shell Programming and Scripting

Read input file and used it to SQL query

Hi All, Seeking for your assistance to read each line $1 and $2 of input file and used it to query. Ex. file1.txt(number range) 9064500000 9064599999 9064600000 9064699999 9064700000 9064799999 Database name: ranges_log a_no message 9064500001 test 9064700000 ... (7 Replies)
Discussion started by: znesotomayor
7 Replies

4. Shell Programming and Scripting

Shell scripting unable to send the sql query data in table in body of email

I have written a shell script that calls below sql file. It is not sending the query data in table in the body of email. spool table_update.html; SELECT * FROM PROCESS_LOG_STATS where process = 'ActivateSubscription'; spool off; exit; Please use code tags next time for your code and data.... (9 Replies)
Discussion started by: Sharanakumar
9 Replies

5. UNIX for Advanced & Expert Users

Output the SQL Query result to a File

Hello Guys, This message is somewhat relates with last thread. But I need to re-write thing. I start over a little. I am stuck now and need your help. Here is my script- #! /bin/ksh export ORACLE_HOME=/opt/oracle/app/oracle/product/9.2 /opt/oracle/app/oracle/product/9.2/bin/sqlplus -s... (5 Replies)
Discussion started by: thepurple
5 Replies

6. Shell Programming and Scripting

Dynamic SQL query based on shell script parameters

Hi, I need a script that will run a dynamic Oracle SQL. Dynamic meaning the SQL statement depends on the parameter. For instance, something like this: #!/bin/ksh -x # Set environment . /home/mine/set_vars sqlplus $LOGINID <<! >> /home/mine/log.txt select count(1) from $1 where... (2 Replies)
Discussion started by: laiko
2 Replies

7. Shell Programming and Scripting

How to use sql data file in unix csv file as input to an sql query from shell

Hi , I used the below script to get the sql data into csv file using unix scripting. I m getting the output into an output file but the output file is not displayed in a separe columns . #!/bin/ksh export FILE_PATH=/maav/home/xyz/abc/ rm $FILE_PATH/sample.csv sqlplus -s... (2 Replies)
Discussion started by: Nareshp
2 Replies

8. Shell Programming and Scripting

redirecting sql query output to a file

Hi, I am executing sql files in my unix shell script. Now i want to find whether its a success or a failure record and redirect the success or failure to the respective files. meaning. success records to success.log file failure record to failure.log file. As of now i am doing like... (1 Reply)
Discussion started by: sailaja_80
1 Replies

9. Shell Programming and Scripting

how to use data in unix text file as input to an sql query from shell

Hi, I have data in my text file something like this. adams robert ahmed gibbs I want to use this data line by line as input to an sql query which i run by connecting to an oracle database from shell. If you have code for similar scenario , please ehlp. I want the output of the sql query... (7 Replies)
Discussion started by: rdhanek
7 Replies

10. Shell Programming and Scripting

unloading sql query to file

In unload to "/usr/home/data.012202" I wish to use a date variable as in unload to "/usr/home/data.`date`" for the file is that possible in a query to do, or will i need to add a mv command after the query to do it. I tried backquotes, , and () on `date` but didn't seem to work Thanks... (2 Replies)
Discussion started by: Link_02
2 Replies
Login or Register to Ask a Question