DBAsupport.com Forums - Powered by vBulletin
Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Sql plus shell exit status

Hybrid View

  1. #1
    Join Date
    Aug 2000
    Posts
    143
    Is there a way of running a script from sql plus and then finding the exit status.

    Ie this normally works for most unix tools but not sqlplus for some reason.

    sqlplus user/password < @test.sql
    exit
    EOF
    EXITSTATUS=$?
    if [ $EXITSTATUS != 0 ]
    then
    echo error
    else
    echo success
    fi



  2. #2
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092

  3. #3
    Join Date
    Aug 2000
    Posts
    143
    Now I havnt a different problem, I can use the following to run a script:

    #/usr/bin/sh
    sqlplus girs/gpassword < @test.sql
    exit sql.sqlcode;
    EOF
    LOG=$?
    echo log is $LOG
    if [ $LOG != 0 ]
    then
    echo error
    else
    echo success
    fi

    Which works for a 1 command script but if the script contains 2 commands and the first command produces an error but the second command works ok the return value is ok.

    eg:

    Connected to:
    Oracle8i Enterprise Edition Release 8.1.6.0.0 - Production
    With the Partitioning option
    JServer Release 8.1.6.0.0 - Production

    SQL> select sysdate from dua
    *
    ERROR at line 1:
    ORA-00942: table or view does not exist



    SYSDATE
    ---------
    12-APR-02

    SQL> Disconnected from Oracle8i Enterprise Edition Release 8.1.6.0.0 - Production
    With the Partitioning option
    JServer Release 8.1.6.0.0 - Production
    log is 0
    success


    What I really need is a way of getting an error status for the whole script ie, if just one command fails the return status is non zero. Has anyone got any ideas?



  4. #4
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    You want the whole script to run even if you get an error somewhere in the script?
    Jeff Hunter

  5. #5
    Join Date
    Aug 2000
    Posts
    143
    Id ideally like it to stop yes, but if not carry on but log the error.

  6. #6
    Join Date
    Nov 2000
    Location
    greenwich.ct.us
    Posts
    9,092
    This should be the first line of your script:
    Code:
    whenever sqlerror exit sql.sqlcode
    It will exit immediately with a non-zero number whenever it encounters an error.
    Jeff Hunter

  7. #7
    Join Date
    Aug 2000
    Posts
    143
    Is there anyway to get this
    to react to errors in a pl/sql block that occur as a result of dbms_sql.parse statements

  8. #8
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    pl/sql is easy

    for example

    Code:
    #!/bin/ksh
    
    MAILTO="lcheng@amena.es"
    METADIR=${HOME}/bin/metadatos
    METASQL=${HOME}/bin/metadatos/sql
    METATMP=${METADIR}/log
    . ${METADIR}/env/metadatos.env
    
    metainsert()
    {
    # inserto en la table md_ejecucion_proceso
    
    codejecucionproceso=`sqlplus -s << KOF
    ${METAUSER}/${METAPWD}@${METATNS}
    @$METASQL/insert_metadatos.sql "$1" "$2" "$3" "$4"
    exit 0;
    KOF`
    
    case $? in
      0) export codejecucionproceso
         ;;
      1) echo "Ha fallado la extraccion del codigo del proceso en metadatos en $1 $2 $3" > ${METATMP}/metadatos${FECHA}.log
         mailx -s "Error de insercion en Metadatos Move" $MAILTO < ${METATMP}/metadatos${FECHA}.log
         ;;
      2) echo "Intento de ejecucion de un proceso inexistente" > ${METATMP}/metadatos${FECHA}.log
         echo "El proceso pertenece al grupo $1, aplicacion $2, Tipo carga $3" >> ${METATMP}/metadatos${FECHA}.log
         mailx -s "Metadatos Move detecto un ejecucion de proceso inexistente" $MAILTO < ${METATMP}/metadatos${FECHA}.log
         exit 1
         ;;
      3) echo "Intento de ejecucion de un proceso inactivo" > ${METATMP}/metadatos${FECHA}.log
         echo "Revisa la tabla md_ejecucion_proceso con codigo de ejecucion $codejecucionproceso" >> ${METATMP}/metadatos${FECHA}.log
         mailx -s "Metadatos Move detecto la ejecucion de un proceso inactivo" $MAILTO < ${METATMP}/metadatos${FECHA}.log
         exit 1
         ;;
      5) echo "Error de SQL durante la insercion de metadatos, revise @$METASQL/insert_metadatos.sql" > ${METATMP}/metadatos${FECHA}.log
         mailx -s "Error de insercion en Metadatos Move" $MAILTO < ${METATMP}/metadatos${FECHA}.log
         ;;
    esac
    }
    ignore those msgs coz they are in Spanish, so basically there are 5 exit status, in the pl/sql block you do

    Code:
    whenever sqlerror exit 5
    set pagesize 0
    set linesize 100
    set verify off
    set echo on
    set feed off
    set serverout on
    
    variable grupo varchar2(32)
    variable aplicacion varchar2(32)
    variable tipocarga varchar2(32)
    variable fasecarga number 
    variable return number
    
    DECLARE
      errmsg md_error.des_error%type;
    BEGIN
      :grupo := '&1';
      :aplicacion := '&2';
      :tipocarga := '&3';
      :fasecarga := &4;
    EXCEPTION
      WHEN OTHERS THEN
      errmsg := 'Error asignando variables bind en la insercion de metadatos:' || sqlerrm;
      insert into md_error values(seq_cod_error.nextval, sysdate, errmsg);
      commit;
      dbms_output.put_line('ERROR insertando md_ejecucion_proceso: ' || sqlerrm);
      :return := 1;
    END;
    /
    
    DECLARE
      inactive_process exception;
      errmsg md_error.des_error%type; 
      proceso_rec md_proceso%rowtype;
      ejec_proceso_rec md_ejecucion_proceso%rowtype;
    BEGIN
      select d.cod_estado_proceso, max(d.cod_proceso)
      into proceso_rec.cod_estado_proceso, proceso_rec.cod_proceso
      from md_grupo a, md_aplicacion b, md_tipo_carga c, md_proceso d, md_estado_proceso e
      where a.cod_grupo = b.cod_grupo
        and b.cod_aplicacion = c.cod_aplicacion
        and c.cod_tipo_carga = d.cod_tipo_carga
        and e.cod_estado_proceso = d.cod_estado_proceso
        and a.nom_grupo = :grupo
        and b.nom_aplicacion = :aplicacion
        and c.nom_tipo_carga = :tipocarga
        and d.cod_fase_carga= :fasecarga 
        group by d.cod_estado_proceso;
      select seq_cod_ejecucion_proceso.nextval
      into ejec_proceso_rec.cod_ejecucion_proceso
      from dual;
      if proceso_rec.cod_estado_proceso = 0
      then
           raise inactive_process;
      end if;
      insert into md_ejecucion_proceso
      (cod_ejecucion_proceso, cod_proceso, cod_status_salida, fec_inicio, fec_fin,
       num_reg_cargado, num_reg_erroneo, num_reg_descartado, des_mensaje_salida)
      values
      (ejec_proceso_rec.cod_ejecucion_proceso, proceso_rec.cod_proceso, '', sysdate, '',
       '', '', '', '');
      commit;
      dbms_output.put_line(ejec_proceso_rec.cod_ejecucion_proceso);
      :return := 0;
      EXCEPTION 
        WHEN INACTIVE_PROCESS THEN
          errmsg := 'Proceso ' || proceso_rec.cod_proceso || ' inactivo';
          insert into md_ejecucion_proceso
         (cod_ejecucion_proceso, cod_proceso, cod_status_salida, fec_inicio, fec_fin,
         num_reg_cargado, num_reg_erroneo, num_reg_descartado, des_mensaje_salida)
         values
         (ejec_proceso_rec.cod_ejecucion_proceso, proceso_rec.cod_proceso, 4, sysdate, sysdate,
          '', '', '', errmsg);
         commit;
         :return := 3;
        WHEN NO_DATA_FOUND THEN 
          errmsg := 'Proceso inexistente, abortando carga. Revise los procesos del grupo ' || :grupo || ' aplicacion ' || :aplicacion || ' tipo carga ' || :tipocarga || ' en la fase de carga ' || :fasecarga;
          insert into md_error values(seq_cod_error.nextval, sysdate, errmsg);
          commit; 
          dbms_output.put_line('Proceso inexistente, abortando carga');
          :return := 2;
        WHEN OTHERS THEN
          errmsg := 'Error en el proceso que pertenece al grupo ' || :grupo || ' aplicacion ' || :aplicacion || ' tipo carga ' || :tipocarga || ' fase carga ' || :fasecarga || sqlerrm;
          insert into md_error values(seq_cod_error.nextval, sysdate, errmsg);
          commit;
          dbms_output.put_line('ERROR insertando md_ejecucion_proceso: ' || sqlerrm);
          :return := 1;
    END;
    /
    
    exit :return
    the exit:return does the trick

  9. #9
    Join Date
    Aug 2000
    Posts
    143
    Is there anyway to set the exit status for pl/sql compilation errors.

    For example

    4/2 PL/SQL: SQL Statement ignored
    5/7 PLS-00201: identifier 'VIEW_RATINGSSEQUENCEDCOLUMNS' must be
    declared

  10. #10
    Join Date
    Jun 2000
    Location
    Madrid, Spain
    Posts
    7,447
    no because that is not an SQL error

    notice whenever sqlerror is for SQL not PL/SQL, plus PL/SQL is compiled internally, SQL*PLUS cannot interpret that kind of errors

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width