0

I use "WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK;" in my plsql scripts to use them in shell scripts. This works fine:

echo exit | sqlplus user/pass@XE  @normal.sql && echo "boo"

Executes the script and prints "boo" This works fine too:

echo exit | sqlplus user/pass@XE  @bad.sql && echo "boo"

"boo" is not printed.

However in case bad is:

WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK;

create or replace
PACKAGE TESTING
IS
function boo (co_id number) return varchar2;
END;
/

create or replace
PACKAGE BODY TESTING
is
end;

Which is obviously wrong - no error code is returned and "boo" is printed. How can I return plsqsl compilation error codes from sqlplus scripts?

Ivan Sopov
  • 2,300
  • 4
  • 21
  • 39

1 Answers1

2

You will need to parse them from the output. Unix error codes are in the range 0 to 255, and theres all sorts of masks and signal stuff hidden in there. So you cannot record oracle error numbers in unix error codes.

So basically you need to make your sql scripts include the show errors statement. But you do NOT want the WHENEVER statement in there because that will error before the errors are printed. e.g. bad.sql will be

create or replace
PACKAGE TESTING
IS
function boo (co_id number) return varchar2;
END;
/
show errors

create or replace
PACKAGE BODY TESTING
is
end;
/
show errors

Then your shell script should be something like:

ERR_OUT="$( sqlplus rdbds/rdbds@XE  < bad.sql | egrep '^(ORA|PLS)-' )"
if [ -n "$ERR_OUT" ]
then
    echo "Errors in SQL:"
    echo "$ERR_OUT"
else
    echo boo
fi
Sodved
  • 8,428
  • 2
  • 31
  • 43
  • Thanks, that is what I was thinking about, but I hoped there is is a better way. – Ivan Sopov Nov 10 '11 at 12:32
  • Instead of parsing output of "show errors" it may be better to parse in sql the content of user_errors or all_errors table, as suggested here: https://forums.oracle.com/forums/thread.jspa?threadID=692710 (there is also another - third solution suggested there) – Ivan Sopov Nov 11 '11 at 11:46