Sqlplus not connecting the 2nd time in for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sqlplus not connecting the 2nd time in for loop
# 1  
Old 11-02-2017
Sqlplus not connecting the 2nd time in for loop

Hi,
I am trying to get the rows(First step is to get the poolid's and then second step run a loop to get the output based on each pool id and third connection is to get the member id and pool id based on a different condition) where based of certain conditions and storing it in a file. I wrote the below code, but when I try to execute it the first output is working fine and getting output.csv spooled, but from the next step I am getting login denied error message.

Code:
SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}] [edition=value]]
where <logon> ::= <username>[/<password>][@<connect_identifier>]
      <proxy> ::= <proxyuser>[<username>][/<password>][@<connect_identifier>]
SP2-0306: Invalid option.
Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM}] [edition=value]]
where <logon> ::= <username>[/<password>][@<connect_identifier>]
      <proxy> ::= <proxyuser>[<username>][/<password>][@<connect_identifier>]
SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus

Also I keep seeing this on the screen:
Code:
cat: output1.csv: No such file or directory
cat: output1.csv: No such file or directory
cat: output1.csv: No such file or directory
cat: output1.csv: No such file or directory
cat: output1.csv: No such file or directory
cat: output1.csv: No such file or directory
cat: output1.csv: No such file or directory
cat: output1.csv: No such file or directory

Below is my code:

Code:
#!/bin/bash

umask 022

cd /data/datatransfer/Astra_pool_alert
rm /data/datatransfer/Astra_pool_alert/output.csv /data/datatransfer/Astra_pool_alert/output1.csv /data/datatransfer/Astra_pool_alert/final_output.csv /data/datatransfer/Astra_pool_alert/output3.csv 2>/dev/null
# Cleanup from previous run

# Connect to DB and spool query result into a CSV file
sqlplus -S 'ajay/ajay@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abdcef)(PORT=1521)))(CONNECT_DATA=(service_name=1234)))'  >>log_file_2>>err_file  <<EOF
set echo off head off feed off pagesize 0 trimspool on linesize 1000 colsep ,
spool output.csv REPLACE
select poolid from pools where poolname ='ajay';
spool off;
exit;
EOF

# Using while loop read values into variables from CSV file and create flat file for each records
for i in `cat /data/datatransfer/Astra_pool_alert/output.csv`
do
sqlplus -S 'ajay/ajay@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abdcef)(PORT=1521)))(CONNECT_DATA=(service_name=1234)))'  >>log_file_2>>err_file  <<EOF
set echo off head off feed off pagesize 0 trimspool on linesize 1000 colsep ,
spool output1.csv REPLACE
SELECT p.poolid||','||p.POOLNAME||','|| p.STARTDATE||','|| p.ENDDATE||','|| p.POOLTYPE||','|| p.RELEASENUMBER||','|| p.UPDATE_DATE||','|| pi.MEMBERID FROM
POOLS p inner join POOLSITEMS pi on p.POOLID=pi.POOLID
inner join mediagroups m on m.MEDIAGROUPID=pi.MEMBERID
WHERE p.poolid=$i and p.poolname ='ajay'
AND p.SITECHANNEL = 'ABC'
and p.enddate >= sysdate ORDER BY p.UPDATE_DATE DESC;
spool off;
exit;
EOF
	for j in `cat output1.csv`
	do
		awk -F "," '{print $1 "," $6}' $j > output2.csv
		while IFS=, read V1 V2
		do
sqlplus -S 'ajay/ajay@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abdcef)(PORT=1521)))(CONNECT_DATA=(service_name=1234)))'  >>log_file_2>>err_file  <<EOF
set echo off head off feed off pagesize 0 trimspool on linesize 1000 colsep ,
SPOOL output3.csv REPLACE
SELECT pi.memberid||,||pi.poolid||,||m.mediagtype from poolsitems pi inner join mediagroups m on
m.mediagroupid=pi.memberid where pi.poolid='$V1' and pi.RELEASENUMBER='$V2'
and m.mediagtype='1' and m.startdate <= sysdate and m.enddate >= sysdate;
spool off;
exit;
EOF
			echo "$j" >> /data/datatransfer/Astra_pool_alert/final_output.csv
		done < output2.csv

		break;
	done
done

I am not sure on why it is giving login denied as its the same id and password as first step. Please let me know if I am not doing it correctly.

Thanks
Ajay
# 2  
Old 11-02-2017
Set xtrace, verbose and run your script to debug:-
Code:
#!/bin/bash -xv

I would also suggest to replace for loop with a while loop:-
Code:
while read i
do
    <your code here>
done < /data/datatransfer/Astra_pool_alert/output.csv

# 3  
Old 11-02-2017
Thanks for your reply. My bad, I had a typo in the password and so throwing that error.

