Ivan Sopov
Ivan Sopov

Reputation: 2370

How to return error code in case plsql compilation error from sqlplus

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?

Upvotes: 0

Views: 3831

Answers (1)

Sodved
Sodved

Reputation: 8588

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

Upvotes: 2

Related Questions