Shell Script: Traverse Database Table Row by Row


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Shell Script: Traverse Database Table Row by Row
# 1  
Old 08-22-2011
Shell Script: Traverse Database Table Row by Row

Hello Everyone,
My issue is that I want to traverse a database table row by row and do some action on the value retrieved in each row.

I have gone through a lot of shell script questions/posts. I could find row by row traversal of a file but not a database table.

Please help.

Thanks & Regards,
Ahsan Ashgar
# 2  
Old 08-22-2011
which db ?

how you are connecting to the db. post the code
# 3  
Old 08-22-2011
The database table is on Oracle DB.

Please see the code below for connecting to DB:
=================================
Code:
MIN_BATCH=`sqlplus -s user/pw@sid<<eof
	set serveroutput on;
	set feedback off;
	set linesize 1000;
	SELECT to_char(Min(IF_ROW_BATCH_NUM),'999999999999999')MIN FROM SIEBEL.EIM_SRV_REQ WHERE IF_ROW_STAT = 'FOR RECUR IMPORT';
	`
	min="${MIN_BATCH#"${MIN_BATCH%%[[:digit:]]*}"}"   # strip off non-digit prefix from str ...........................................................................

=================================
Now I have to pick multiple values/rows rather than just the minimum batch number. So I need to traverse the table row by row.

Regards,
Ahsan

Last edited by pludi; 08-22-2011 at 11:16 AM.. Reason: code tags please
# 4  
Old 08-22-2011
Have a look at this example of reading data into shell variables from a SQL/Plus session. It should be of some help:

https://www.unix.com/shell-programmin...variables.html
# 5  
Old 08-22-2011
How about something like:

Code:
sqlplus -s user/pw@sid > /tmp/$$ <<eof 
	set serveroutput on;
	set feedback off;
	set linesize 1000;
	SELECT * FROM table;
EOF

while read LINE
do
       ...
done < /tmp/$$

rm -f /tmp/$$

Better to use a temp file than to cram it in backticks because if the output is large enough, backticks will truncate it.

It'd be more easily possible to pipe it into the while loop instead of storing in a temp file if you used a sql script:

Code:
sqlplus -s user/pw@sid < script.sql | while read LINE
do
        ...
done

# 6  
Old 08-22-2011
you'll most likely need:
Code:
set pagesize 0;
set heading off;

too.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

2. Shell Programming and Scripting

Splitting single row into multiple rows based on for every 10 digits of last field of the row

Hi ALL, We have requirement in a file, i have multiple rows. Example below: Input file rows 01,1,102319,0,0,70,26,U,1,331,000000113200000011920000001212 01,1,102319,0,1,80,20,U,1,241,00000059420000006021 I need my output file should be as mentioned below. Last field should split for... (4 Replies)
Discussion started by: kotra
4 Replies

3. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

My original files are like this below and I distinguish them from the AP_ID (file1 has 572 and file2 has 544). Also, the header on file1 has “G_” pre-pended. NOTE: these are only snippets of very large files and much of the data is not present here. Original File 1: ... (36 Replies)
Discussion started by: aachave1
36 Replies

4. Shell Programming and Scripting

Add Row from First Row (Split Row)

HI Guys, I have Below Input :- RepigA_hteis522 ReptCfiEtrBsCll_aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 RepigA ReptCfiEtrBsCll hteis522 aofe MSL04_MSL2_A25_1A 0 9 MSL04_MSL2_A25_1B 0 9 MSL04_MSL2_A25_1C 0 9 Split Data in two first row... (2 Replies)
Discussion started by: pareshkp
2 Replies

5. Shell Programming and Scripting

awk to convert table-by-row to matrix table

Hello, I need some help to reformat this table-by-row to matrix? infile: site1 A:o,p,q,r,s,t site1 C:y,u site1 T:v,w site1 -:x,z site2 A:p,r,t,v,w,z site2 C:u,y site2 G:q,s site2 -:o,x site3 A:o,q,s,t,u,z site3 C:y site3 T:v,w,x site3 -:p,routfile: SITE o p q r s t v u w x y... (7 Replies)
Discussion started by: yifangt
7 Replies

6. Shell Programming and Scripting

In php, Moving a new row to another table and deleting old row

Hi, I already succeed moving a new row to another table if the field from new row doesn't have the first word that I categorized (like: IRC blablabla, PTM blablabla, ADM blablabla, BS blablabla). But it can't delete the old row. Please help me with the script. my php script: INSERT INTO... (2 Replies)
Discussion started by: jazzyzha
2 Replies

7. Shell Programming and Scripting

Moving new row and deleting old row to another table

Hi, I want to move a new row to another table if the field from new row doesn't have the first word that I categorized (like: IRC blablabla, PTM blablabla, ADM blablabla, BS blablabla). I already use this script but doesn't work as I expected. CHECK_KEYWORD="$( mysql -uroot -p123456 smsd -N... (7 Replies)
Discussion started by: jazzyzha
7 Replies

8. Shell Programming and Scripting

Script shell on line and row

Hello, I want to make a script to do this : the file is with line : TOTO TEST1 TOTO TEST2 TITI TEST1 TITI TEST2 TITI TEST3 i want he become : TOTO TEST1 TEST2 TITI TEST1 TEST2 TEST3 (3 Replies)
Discussion started by: safsound
3 Replies

9. Shell Programming and Scripting

How to do row comparison in shell script

Hi, I need help on doing the below thing in shell script. I have a file with millions of rows called "abc.txt". i have another file with millions of rows called "xyz.txt". I would like to do the below operation. Open the abc.txt, read the first line, do some operations on the column... (2 Replies)
Discussion started by: informsrini
2 Replies

10. Shell Programming and Scripting

shell script add each row value

Hi, I wrote below small script which will add up each row value from a file and print the total, but it is not.plz let me know what is wrong.script hangs and not displaying anything. ----------------- while read line ; do sum=0; sum=$sum+$line; done echo $sum; exit 0; ------------------... (8 Replies)
Discussion started by: pradeep_script
8 Replies
Login or Register to Ask a Question