Skip to Main Content
  • Questions
  • Whenever sqlerror - transferring text varuiable into shell

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, Anastasia.

Asked: November 01, 2016 - 10:14 am UTC

Last updated: November 02, 2016 - 12:45 am UTC

Version: 12c

Viewed 1000+ times

You Asked

Hello, Tom.
The description of WHENEVER SQLERROR instruction says, that one can define EXIT {variable} in it.
is it possible to return the text variable?
I need sql-script to return into shell-script (by which it's run) not onlu SQLCODE (or a predefined in EXIT {n} number), but also the sql-script file name.
When I use the syntax like this:

sqlplus sys/syspwd as sysdba << EOS
DEFINE VAR_EXT=script1.sql
SET ECHO ON
WHENEVER SQLERROR EXIT &VAR_EXT
@sql_scr_1.sql
@scl_scr_2.sql
select 1 from i_dont_exist;
EOS

echo $? > scr_lod.txt
I get
'EXIT variable "script1.sql" was non-numeric' error in shell. When I change it to:

sqlplus sys/syspwd as sysdba << EOS
SET ECHO ON
WHENEVER SQLERROR EXIT SQL.SQLCODE
@sql_scr_1.sql
@scl_scr_2.sql
select 1 from i_dont_exist;
EOS

echo $? > scr_lod.txt

then the shell has only a number, and no idea, whether the error occured while running sql_scr_1.sql or sql_scr_2.sql or the 'main' script from the here-document.
Is it possible to correspond the filename of the script file, or maybe there is another way to have a kind of flags 'Success\failure' on every script execution?

Thanks a lot in advance.

and Connor said...

Yes, the exit code has to be numeric.

For me, I would simply have regular information outputted to a log file, eg

host echo Starting script 1 >>$LOGFILE
@script1
host echo Starting script 2 >>$LOGFILE
@script2
host echo Starting script 3 >>$LOGFILE
@script3

and this can be extended to include timings etc.

That way,if you do have an error (and you exit), the logfile shows a full history of where you got to.

Hope this helps

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