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
- [Vue Router] Error Handling and 404sVueError
- 【譯】 WebSocket 協議第八章——錯誤處理(Error Handling)Web協議Error
- Multi-path handling for asmASM
- Netty series: handling CORS in nettyNettyCORS
- [LeetCode] 3142. Check if Grid Satisfies ConditionsLeetCode
- 好用的expected_conditions模組
- Handling duplicate form submission in Spring MVCORMSpringMVC
- [Typescript] Handling a Truly Empty Object in TypeScriptTypeScriptObject
- SAP Fiori 的附件處理(Attachment handling)
- 第三週--20200314--Handling NULL Values in PostgreSQLNullSQL
- selenium細節實戰02-->好用的expected_conditions模組
- SAP cross distribution chain status在Fiori應用中的draft handlingROSAIRaft
- SAP 沒有啟用HUM功能照常可以使用Handling Unit
- ERROR 2026 (HY000): SSL connection error: unknown error numberError
- mount error(5): Input/output errorError
- 【ERROR】OPatch failed with error code 73ErrorAI
- Original error: Error: socket hang upError
- SAP S4HANA 根據PO號碼得到各個ITEM的Conditions資料
- error:03000086:digital envelope routines::initialization errorErrorGit
- 如何解決"Parse error: syntax error"Error
- ERROR 1045 (28000): ProxySQL Error: 報錯ErrorSQL
- 『心善淵』Selenium3.0基礎 — 24、Selenium的expected_conditions模組詳細介紹
- Error page: / Error infos: DedeCms錯誤警告Error
- Last_IO_Error: Got fatal error 1236ASTErrorGo
- Error for iOSErroriOS
- Setup had an error Error: At least one of these paths should existErrorAST
- Golang 學習——error 和建立 error 原始碼解析GolangError原始碼
- Angular 錯誤訊息:ERROR Error NullInjectorError No provider for XXAngularErrorNullIDE
- Error reporting for dbusError
- FileReader error 事件Error事件
- System Error CodesError
- ORACLE STREAM ERROROracleError
- Exception和ErrorExceptionError
- git add errorGitError
- ERROR | [iOS] unknown: Encountered an unknown error (Could not find a `ios` simulator (valid values:ErroriOS
- Error: error:0308010C:digital envelope routines::unsupported 解決方案ErrorGit
- 帝國CMS提示parse error syntax error的解決方法Error
- HBuilder解決:Error: error:0308010C:digital envelope routines::unsupportedUIErrorGit