2013-08-07 16 views
13

Ogni volta che eseguo uno script sql usando Sql * plus e controllo $?, Ottengo 0 anche quando lo script non ha avuto successo.Sql * plus restituisce sempre il codice di uscita 0?

Esempio

#$ sqlplus user/[email protected] @script.sql 


SQL*Plus: Release 10.2.0.1.0 - Production on Wed Aug 7 14:20:44 2013 

Copyright (c) 1982, 2005, Oracle. All rights reserved. 


Connected to: 
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production 
With the Partitioning, OLAP and Oracle Data Mining options 
JServer Release 9.2.0.8.0 - Production 

    v$dataf-ile d, 
      * 
ERROR at line 6: 
ORA-00933: SQL command not properly ended 


Disconnected from Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production 
With the Partitioning, OLAP and Oracle Data Mining options 
JServer Release 9.2.0.8.0 - Production 
$ echo $? 
0 
$ 

desidero per restituire un valore diverso da zero quando si verifica un errore.

Come posso ottenere quello?

risposta

20

Devi dire esplicitamente a sqlplus di farlo, nel tuo script. In sostanza, ci sono due affermazioni che si possono utilizzare:

  • WHENEVER SQLERROR EXIT SQL.SQLCODE
  • WHENEVER OSERROR EXIT

Ad esempio:

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

E per gli errori del sistema operativo:

WHENEVER OSERROR EXIT FAILURE 
START no_such_file 

Per ulteriori informazioni, vedere this e that.

Spero che aiuti. In bocca al lupo!

+1

MODIFICA - Il codice di uscita predefinito con WHENEVER OSERROR EXIT è 0 (SUCCESSO), quindi alcune altre opzioni devono essere specificate esplicitamente. Ho aggiunto l'opzione FAILURE. – dbenham

5

Vlad's è la risposta che vorrei usare. Per aumentare la sua, tuttavia, cerco di usare un'istruzione EXIT esplicita se ho davvero bisogno di quello stato di ritorno. Per esempio

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

exit &exitcode; 
0

L'azione migliore potrebbe una combinazione delle altre idee su questa pagina e le idee a

Help with SQLPLUS please? How to make SQLPLUS startup with DEFINE `OFF` initially?

Creare un file login.sql, o modificare quello globale ad avere

WHENEVER OSERROR EXIT FAILURE 
WHENEVER SQLERROR EXIT SQL.SQLCODE 

al suo interno. Quindi, se il file non esiste, genererà un errore. Se una linea fallisce, si verificherà un errore.

Tuttavia, tenere a mente che, come dicono i documenti a: https://docs.oracle.com/cd/E11882_01/server.112/e16604/ch_twelve052.htm#SQPUG135 che alcuni comandi ancora non errore come ci si potrebbe aspettare.

Problemi correlati