Sponsored Content
Top Forums Shell Programming and Scripting How to catch sql error in script? Post 302892826 by Ditto on Friday 14th of March 2014 04:36:52 PM
Old 03-14-2014
Quote:
Originally Posted by shamrock
Put this line whenever sqleror exit sql.sqlcode as the first line of your "query.sql" file...
That won't quite work as expected:

Code:
 > more gg.sql
whenever sqlerror exit sql.sqlcode;

select 1/0 from dual;

exit

 > sqlplus -s myid@mydb @gg.sql
Enter password:
select 1/0 from dual
        *
ERROR at line 1:
ORA-01476: divisor is equal to zero


 > echo $?
196
 >

So not quite.
has something to do with 1476 being modded down by 256 (or something - i'll let somebody more knowledgeable comment on that)

I've had most success by just grepping the output file for an ORA- error code (or SP2, etc.)

Code:
# error codes to look for (add/remove what you want to look for)
ERRCODES="(ORA-|EXP-|IMP-|KUP-|MOD-|PLS-|SP2-|SQL-|TNS-)"

sqlplus -s myid@mydb @gg.sql   2>&1 >> ${LOGFILE}
ERR=$?

ERRC=`egrep "${ERRCODES}" ${LOGFILE} | wc -l`

and then you have an error if either:
[ $ERR -ne 0 ] || [ $ERRC -ne 0 ]

Also, to the OP: Ken6503: you should be careful with this.

Quote:
sqlplus -s user/${PASSWD}@${ORACLE_SID} @${DIR}/query.sql > outputfile
Your password will be visible to anyone on the same server, via "ps -ef" command.

If you're using an automated script, you really should consider externally defined ids, and then just use:

Code:
sqlplus -s  / @${DIR}/query.sql > outputfile


Last edited by Ditto; 03-14-2014 at 05:38 PM.. Reason: directed final comment to OP
This User Gave Thanks to Ditto For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop statement - catch error

I'm having a question about for loops. (bash) I have the following for example: for file in `ls *.txt` do read file ... done Now when there is a file present there is no problem, now when there is no file present I get the following output in my standard mail box : "No such... (4 Replies)
Discussion started by: lumdev
4 Replies

2. Shell Programming and Scripting

Shell script to catch PL/SQL return values

Hello, I need some help from the experts on PL/SQL and Shell scripting. I need a shell script that runs a PL/SQL procedure and gets the values returned from the PL/SQL procedure into the shell variables. The PL/SQL procedure returns multiple values. I was able to assign a single return value... (1 Reply)
Discussion started by: Veera_Raghav
1 Replies

3. Shell Programming and Scripting

Catch a PL/SQL exception in ksh file

Hi all Im trying to call a PL SQl block from a ksh file like this : sqlplus -s $DB_USERID/$DB_PASSWD@$DB_NAME<<eof whenever SQLERROR exit 1 var varError VARCHAR2(200); exec ODAS_BATCH_JOBS_RETRIEVE.retrieve_user_info(:varError); eof If there is a error then varError will return a... (1 Reply)
Discussion started by: Sam123
1 Replies

4. Shell Programming and Scripting

How to use catch, try and final in bash script

Hi Everyone, How to use catch, try and final in bash script? what is (SQLException e) and (IOException e), who to conver this 2 function to bash script? Thank you (8 Replies)
Discussion started by: ryanW
8 Replies

5. Shell Programming and Scripting

Shell script to catch PL/SQL return values

I tried searching the forum for similar posts but its closed now. Would appreciate any help on this. I am trying to capture return value from a select query into a variable. DB is Oracle I am able to spool it to a file but I donot intend to use it. Here is my script that does not work ;) I... (27 Replies)
Discussion started by: monie2717
27 Replies

6. Programming

C - advice how to catch some weird error

I have some unstable mistake in my program and out-of-idea how to catch it. I am looking for advice with a way to work it out! I have in a pretty complicated program (but one source file) set of int-counters - 15, if exactly. Lately, on final printout I have inpossible value (I am... (3 Replies)
Discussion started by: alex_5161
3 Replies

7. Shell Programming and Scripting

Catch error from SFTP session

We have script running to SFTP some file to the remote server. The problem is the SFTP transfer returns an exit code of 0 even if there is permission error during file transfer, connection refuse (like when sftp server is down), thus, returning the status of the script as success. I was thinking... (3 Replies)
Discussion started by: The One
3 Replies

8. Shell Programming and Scripting

How to grep sql error in shell script and exit the script?

I need help in the following script. I want to grep the sql errors insert into the error table and exit the shell script if there is any error, otherwise keep running the scripts. Here is my script #!/bin/csh -f source .orapass set user = $USER set pass = $PASS cd /opt/data/scripts echo... (2 Replies)
Discussion started by: allinshell99
2 Replies

9. Shell Programming and Scripting

Unable to catch the redirection error when the disk is full

Hi Experts, Problem summary : I am facing the below problem on huge files when the disk is getting full on the half way through the execution. If the disk was already full , the commands fail & everything is fine. Sample Code : head_rec_data_file=`head -1 sample_file.txt` cat... (9 Replies)
Discussion started by: Pruthviraj_shiv
9 Replies

10. Shell Programming and Scripting

Getting detailed error from sql script

Hello, I have a main.sql file which runs around 5-6 .sql files and each .sql file is spooling it in separate text file. In my shell script I am appending main.sql to one of my log file but I am not able to get detailed error if anything fails from those 5-6 .sql files. Those errors are... (1 Reply)
Discussion started by: sp92
1 Replies
All times are GMT -4. The time now is 09:09 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy