Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Deepa.

Asked: June 29, 2000 - 12:45 pm UTC

Last updated: September 30, 2005 - 7:52 am UTC

Version: v8.0

Viewed 10K+ times! This question is

You Asked

Hi Tom,
Can we use > whenever sqlerror EXIT within PL/SQL
block.If yes, How....

Thankx....
Deepa....



and Tom said...

No, but you do not need to.

If an error happens in a block, that error will be turned into an exception. If your plsql block catches and handles that exception then it is no longer an error. If on the other hand, it lets the exception propagate OUT of the block, that will be an error raised to sqlplus and sqlplus will "exit".

So, if you have:

ops$tkyte@DEV8I.WORLD> whenever sqlerror exit
ops$tkyte@DEV8I.WORLD> begin
2 if ( to_number('a') > 5 ) then null; end if;
3 end;
4 /
begin
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 2


Disconnected from Oracle8i Enterprise Edition Release 8.1.5.0.0 - Production
With the Partitioning and Java options
PL/SQL Release 8.1.5.0.0 - Production

The very act of getting the error bumps us out of the block and then out of sqlplus.



Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

A reader, July 25, 2005 - 3:58 pm UTC

Hi Tom,

One question about whenever sqlerror - can it be used to selectively suppress/ignore oracle errors? I prepared this DDL implementation script for the DBAs, now they come back and say that their "policy" is that every "create table"/"create index" should precede with the "drop" statement; and the script should ignore the errors triggered by the "drop" command, but it should stop if any errors are returned by the "create" command. In short, they want to ingnore ora-942, ora-1418 and ora-955 errors only (the script should continue upon receiving these errors), but should stop upon receiving other errors.

I dont know if this can be done without pl/sql. When I asked them if they could tell me how to do it -they dont reply. Do you have any solution for this? (short of hiring a sniper to take down my entire dba team...)! Thanks.

Tom Kyte
July 25, 2005 - 4:14 pm UTC

if you want to handle specific errors, you'll need a programming language, like PLSQL.

Error handling before actually connecting to oracle

Jignesh, September 29, 2005 - 10:34 am UTC

ENV_HAR_DATABASE=bacshar
HAR_PROCESS=har_transform.ksh
ENV_DATABASE=ONE/TWO@THREE
TSTAMP=`date "+%d-%m-%Y %H:%M:%S"`
TRANS_LOG_MSG="####<${TSTAMP} o'clock GMT><2><Hard Archiving><`uname -n`><N/A><${HAR_PROCESS}><${ENV_HAR_DATABASE}><Hard Archiving><N/A><HAR ALERT_CODE Ha1016 APP_MESSAGE 2 Transform Unsuccessful>"

sqlplus -s /nolog <<EOF
connect ${ENV_DATABASE}
set feedback off
whenever sqlerror exit 1
exec P_TRANSFORM('$PROCESS_NAME','$JOB_TYPE');
exit 0;
EOF

if [ $? -ne 0 ] ; then
echo "An error was encountered." >> har_${PROCESS_NAME}_${JOB_TYPE}_err.log
echo "An error was encountered."
echo $TRANS_LOG_MSG >>$ENV_HAR_EMS_LOG_FILE
exit 1
fi


In the above case, I want to return 1 if I am not able to connect to oracle (due to tnsentry failure or oracle is not available) But failed to achive using above script. Any suggestions ?



Tom Kyte
September 30, 2005 - 7:52 am UTC

move the whenever sqlerror exit 1 to be before the connect. if connect fails, it'll exit.

Please ignore my above query

Jignesh, September 29, 2005 - 11:04 am UTC

It works fine if you put

whenever sqlerror exit 1

before

connect ${ENV_DATABASE}


More to Explore

VLDB

If you are new to partitioning, check out Connor McDonald's introduction series here.

VLDB

Documentation set on VLDB and Partitioning.