Managing error handling while running sqlplus from shell scripts

前端 未结 5 1944
天涯浪人
天涯浪人 2020-12-14 03:58
#!/bin/sh

echo \"Please enter evaluate database username\"
read eval_user
echo \"Please enter evaluate database password\"
read eval_pass
echo \"Please enter the da         


        
相关标签:
5条回答
  • 2020-12-14 04:22

    What Max says is correct. Try this modified script

    #!/bin/sh
    
    echo "Please enter evaluate database username"
    read eval_user
    echo "Please enter evaluate database password"
    read eval_pass
    echo "Please enter the database name"
    read db_name
    
    LOGFILE=shell_log.txt
    
    sqlplus -s /nolog <<-EOF>> ${LOGFILE}
    WHENEVER OSERROR EXIT 9;
    WHENEVER SQLERROR EXIT SQL.SQLCODE;
    connect $eval_user/$eval_pass@$db_name
    DBMS_OUTPUT.put_line('Connected to db');
    EOF
    
    sql_return_code=$?
    
    if [ $sql_return_code != 0 ]
    then
    echo "The upgrade script failed. Please refer to the log results.txt for more information"
    echo "Error code $sql_return_code"
    exit 0;
    fi
    

    Please note the use of sql_return_code to capture the SQLPLUS return code.

    The DBMS_OUTPUT statement should fail with error - "SP2-0734: unknown command beginning...". You can find the error message in log file.

    It is possible to trap the sp2 errors in SQLPLUS 11g using the error logging facility. Please have a look at http://tkyte.blogspot.co.uk/2010/04/new-thing-about-sqlplus.html for more information.

    0 讨论(0)
  • 2020-12-14 04:26

    The fact you are entering fake values, are probably only related to the login. Then: Check database connectivity using Shell script

    The WHENEVER ... are for errors during the SQL script execution. Once you'll successfuly connect with your script (I assume this your problem right now), you should get the kind of error managed by WHENEVER ERROR because you forgot the EXEC at your line with DBMS_OUTPUT.

    0 讨论(0)
  • 2020-12-14 04:26

    You can only trap sql error or os error. The dbms_output will fail at sqlplus level itself so the whenever error setting does not affect it.

    0 讨论(0)
  • 2020-12-14 04:29

    it might be possible that your whenever statements are executed after connection to the db has been established (since you have mentioned them afterwards). Try the following code :-

    $ORACLE_HOME/bin/sqlplus -s /nolog <<-EOF>> ${LOGFILE}
    WHENEVER OSERROR EXIT 9;
    WHENEVER SQLERROR EXIT SQL.SQLCODE;
    connect $eval_user/$eval_pass@$db_name
    DBMS_OUTPUT.put_line('Connected to db');
    EOF
    
    0 讨论(0)
  • 2020-12-14 04:36

    Aji's answer with

    WHENEVER SQLERROR EXIT SQL.SQLCODE;
    

    and then using

    sql_return_code=$?
    

    is not correct (or not correct in most cases). See details below.


    Shell script in an UNIX OS can return codes up to 255. E.g. "ORA-12703 this character set conversion is not supported" return code should be 12703, but it doesn't fit into UNIX 8-bit return code.
    Actually I just did a test and ran a bad SQL that fails with "ORA-00936: missing expression" -
    sqlplus returned 168 (!).
    So the actual return code 936 was wrapped at 256 and just remainder got returned. 936%256=168.


    On Windows this probably could work (not tested), but not on UNIX (tested as explained above).


    The only reliable mechanism is probably to spool results into a log file and then do something like

    tail -n 25 spool.log | egrep "ORA-" | tail -n 1 | cut -d: -f1 | cut -d- -f2
    

    So it would grep the spool log file and then cut actual latest ORA-code.

    0 讨论(0)
提交回复
热议问题