But it is now failing with below error:
Code:
SELECT pi.memberid||,||pi.poolid||,||m.mediagtype from poolsitems pi inner join mediagroups m on
                    *
ERROR at line 1:
ORA-00936: missing expression

Code:
Code:
	while read j
	do
		echo $j > /data/datatransfer/Astra_pool_alert/j.csv
		awk -F "," '{print $1 "," $6}' j.csv >/data/datatransfer/Astra_pool_alert/output2.csv
		while IFS=, read V1 V2
		do
		echo $V1 $V2
sqlplus -S 'ajay/ajay@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abcde)(PORT=1521)))(CONNECT_DATA=(service_name=12345)))'  >>log_file_2>>err_file  <<EOF
set echo off head off feed off pagesize 0 trimspool on linesize 1000 colsep ,
SPOOL output3.csv REPLACE
SELECT pi.memberid||,||pi.poolid||,||m.mediagtype from poolsitems pi inner join mediagroups m on
m.mediagroupid=pi.memberid where pi.poolid='$V1' and pi.RELEASENUMBER='$V2'
and m.mediagtype='17' and m.startdate <= sysdate and m.enddate >= sysdate;
spool off;
exit;
EOF
			echo "$j" >> /data/datatransfer/Astra_pool_alert/final_output.csv
		done < /data/datatransfer/Astra_pool_alert/output2.csv

pi.RELEASENUMBER field is a string. Am I missing quotes when passing the variables?

Thanks
Ajay
# 4  
Old 11-03-2017
Consider moving your loop above the sqlplus, and simply writing the script out fully FIRST, then connect once and execute the script. The unneeded overhead of what you are doing currently is excessive The select statement has errors, I fixed them and moved things around so that you invoke sqlplus one time, run a script which can have as many lines as you need.

I also do not see what all the redirections are for but that is your problem
Code:
while read j
	do
		echo $j > /data/datatransfer/Astra_pool_alert/j.csv
		awk -F "," '{print $1 "," $6}' j.csv >/data/datatransfer/Astra_pool_alert/output2.csv
		while IFS=, read V1 V2
		do
		echo $V1 $V2
		print "SELECT pi.memberid,pi.poolid,m.mediagtype from poolsitems pi inner join mediagroups m on
m.mediagroupid=pi.memberid where pi.poolid=%s$V1%s and pi.RELEASENUMBER=%s$V2%s
and m.mediagtype='17' and m.startdate <= sysdate and m.enddate >= sysdate;"  "'" "$V1" "'"  "'" "$V2" "'"


			echo "$j" >> /data/datatransfer/Astra_pool_alert/final_output.csv
		done < /data/datatransfer/Astra_pool_alert/output2.csv > /tmp/tmp.sql
done		

sqlplus -S 'ajay/ajay@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=abcde)(PORT=1521)))(CONNECT_DATA=(service_name=12345)))'  >>log_file_2>>err_file  <<EOF
set echo off head off feed off pagesize 0 trimspool on linesize 1000 colsep ,
SPOOL output3.csv REPLACE
start /tmp/tmp.sql
spool off;
exit;
EOF		
rm /tmp/tmp.sql

This User Gave Thanks to jim mcnamara For This Post:
# 5  
Old 11-06-2017
Thanks Jim for your reply. print is not working and so I changed it to printf, but it is not printing it out correctly:

Below is the statement that I see in tmp.sql:
Code:
SELECT pi.memberid,pi.poolid,m.mediagtype from poolsitems pi inner join mediagroups m on
m.mediagroupid=pi.memberid where pi.poolid='285071285071 and pi.RELEASENUMBER='2017.x.17_MWR'
and m.mediagtype='17' and m.startdate <= sysdate and m.enddate >= sysdate;SELECT pi.memberid,pi.poolid,m.mediagtype from poolsitems pi inner join mediagroups m on
m.mediagroupid=pi.memberid where pi.poolid=2017.x.17_MWR285071' and pi.RELEASENUMBER=2017.x.17_MWR
and m.mediagtype='17' and m.startdate <= sysdate and m.enddate >= sysdate;

When using print(nothing is going to tmp.sql since print doesnt work):
Code:
+ print 'SELECT pi.memberid,pi.poolid,m.mediagtype from poolsitems pi inner join mediagroups m on
m.mediagroupid=pi.memberid where pi.poolid=%s219808%s and pi.RELEASENUMBER=%s2017.x.07_MWR%s
and m.mediagtype='\''17'\'' and m.startdate <= sysdate and m.enddate >= sysdate;' ''\''' 219808 ''\''' ''\''' 2017.x.07_MWR ''\'''
./prod_Astra_pool_alert_2.sh: line 43: print: command not found

