Reading values from sql query


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reading values from sql query
# 1  
Old 06-24-2013
Reading values from sql query

I have sql query in shell script.[B]

Code:
select  distinct account_no  from adj  order by account_no;

This query returns account number daily.Sometimes it may return 90 rows sometime it may return 1 row only and someday it may return 0 rows
I am storing the output of this query in sql_output.txt.

I am passing the account number to another shell script
Code:
bip.sh

how can i do this in a loop

Code:
bip.sh 01 account_number_1;
bip.sh 02 account_number_2; ---this bip.sh is second instance of bip.sh

i mean i want to take 2 different account numbers at a time and pass it to
bip.sh
# 2  
Old 06-24-2013
If you have account numbers in sql_output.txt

Code:
 
exec 5< sql_output.txt
while read acc1 <&5 ; do
read acc2 <&5
bip.sh 01 acc1
bip.sh 02 acc2
done

This User Gave Thanks to vidyadhar85 For This Post:
# 3  
Old 06-24-2013
Quote:
Originally Posted by vidyadhar85
If you have account numbers in sql_output.txt

Code:
 
exec 5< sql_output.txt
while read acc1 <&5 ; do
read acc2 <&5
bip.sh 01 acc1
bip.sh 02 acc2
done


Hi what does exec 5 means.what does 5 signifies.

I dont think i can use your code in my script.Currently i am passing one account number at a time

Code:
for i in `cat sql_output.txt`
do
echo "bip $i is running"

sh bip.sh 01 0 $i > sample_output.txt
sleep 100
--other stuff here
done


Last edited by rafa_fed2; 06-24-2013 at 07:47 AM..
# 4  
Old 06-24-2013
exec 5< will create a file handler...

I wont get it why you cant use it? the whole reason you posted here is to get a solution because your current code doesnt work that way right?

I am unable to sense the requirement here Smilie
# 5  
Old 06-24-2013
Quote:
Originally Posted by vidyadhar85
exec 5< will create a file handler...

I wont get it why you cant use it? the whole reason you posted here is to get a solution because your current code doesnt work that way right?

I am unable to sense the requirement here Smilie


Let me rephrase.
Currently,I am storing account numbers in my sql_output.txt.
I am passing one account number one at a time to bip.sh(it runs in the background and genrate a number which i need to fetch).

how can i run
sh bip.sh 01 $i ---i has my first account number
sh bip.sh 01 $ --- what variable should i use here so it gets the second account number
# 6  
Old 06-24-2013
It could be simplified by using:-
Code:
while read acc
do
   bip.sh $acc
done < sql_output.txt

This will loop for every record in your file. Be careful if you need to exclude things such as the column headings and numbers of row returned or blank lines.

If you have more than one column in the file and (for instance) the account number is in column 2 and you don't care about columns 1, 2 or three and from 5 onward, you could:-
Code:
while read col1 col2 col3 acc rest
do
   bip.sh $acc
done < sql_output.txt


You could run bip.sh 01 $acc if the 01 is a required parameter or even bip.sh 01 $acc & to run it in the background, but be careful if your file may have more than just a few records. There are limits to how many processes you should have running at the same time.



I hope that this helps


Robin
Liverpool/Blackburn
UK

Last edited by rbatte1; 06-24-2013 at 11:15 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Asign to variable ksh some values of sql query

Hi, I'm trying to asign to ksh varible some values of a sql query. The output query would be: xxxx 1 yyyy 2 I do: values=`$PATH_UTI/query_sh " select think1||'------'||think2 from some_table where think3 = '$1'; ... (2 Replies)
Discussion started by: mierdatuti
2 Replies

2. Programming

Help writing SQL query

Hello All, I hope I'm posting this in the right section. I have zero sql query writing skill, in fact, I've never done it before, but for some reason, a request came across my desk to get information from one of our databases. I have about 200 ticket numbers that have no information attached,... (8 Replies)
Discussion started by: bbbngowc
8 Replies

3. Programming

Query SQL get two values differents from the same columns

Hi, I have 2 different values in the same column and two different values in other column Query 1 ins name value 1 Test 12345 1 TestV1 12/10/2014 8 Test 85435 8 TestV1 11/11/2005 9 Test 42232 9 TestV1 19/10/2000 6 Test 54321... (6 Replies)
Discussion started by: faka
6 Replies

4. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

5. UNIX for Dummies Questions & Answers

Getting values of 2 columns from sql query in UNIX variables

Hi, I have connected to oracle database with sqlplus -s / <<EOF select ename, age from emp where empid=1234; EOF I want to save the values of ename and age in unix shell variables. Any pointers would be welcome.. Thanks in advance!!1 Cheers :):):):) (1 Reply)
Discussion started by: gonchusirsa
1 Replies

6. Shell Programming and Scripting

Run SQL thru shell script: how to get a new line when run sql query?

Hi, this's Pom. I'm quite a new one for shell script but I have to do sql on shell script to query some information from database. I found a concern to get a new line...When I run my script, it retrieves all data as wondering but it's shown in one line :( What should I do? I'm not sure that... (2 Replies)
Discussion started by: Kapom
2 Replies

7. Shell Programming and Scripting

Query Oracle tables and return values to shell script that calls the query

Hi, I have a requirement as below which needs to be done viz UNIX shell script (1) I have to connect to an Oracle database (2) Exexute "SELECT field_status from table 1" query on one of the tables. (3) Based on the result that I get from point (2), I have to update another table in the... (6 Replies)
Discussion started by: balaeswari
6 Replies

8. 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

9. Shell Programming and Scripting

Reading values from a file using DB2 SQL

I have some alphbetical codes in that (1 Reply)
Discussion started by: kavithakuttyk
1 Replies

10. Shell Programming and Scripting

need help in reading a output of a sql query in unix script

i'm used a sql query in a unix script to get the information from table. but unable to extract the output which i need. Any help with logic will be greatly appreciated. my sql query provide output some thing like this - col1 col2 count ---- ---- ------ A B 10 c D 6 e... (8 Replies)
Discussion started by: pharos467
8 Replies
Login or Register to Ask a Question