Oracle Sql Script Error Handling
class="underline">COMMIT | ROLLBACK] | CONTINUE [COMMIT | ROLLBACK | NONE]} Performs the specified action (exits SQL*Plus by default) if a SQL command or PL/SQL block generates an error. In iSQL*Plus, performs the specified action (stops the current script by default) and returns focus to the Workspace if a SQL command or PL/SQL block generates an error. Terms [SUCCESS | FAILURE | WARNING | n | variable | :BindVariable] Directs SQL*Plus to perform the specified action as soon as it detects a SQL command or PL/SQL block error (but after printing the error message). SQL*Plus will not exit on a SQL*Plus error. EXIT [SUCCESS | FAILURE | WARNING | n | variable | :BindVariable] Directs SQL*Plus to exit as soon as it detects a SQL command or PL/SQL block error (but after printing the error message). SQL*Plus will not exit on a SQL*Plus error. The EXIT clause of WHENEVER SQLERROR follows the same syntax as the EXIT command. See EXIT for more information. CONTINUE Turns off the EXIT option. COMMIT Directs SQL*Plus to execute a COMMIT before exiting or continuing and save pending changes to the database. ROLLBACK Directs SQL*Plus to execute a ROLLBACK before exiting or continuing and abandon pending changes to the database. NONE Directs SQL*Plus to take no action before continuing. Usage The WHENEVER SQLERROR command is triggered by SQL command or PL/SQL block errors, and not by SQL*Plus command errors. Examples The commands in the following script cause iSQL*Plus to stop processing the current script and return focus to the Input area on the Workspace if the SQL UPDATE command fails: The commands in the following script cause SQL*Plus to exit and return the SQL error code if the SQL UPDATE command fails: WHENEVER SQLERROR EXIT SQL.SQLCODE UPDATE EMP_DETAILS_VIEW SET SALARY = SALARY*1.1; The following SQL command error causes iSQL*Plus to stop processing the current script and return focus to the Input area on the Workspace if the SELECT command fails: WHENEVER SQLERROR EXIT SQL.SQLCODE select column_does_not_exiSt from dual; select column_does_not_exist from dual * ERROR at line 1: ORA-00904: invalid column name Disconnected from Oracle..... The following examples show that the WHENEVER SQLERROR command is not executed after errors with SQL*Plus commands, but it is executed if SQL commands or PL/SQL blocks cause errors: WHENEVER SQLERROR EXIT SQL.SQLCODE column LAST_name headIing "Employee Name" Unknown COLU
shot at without result. —Winston Churchill Run-time errors arise from design faults, coding mistakes, hardware failures, and many other sources. Although you cannot anticipate all possible errors, you can plan to handle certain kinds of errors meaningful to your PL/SQL program. With many programming languages, unless you disable error checking, a run-time error such as stack overflow or division by zero stops normal processing and returns control to the operating system. With PL/SQL, a mechanism called exception handling lets you "bulletproof" your program so that it can https://docs.oracle.com/cd/B19306_01/server.102/b14357/ch12052.htm continue operating in the presence of errors. This chapter contains these topics: Overview of PL/SQL Runtime Error Handling Advantages of PL/SQL Exceptions Summary of Predefined PL/SQL Exceptions Defining Your Own PL/SQL Exceptions How PL/SQL Exceptions Are Raised How PL/SQL Exceptions Propagate Reraising a PL/SQL Exception Handling Raised PL/SQL Exceptions Tips for Handling PL/SQL Errors Overview of PL/SQL Compile-Time Warnings Overview of PL/SQL https://docs.oracle.com/cd/B13789_01/appdev.101/b10807/07_errs.htm Runtime Error Handling In PL/SQL, an error condition is called an exception. Exceptions can be internally defined (by the runtime system) or user defined. Examples of internally defined exceptions include division by zero and out of memory. Some common internal exceptions have predefined names, such as ZERO_DIVIDE and STORAGE_ERROR. The other internal exceptions can be given names. You can define exceptions of your own in the declarative part of any PL/SQL block, subprogram, or package. For example, you might define an exception named insufficient_funds to flag overdrawn bank accounts. Unlike internal exceptions, user-defined exceptions must be given names. When an error occurs, an exception is raised. That is, normal execution stops and control transfers to the exception-handling part of your PL/SQL block or subprogram. Internal exceptions are raised implicitly (automatically) by the run-time system. User-defined exceptions must be raised explicitly by RAISE statements, which can also raise predefined exceptions. To handle raised exceptions, you write separate routines called exception handlers. After an exception handler runs, the current block stops executing and the enclosing block resumes with the next statement. If there is no enclosin
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About http://stackoverflow.com/questions/14734131/managing-error-handling-while-running-sqlplus-from-shell-scripts Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Managing error handling while running sqlplus oracle sql from shell scripts up vote 9 down vote favorite 4 #!/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 $ORACLE_HOME/bin/sqlplus -s /nolog <<-EOF>> ${LOGFILE} connect $eval_user/$eval_pass@$db_name WHENEVER OSERROR EXIT 9; WHENEVER SQLERROR EXIT SQL.SQLCODE; DBMS_OUTPUT.put_line('Connected to db'); EOF if [ $? != 0 ] then echo "The upgrade script oracle sql script failed. Please refer to the log results.txt for more information" echo "Error code $?" exit 0; fi I am entering garbage values trying to force this script to fail. But, annoyingly, it keeps moving ahead without any mention of any error code. What else needs to be done here? oracle shell sqlplus aix share|improve this question asked Feb 6 '13 at 16:35 roymustang86 1,941113573 Which OS user account are you running under? What database does the script login to? –APC Feb 11 '13 at 14:01 I tried your script, and the log file is capturing the error code. Here is the grab from the log file: ERROR: ORA-01017: invalid username/password; logon denied SP2-0734: unknown command beginning "DBMS_OUTPU..." - rest of line ignored. –Incognito Feb 12 '13 at 9:55 May I know, what're you getting in log file when you execute the script? –Incognito Feb 12 '13 at 11:18 add a comment| 5 Answers 5 active oldest votes up vote 7 down vote accepted +50 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_