Oracle 脚本中的错误处理
我已经尝试解决这个问题有一段时间了,我认为是时候寻求帮助了。我正在构建一个架构配置脚本,我想添加一些脚本输出和错误处理。这个想法是脚本输出窗口只会向我显示关键消息,而不会显示所有噪音。
Create Temporary Error Table
Begin Transaction
-- begin work block
Print "Doing some types of work"
-- do work here
If Error and Active Transactions > 0 Then Rollback
If Active Transactions = 0 Then Insert Error In Temp Error Table and Start Another Transaction
-- end work block
-- once all all work complete
If Active Transactions > 0 Then Commit Transactions
在 SQL Server 领域,我通常会使用 Red Gate 的 SQL Packager 已经弄清楚了(提示,提示 Red Gate - 我们需要 Oracle 版本:))。有什么想法可以从 Oracle 开始获得类似的东西吗?
I have been trying to figure this out for a little while now and I think it is time to ask for help.. I am building a schema provisioning script and I want to add some script output and error handling. The idea is that the script output window would only show me key messages without all the noise.
Create Temporary Error Table
Begin Transaction
-- begin work block
Print "Doing some types of work"
-- do work here
If Error and Active Transactions > 0 Then Rollback
If Active Transactions = 0 Then Insert Error In Temp Error Table and Start Another Transaction
-- end work block
-- once all all work complete
If Active Transactions > 0 Then Commit Transactions
In the SQL Server world I would normally just do this with Red Gate's SQL Packager which has it figured out (hint, hint Red Gate - we need an Oracle version :)). Any thoughts on where to begin with Oracle to get something similar?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Oracle 中,您定义事务边界——完成后提交,并且每个语句都是原子的。
如果您使用 SQL*Plus 并且在出现问题时根本不希望提交任何内容,则可以将以下内容放入 SQL 脚本中:
这将在第一次遇到错误时崩溃,因此错误语句将位于日志文件的末尾。它还会回滚任何更改,因此只要没有任何
COMMIT
(或导致这些更改的语句,例如CREATE、ALTER、DROP、GRANT
或 < code>REVOKE),那么整个升级要么全有要么全无。In Oracle, you define the transaction boundaries -- you commit when you're done, and each statement is atomic.
If you're using SQL*Plus and you don't want anything at all to commit if anything goes wrong, you can do put the following in the SQL script:
This will bomb out the first time it encounters an error, so the erroring statement will be at the end of the log file. It will also rollback any changes, so as long as there aren't any
COMMIT
s (or statements that cause them, likeCREATE, ALTER, DROP, GRANT
, orREVOKE
), then entire upgrade is all-or-nothing.在我见过的大多数情况下,此类(重复)任务是使用脚本自动执行的。
我们目前的一种方法是使用 UNIX 脚本来运行给定目录中的所有 .sql 文件并生成 .log 文件。在该过程结束时,我们 grep 日志文件并查看是否有任何错误。
您不需要手动打印任何错误,因为 sqlplus 已经打印出错误并将其捕获在日志文件中。
下面是一个非常简单的示例...
.ksh 文件
以及 file1.sql 的内容(在本示例中位于同一目录中),
当我运行此脚本时,第一个创建成功,第二个创建成功一个失败..日志文件将是..就像..
您可以按照这些方式准备一个脚本..然后在执行完成后在日志文件中查找任何错误..。您可以使用各种 sqlplus 会话命令来删除所有不需要的注释等。
我不知道有任何自动化工具可以做同样的事情。我一直希望这有帮助的原因
......
In most of the cases I have seen, such (repeating) tasks are automated using scripts.
One way we do it currently is a UNIX script that runs all the .sql files in a given directory and generates a .log file. At the end of the process, we grep the log file and see if there are any errors.
You wont need to print any errors manually, because sqlplus already prints out the error and it is captured in the log file.
Below is a very simple example...
.ksh file
And the contents of file1.sql (which is in the same directory for this example)
when i run this script, the first create succeeds and the second one fails..and the log file would be .. like..
You can prepare a script along these lines.. and then look for any errors in the log file.. once the execution is complete. You could use various sqlplus session commands t get rid of all the unwanted comments and such.
I am not aware of any automated tools that do the same. The reason I've always
Hope this helps...