Handling Error Conditions
When we run SQL statements interactively in SQL*Plus or SQL Developer or another utility, we decide
how to react to an error. If a SQL statement fails from a syntax error or from an unexpected data
condition such as ORA-01403: no rows found, do we want to proceed and run the next SQL statement, or do we want to simply roll back all work that has been done and exit? When executing interactively, we
can decide interactively. But what about when we’re running a script?
SQL*Plus provides the WHENEVER command to direct SQL*Plus how to react to failures. WHENEVER is
particularly useful when running a script. Table 11-7 describes two variations of the command.
Table 11-7. WHENEVER Error-Handling Conditions
Error condition Description
WHENEVER OSERROR Triggers whenever a SQL*Plus command like CONNECT, DISCONNECT, SPOOL, HOST,
START, or any other command which interacts with the operating system fails.
WHENEVER SQLERROR Triggers whenever a SQL statement like SELECT, INSERT, UPDATE, DELETE, CREATE,
ALTER, DROP, TRUNCATE, GRANT, REVOKE, or any other SQL command fails.
Table 11-8. WHENEVER Error-Handing Directives
Error condition Description
EXIT [ exit-status | txn-directive ] Exit from SQL*Plus with the specified exit status after
committing or rolling back the current transaction as directed.
CONTINUE [ txn-directive ] Continue executing SQL*Plus after committing, rolling back,
or doing nothing.
Exit-status Can be one of:
[ SUCCESS | FAILURE | n | substitution-variable | bind-
variable ]
where SUCCESS is an operating-system dependent exit status signifying successful completion, FAILURE is an operating-
system dependent exit status signifying failure, and n is a
number value. SQL*Plus substitution variables and SQL*Plus
bind variables containing numeric values can also be used as
return statuses. SUCCESS is the default.
Txn-directive Can be one of:
[ COMMIT | ROLLBACK | NONE ]
where NONE can be used only with the CONTINUE directive.
When used with the EXIT directive, COMMIT is the default and
when used with the CONTINUE directive, NONE is the default.
So, if a SQL*Plus script. contains five UPDATE statements in a row and you want the script. to stop
executing, roll back any work already performed, and then exit to the operating system with a failure
status, your script. might look something like that shown in Listing 11-45.
Listing 11-45. Error-Handling in a SQL*Plus Script
whenever oserror exit failure rollback
whenever sqlerror exit failure rollback
set echo on feedback on timing on
spool update_script.
update …
update …
update …
update …
update …
exit success commit (*由此可以看出不在前面加whenever條件也是可以的,因為exit、continue都是指令,都可以單獨使用的)
In Listing 11-45, we see the use of the WHENEVER command directing SQL*Plus to exit back to the
operating system with a FAILURE exit status, and perform. a ROLLBACK as it does so, should any OS
commands (such as SPOOL) or SQL commands (such as UPDATE) fail. If all of the commands are successful
and we reach the very last line of the script, then we will EXIT back to the operating system with SUCCESS
exit status and perform. a COMMIT as it does so.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27042095/viewspace-735492/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [Bash] Error handlingError
- RxJava Error Handling OperatorsRxJavaError
- error C2713: Only one form of exception handling permitted per functionErrorORMExceptionMITFunction
- glassfish3 install,dwr3+struts2 deploy error handlingError
- Under the conditions with the lancel handbags sale
- 【譯】 WebSocket 協議第八章——錯誤處理(Error Handling)Web協議Error
- 好用的expected_conditions模組
- ALL, ANY and SOME Comparison Conditions in SQLSQL
- Cookie Handling in WinHTTPCookieHTTP
- Multi-path handling for asmASM
- Exception Handling in Asp.net MVCExceptionASP.NETMVC
- PL/SQL Tutorials - HANDLING ERRORS AND EXCEPTIONSSQLErrorException
- [Typescript] Handling a Truly Empty Object in TypeScriptTypeScriptObject
- Handling duplicate form submission in Spring MVCORMSpringMVC
- Netty series: handling CORS in nettyNettyCORS
- Android TV-Handling TV HardwareAndroid
- [LeetCode] 3142. Check if Grid Satisfies ConditionsLeetCode
- SAP Fiori 的附件處理(Attachment handling)
- [譯] Architecture Components 之 Handling Lifecycles
- selenium細節實戰02-->好用的expected_conditions模組
- Lerning Entity Framework 6 ------ Handling concurrency With SQL Server DatabaseFrameworkSQLServerDatabase
- back button history handling for iframe with jquery (IE failed me again)jQueryAI
- Master Note for Handling Oracle Database Corruption Issues [ID 1088018.1]ASTOracleDatabase
- android-Optimizing Content for the Assistant,Handling App LinksAndroidAPP
- PLSQL Language Referenc-PL/SQL集合和記錄-使用Multiset Conditions比較巢狀表SQL巢狀
- SAP S4HANA 根據PO號碼得到各個ITEM的Conditions資料
- mount error(5): Input/output errorError
- 【ERROR】OPatch failed with error code 73ErrorAI
- error:slave communication error with ASMErrorASM
- ERROR: slave communication error with ASMErrorASM
- SAP 沒有啟用HUM功能照常可以使用Handling Unit
- SAP cross distribution chain status在Fiori應用中的draft handlingROSAIRaft
- MMON encountered error 959, clearing the errorError
- 如何解決"Parse error: syntax error"Error
- 『心善淵』Selenium3.0基礎 — 24、Selenium的expected_conditions模組詳細介紹
- Java併發程式設計(Java Concurrency)(8)- 競爭與臨界區(Race Conditions and Critical Sections)Java程式設計
- DB error due to HP-UX Error:23ErrorUX
- CRS ERROR - PRKC-1073 - ErrorError