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.
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:
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:
#!/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 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:
SELECT pi.memberid||,||pi.poolid||,||m.mediagtype from poolsitems pi inner join mediagroups m on
*
ERROR at line 1:
ORA-00936: missing expression
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?
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
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
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:
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):
+ 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
Note that print is a ksh built-in. In bash you can use printf :-
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