Need to Capture Sql Failures


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need to Capture Sql Failures
# 1  
Old 11-25-2011
Need to Capture Sql Failures

Hi guys,

I am inserting and updating records in Oracle database using shell Script based on business logic.

Now Whenever the insert or Update fails, i want to capture it.

i tried to capture the return code ($?), but it is not reflecting it.
Code:
####################################################################
##  Insertion of  Traking ID
####################################################################

sqlplus -s PASS/USERID@DB << EOF
+ sqlplus -s PASS/USERID@DB INSERT INTO TRACKING VALUES (sequencer.NEXTVAL,'GME','MEXICO','NA',current_date)
            *
ERROR at line 1:
ORA-00947: not enough values
 
ReturnCode=$?
+ ReturnCode=0
if (( $ReturnCode!=0 ))
        then
        echo "Prevalidation script Failed during insertion in PCM_BATCH_TRACKING table" | mail -s "Prevalidation Script Failure" xxx@xxx.com;
        exit 3;
fi
+ ((  0!=0  ))

I purposely did it inorder to check whether it is capturing or not.

Can somebody help me out.

Cheers!!!!!
# 2  
Old 11-25-2011
Well, the sqlplus command itself has succeeded! Smilie

You could use WHENEVER SQLERROR EXIT <code>, e.g.
Code:
# sqlplus -s PASS/USERID@DB << EOF
> WHENEVER SQLERROR EXIT SQL.SQLCODE
> SELECT * from notable;
> EOF
SELECT * from notable
              *
ERROR at line 1:
ORA-00942: table or view does not exist


# echo $?
174

# 3  
Old 11-25-2011
I generally work with both solutions:
-----> Use: "WHENEVER SQLERROR EXIT 1" (as commented by Carlo)
-----> Redirect SQLPLus' output to a file and validate the error.
-----> Also use: "sqlplus -S -L" to try to login just once.

Code:
sqlOutputFile="./sqlplusExec.log"
# sqlplus -S -L PASS/USERID@DB << EOF > "${sqlOutputFile}"
     WHENEVER SQLERROR EXIT 1
     SELECT * from notable;
EOF
sqlplusRetCode=${?}

countErrors=`egrep -c 'ORA-[0-9]{4}|SP2-|TNS-|Usage' "${sqlOutputFile}"`

if [ ${sqlplusRetCode} -ne 0 -o ${countErrors} -ne 0 ]
then
     echo "Found errors while executing SQLPlus[${sqlplusRetCode}][${countErrors}]: "
     egrep 'ORA-[0-9]{4}|SP2-|TNS-|Usage' "${sqlOutputFile}"
     exit 1
fi

I hope it helps!

Last edited by felipe.vinturin; 11-25-2011 at 08:55 AM..
These 2 Users Gave Thanks to felipe.vinturin For This Post:
# 4  
Old 11-29-2011
Thanks guys... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

UNIX Sqlplus - Capture the sql statement about to run and execution status

Greetings Experts, I am on AIX using ksh. Created a unix script which generates the CREATE OR REPLACE VIEW ... and GRANT .. statements, which are placed in a single .txt file. Now I need to execute the contents in the file (there are around 300 view creation and grant statements) in Oracle and... (4 Replies)
Discussion started by: chill3chee
4 Replies

2. Solaris

11.0 to 11.2 update failures

Attempting to update an 11.0 server with many non-global zones installed. pkg publisher is pkg.oracle.com/solaris/support. FMRI = pkg://solaris/entire@0.5.11,5.11-0.175.1.15.0.4.0:20131230T203500Z When we run pkg update --accept the server contacts oracle, checks packages, finds about 700... (4 Replies)
Discussion started by: CptCarrot
4 Replies

3. Shell Programming and Scripting

Capture rows for a column in file from delete sql -Oracle

Hi, This may not be the right forum but i am hoping someone knows an answer to this. I have to capture rows for a column that was deleted. How can i do that without having to write a select query? delete from myschema.mytable where currentdatetimestamp > columnDate this should delete 5... (4 Replies)
Discussion started by: jakSun8
4 Replies

4. Shell Programming and Scripting

Need to capture error of sybase sql in unix

Hi Gurus, I am very new in Unix, I have 1 script, in which I am truncating the table , then BCP the data in Sybase table, and then loading the data from sybase table to sybase table. every thing is working fine, but the problem is with Error. I made some hanges in my insert statement so... (1 Reply)
Discussion started by: aksar
1 Replies

5. UNIX for Dummies Questions & Answers

Capture Error In SQL Plus

Hi I am trying to fetch the data from Oracle Table More my_query.sql | sqlplus -s 'scott/tiger@OrcaleSID ' | sed 's///g;s///g' > sample_file Now if the table passed in the my_query.sql is wrong or there is some other error insied SQL how i can caputure that i tried More... (1 Reply)
Discussion started by: max_hammer
1 Replies

6. Shell Programming and Scripting

Capture linking failures

Hi all, I have a script file that has numerous linking commands (ln -s) and currently there is no checking to see if the linking is successful or not and I need to implement something that checks if any of the linking failed and report a failure. The method I can think of is a small function... (3 Replies)
Discussion started by: zmfcat1
3 Replies

7. UNIX for Dummies Questions & Answers

how to capture no. of rows updated in update sql in unix db2

hi, i am a new user in unix..and we have unix db2. i want to capture the no. of rows updated by a update db2 sql statement and redirect into a log file. I've seen db2 -m...but not sure how the syntax should be. The update sql that I'm going to run is from a file... Can you please share... (1 Reply)
Discussion started by: j_rymbei
1 Replies

8. Shell Programming and Scripting

How to capture value in shell variable from oracle sql?

Hi Friends, Do someone know how to capture value in a shell variable from oracle sql? Requirement : In a table we want to count the number of records and want to pass this value to a shell variable where it can be manipulated later. In ksh shell we open oracle connection from sqlplus. For... (1 Reply)
Discussion started by: sourabhsharma
1 Replies

9. HP-UX

Communication Failures

HI ALL, I have been trying to install a particular software using remote linux server. some thing like this: rsh <host ID> /usr/sbin/swinstall -x autoreboot=true -s /tmp/<software> <Product name>. The problem is whenever I try to install the product through a shell script the installation... (1 Reply)
Discussion started by: barun agarwal
1 Replies

10. UNIX for Dummies Questions & Answers

Running file sql from shell without capture the password

Dear All I have file a.sql, let's say the content is: _________________________________ select * from dual; exit; _________________________________ and I have shell script a.sh, the content is: _________________________________ ORACLE_SID=testing; export ORACLE_SID... (0 Replies)
Discussion started by: Aditya Purwanto
0 Replies
Login or Register to Ask a Question