2016-01-25 35 views
0

我有很多shell腳本來執行Oracle數據庫上的過程和其他任務。通常,它們具有相同的結構,檢查envvars,執行sql並使用show sqlcode來知道腳本是否成功執行。腳本不顯示sqlcode

這是代碼:

#!/bin/bash 
# Check for var's in another script 
. /home/oracle/cron/scripts/exp_variaveis.sh 

$ORACLE_HOME/bin/sqlplus User/[email protected] <<! 
spool /home/oracle/cron/log/some_name.log_$date_log 

set serveroutput on 
DECLARE 
    V_QTD NUMBER; 
BEGIN 
SELECT COUNT(*) QTD 
INTO V_QTD 
FROM D.TB_R_A TRA 
    WHERE TRA.CD_STATUS = 'A' 
    AND TRA.CD_T_AUT = 'E' 
    AND TRA.DT_P < TRUNC(SYSDATE); 

    IF V_QTD = 0 THEN 
    DBMS_OUTPUT.PUT_LINE('Sem resgates para serem expirados.'); 
    ELSE 

    FOR R1 IN (SELECT TRA.CD_R, TRA.CD_A, TRA.CD_O 
      from D.TB_R_A TRA 
       WHERE TRA.CD_STATUS = 'A' 
        AND TRA.CD_T_AUT = 'E' 
        and TRA.DT_P < TRUNC(sysdate)) 
    LOOP 

    BEGIN 

    UPDATE D.TB_R_A T 
     SET T.CD_STATUS = 'E' 
     WHERE T.CD_R = R1.CD_R; 

    COMMIT; 

    DBMS_OUTPUT.PUT_LINE('Resgate expirado: ' || R1.CD_R); 

    EXCEPTION 
     WHEN OTHERS THEN 
      ROLLBACK; 
      DBMS_OUTPUT.PUT_LINE('Falha na expiracao do resgate: ' || R1.CD_R); 
     END; 

    END LOOP; 

    END IF; 

END; 
-- command show slqcode to know if was executed with success 
show sqlcode 

spool off 
exit 
! 
# A variavel recebe a instrucao de busca do arquivo e se existir reporta o "sqlcode 0" que equilave a "SUCESSO" 
sql_code=`grep -i "sqlcode 0" /home/oracle/cron/log/some_name.log_$date_log` 

# A variavel trata a instrucao acima e processa os dados nos textos e operacoess com arquivos (awk) mostrando de forma CSV (-F) 
# apresentando as strings da segunda coluna '{ print $2 }' 
ret_code=`echo $sql_code | awk -F" " '{ print $2 }'` 

# Se a variavel $ret_code for diferente de 0 
if [ "$ret_code" != 0 ] 

then 
# Send error message 
mail [email protected] -s "!! ERROR !! CRON => DATABASE - SOME TEXT HERE" < /home/oracle/cron/log/some_name.log_$date_log 
exit 1 
else 
# Send sucessful message 
mail [email protected] -s "Job OK CRON => DATABASE - SOME TEXTE HERE" < /home/oracle/cron/log/some_name.log_$date_log 
exit 0 
fi 

我的問題是:這個SQL不輸出SQLCODE,所以如果我的腳本無法找到日誌字符串「SQLCODE」,它發送一個錯誤信息即使它成功執行。

請有人告訴我什麼是錯的。

回答

3

請注意,SQLCODE將始終返回0,異常處理程序之外使用時,如the docs提到:

SQLCODE僅在異常處理程序中有用。外的處理程序,SQLCODE總是返回0

但原因你甚至不看到0輸出,是你從來沒有執行PL/SQL代碼塊等等show聲明不被識別爲一個語句立即執行。您需要添加一行以forward slash (/)

/(斜槓)

執行存儲在SQL緩衝區中的最近執行的SQL命令或PL/SQL塊。

所以地方削減在這裏:

END LOOP; 
    END IF; 
END; 
/
-- command show slqcode will always show 0. 
show sqlcode 
+0

事情是這樣的: 「END LOOP; END IF; END; \顯示SQLCODE閥芯關閉退出」? –

+0

它應該是一個正斜槓,而不是反斜槓。添加到我的答案。 – trincot

1

SQLCODE不僅使Sens在異常處理程序,這裏是它的用法的例子:

CREATE TABLE errors (code NUMBER, message VARCHAR2(64), happened TIMESTAMP); 
DECLARE 
    name employees.last_name%TYPE; 
    v_code NUMBER; 
    v_errm VARCHAR2(64); 
BEGIN 
    SELECT last_name INTO name FROM employees WHERE employee_id = -1; 
    EXCEPTION 
     WHEN OTHERS THEN 
     v_code := SQLCODE; 
     v_errm := SUBSTR(SQLERRM, 1 , 64); 
     DBMS_OUTPUT.PUT_LINE('Error code ' || v_code || ': ' || v_errm); 
     INSERT INTO errors VALUES (v_code, v_errm, SYSTIMESTAMP); 
END;