# 6  
Old 11-06-2017
Note that print is a ksh built-in. In bash you can use printf:-
Code:
while IFS=, read V1 V2
do
        printf "SELECT pi.memberid,
               pi.poolid,
               m.mediagtype
        FROM   poolsitems pi
               INNER JOIN mediagroups m
                       ON m.mediagroupid = pi.memberid
        WHERE  pi.poolid = \'%s\'
               AND pi.releasenumber = \'%s\'
               AND m.mediagtype = '17'
               AND m.startdate <= sysdate
               AND m.enddate >= sysdate;\n" "$V1" "$V2"
done < /data/datatransfer/Astra_pool_alert/output2.csv > /tmp/tmp.sql

This User Gave Thanks to Yoda For This Post:
# 7  
Old 11-06-2017
Thank you. Everything is working as expected now.

Thanks
Ajay
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Connecting sqlplus from UNIX with multiple select statement

hi, i have a requirement where i need to connect sqlplus from unix and i am able to do so by following command: cust_count=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID << EOF set pagesize 0 set feedback off set verify off ... (1 Reply)
Discussion started by: lovelysethii
1 Replies

2. Red Hat

TNS Timeout Error when connecting to SQLPLUS through scripts only

Hi, I am facing a strange issue when connecting to SQLPLUS via a shell scripts. I am using Linux 2.6.18-274.18.1 and gbash shell. When I connect to SQLPLUS through scripts then it throws TNS Time Out error ""sometimes"" and connects successfully other times.This is only happening when... (9 Replies)
Discussion started by: aashish.sharma8
9 Replies

3. Shell Programming and Scripting

IF loop in sqlplus

Hi all, I am trying to use if loop inside SQLPLUS....is it possible...bcoz i tried a whole day with all the possibilities....no use...pls help Here's the code: KRITI = $? sqlplus -s /NOLOG << EOF connect $USER/$PASS IF $KRITI = 0 THEN create table kriti_employees( emp_id ... (1 Reply)
Discussion started by: kritibalu
1 Replies

4. UNIX for Advanced & Expert Users

Connecting once using sqlplus and doing multiple queries

Hello everyone, It's my first week using unix and shell scripting. I tried creating a script that has a function that execute SQL query. my script looks something like this: ---------------------------------------------------- #!/bin/sh tableName="myTable" secondTable="secondTable"... (2 Replies)
Discussion started by: edlin_r
2 Replies

5. Shell Programming and Scripting

Connecting to Oracle DB using sqlplus

Hi, I am very new to shell scripting and trying to write a simple shell script in which i am trying to achieve the following: 1. Connect to oracle database hosted on a different server 2. fire a query on the oracle db 3. store the output in a variable 4. use this variable for further logic... (26 Replies)
Discussion started by: shrutihardas
26 Replies

6. UNIX for Dummies Questions & Answers

Connecting to Oracle DB using sqlplus

Hi, I am very new to shell scripting and trying to write a simple shell script in which i am trying to achieve the following: 1. Connect to oracle database hosted on a different server 2. fire a query on the oracle db 3. store the output in a variable 4. use this variable for further logic... (1 Reply)
Discussion started by: shrutihardas
1 Replies

7. Shell Programming and Scripting

connecting through sqlplus

I am trying to connect to one of the oracle sever using uni through sqlplus command: sqlplus -s BOXI_ALPH_AUDITOR,Q078_audit$@Q047 But its not getting connected. I tried using some different server using same syntax its working. What differene i found is the password is having no special... (2 Replies)
Discussion started by: gander_ss
2 Replies

8. Shell Programming and Scripting

Showing errors when connecting to sqlplus in shell script

hi, I am trying to automate the compilation of procedures stored in .sql files in Unix. Is there any way in which we can sho err if there errors are raised in the compilation? I am using the following code to connect to the sqlplus sqlplus ${SQL_USER}/${SQL_PASSWORD} (5 Replies)
Discussion started by: silas.john
5 Replies

9. Shell Programming and Scripting

calling sqlplus from within a for loop

i'm not new to programming, but i AM new to unix scripting. here's my deal. this works: #!/bin/ksh echo "HELLO" /oracle_home/bin/sqlplus username/password@MYDB<<EOF SELECT COUNT(*) FROM EMPLOYEES; EOF exit echo "GOODBYE" this doesn't: #!/bin/ksh echo "HELLO" for x in 1 2... (4 Replies)
Discussion started by: akosz
4 Replies

10. UNIX for Dummies Questions & Answers

error connecting to sqlplus

Hi, I wrote a shell script to call oracle procedure. But when i am trying to connet sqlplus with the fallowing statement It is giving me error " callproce.sh : sqlplus: not found". What could be the problem. sqlplus -s $CONNECT_STRING >$LOGFILE <<!! thank u all papachi (2 Replies)
Discussion started by: papachi
2 Replies
Login or Register to Ask a Question