Problem with while loop and SQL


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problem with while loop and SQL
# 15  
Old 05-03-2007
few questions

Is this the entire piece of code that you are running? Where is the variable "$final" initialised? Please change the below in the code
Code:
EOF} | while read line

to
Code:
EOF
} | while read line

I hope you are doing that. If you have additional code other than this, check if the error has something to do with that. If you donot use the '-s' option, you have to handle the information that comes from the sqlplus. Otherwise, it could give confusing results. Otherwise, you can spool the output of the sql to a file.
Code:
set head off
spool $filename
select .......
spool off
exit

This will have the entire output of the sql in that file and you can try to manipulate that file. Check and see what suits you best.
# 16  
Old 05-03-2007
Quote:
Originally Posted by ranj@chn
Is this the entire piece of code that you are running? Where is the variable "$final" initialised? Please change the below in the code
Code:
EOF} | while read line

to
Code:
EOF
} | while read line

I hope you are doing that. If you have additional code other than this, check if the error has something to do with that. If you donot use the '-s' option, you have to handle the information that comes from the sqlplus. Otherwise, it could give confusing results. Otherwise, you can spool the output of the sql to a file.
Code:
set head off
spool $filename
select .......
spool off
exit

This will have the entire output of the sql in that file and you can try to manipulate that file. Check and see what suits you best.
hi,

i tried but its still not running...not too sure if i am doing the correct way

Code:
$DCITS_SQL << EOF
SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF TRIMS ON VERIFY OFF TIMING OFF TERMOUT OFF
spool $tmpfile
select a.FIN,a.STAFF_NAME_X,b.FIN,b.DEPT_C from ci_5day_staff a, ci_cits_consol_dtls b where a.FIN=b.FIN;
spool off
exit
EOF

while read $tmpfile

do
	if [ "$tmpfile" ]#check if line is not null
	then 
		#if line not null, parse the line into words/variables
		set $tmpfile #set the line to positional variables, in this case is $1 and $2
		fin ="$1" #staff fin number
		name="$2" #name of staff
		gway="$3" 
		echo "$fin,$name,$gway" >> $final

fi
done

error -> undefined tmpfile
# 17  
Old 05-04-2007
while syntax wrong

Change the while sytanx from
Quote:
while read $tmpfile

do
if [ "$tmpfile" ]#check if line is not null
then
#if line not null, parse the line into words/variables
set $tmpfile #set the line to positional variables, in this case is $1 and $2
fin ="$1" #staff fin number
name="$2" #name of staff
gway="$3"
echo "$fin,$name,$gway" >> $final

fi
done
to
Code:
while read line
do
	if [ "$line" ]#check if line is not null
	then 
		#if line not null, parse the line into words/variables
		set $line  #set the line to positional variables, in this case is $1 and $2
		fin ="$1" #staff fin number
		name="$2" #name of staff
		gway="$3" 
		echo "$fin,$name,$gway" >> $final

fi
done <$tmpfile

# 18  
Old 05-04-2007
hi,

thanks for your help but the tmpfile is still undefined variable...

Quote:
Originally Posted by ranj@chn
Change the while sytanx from

to
Code:
while read line
do
	if [ "$line" ]#check if line is not null
	then 
		#if line not null, parse the line into words/variables
		set $line  #set the line to positional variables, in this case is $1 and $2
		fin ="$1" #staff fin number
		name="$2" #name of staff
		gway="$3" 
		echo "$fin,$name,$gway" >> $final

fi
done <$tmpfile

# 19  
Old 05-04-2007
have you defined

Have you defined your tmpfile variable, like
Code:
tmpfile=/some/dir/tmpfile

this should be done at the beginning of the script.
# 20  
Old 05-04-2007
yup i did... seriously no idea where went wrong =(
# 21  
Old 05-04-2007
spool file

When you spool a sql to a file, the file automatically gets appended with a '.lst' extension. Could you check if that is the cause of the issue.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use for loop to execute multiple .sql files while using SQLPLUS for db connection.?

Hello , Im calling every single file inside my script like 1.sql,2.sql so on it looks so tedious. I want to replace with for loop where every file gets executed. When i use for loop im getting errorUnexpected EOF] , can anyone please help me out in this.. How i can use for loop to invoke my... (6 Replies)
Discussion started by: preethi87
6 Replies

2. Programming

SQL not working in a for loop

Need help. Any reason why the update is not working in this sql: #!/bin/ksh #setup your environment . /opt/lnpsite/nm00/scripts/setup_env nm00 for tn in `cat /home/cpac/Resync/sv_tn.list` do `sqlplus -s ${DB_USERID} << EOF SET ECHO OFF NEWP 0 SPA 0 PAGES 0 FEED OFF HEAD OFF... (5 Replies)
Discussion started by: mrn6430
5 Replies

3. Shell Programming and Scripting

UNIX/SQL loop to spool files!!

I want to spool files from unix using a sql script that would take values from another sql query within the KSH script.. unix loop select order_date from date_tbl for each order_Date i need to call this spool script with the value sqlplus scott/tiger@order_db @/ordspool/order_date.sql... (2 Replies)
Discussion started by: vr23
2 Replies

4. Shell Programming and Scripting

SQL query in a loop with single sqlplus connection

Hi, I'm trying to build a shell script that reads a set of accounts from a file. For each account I need to perform a set of sql queries. So I have a loop with a set of sqlplus connections to retrieved my data. Is it possible to have a single sqlplus connection before entering the loop and... (4 Replies)
Discussion started by: lsantacana
4 Replies

5. Shell Programming and Scripting

Compiling n number of SQL files in loop

Trying to compile all SQL files using a shell script. But the below code is not working. Below Code works fine when for loop is not there(commenting line no: 1,2 and 9). 1. sq_lfile=`ls *.sql` 2. for current_sql_file in $sql_file 3. do 4. sqlplus uname/pass@Service>>SQLLOG << -ENDOFSQL... (3 Replies)
Discussion started by: Dip
3 Replies

6. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

7. Shell Programming and Scripting

Sql in a linux loop

Hi i want to run a query in loop(unix loop) every 2 hours and spool the result to a file. i am trying but getting error here is the script $ while true do sqlplus ank/ank<<! spool ank.lst select * from v$database; exit sleep 7200 done :D (2 Replies)
Discussion started by: ankurk
2 Replies

8. Shell Programming and Scripting

how to call shell script from pl/sql loop

Hello, I am doing a shell script which contain a pl/sql loop to search for 3 values, i would like to call another shell script inside this sql loop each time it find the values. so how can i call shell script from pl/sql using its variables, any idea? Here is idea about the code: my... (1 Reply)
Discussion started by: rosalinda
1 Replies

9. Shell Programming and Scripting

Help shell script to loop through files update ctl file to be sql loaded

I am currently trying to find a way to loop through files in a given directory and for each file modify a ctl file and sql load it. I have been using the sed command to change the infile, badfile parameters of the control file. I have not yet tried to sql load it. Requirement: files are ftp to... (1 Reply)
Discussion started by: dba_nh
1 Replies

10. Shell Programming and Scripting

Unix-Sql Loop error

Hi, I am getting the error "No matching <<", when i run the below. Is it that we can't execute SQL in a Unix loop. I am executing a SQL in a loop. Is it the EOF is written wrongly. . /opt/app/wlsconfigv61/domains/profiles/oracleV901.profile export DBUSER=ecdb01 set -A DBINSTANCE... (3 Replies)
Discussion started by: manu_byc
3 Replies
Login or Register to Ask a Question