Sql*plus always returns exit code 0?

You have to explicitly tell sqlplus to do that, in your script. Basically, there are two statements that you can use:

  • WHENEVER SQLERROR EXIT SQL.SQLCODE
  • WHENEVER OSERROR EXIT

For example:

WHENEVER SQLERROR EXIT SQL.SQLCODE
begin
  SELECT COLUMN_DOES_NOT_EXIST FROM DUAL;
END;
/

And for OS errors:

WHENEVER OSERROR EXIT FAILURE
START no_such_file

For more information, see this and that.

Hope it helps. Good Luck!


Vlad's is the answer I'd use. To augment his, however, I try to use an explicit EXIT statement if I really need that return status. For example

column status_for_exit new_value exitcode noprint
select status_computation (parm, parm) as status_for_exit from dual;

exit &exitcode;