Oracle 脚本中的错误处理

发布于 2024-09-13 02:41:50 字数 708 浏览 5 评论 0原文

我已经尝试解决这个问题有一段时间了,我认为是时候寻求帮助了。我正在构建一个架构配置脚本,我想添加一些脚本输出和错误处理。这个想法是脚本输出窗口只会向我显示关键消息,而不会显示所有噪音。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倾城花音 2024-09-20 02:41:50

在 Oracle 中,您定义事务边界——完成后提交,并且每个语句都是原子的。

如果您使用 SQL*Plus 并且在出现问题时根本不希望提交任何内容,则可以将以下内容放入 SQL 脚本中:

SET ECHO ON
SPOOL /some/path/to/logfile.log
WHENEVER SQLERROR EXIT SQL.CODE ROLLBACK

-- run your code or DML statements

COMMIT;
EXIT;

这将在第一次遇到错误时崩溃,因此错误语句将位于日志文件的末尾。它还会回滚任何更改,因此只要没有任何 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:

SET ECHO ON
SPOOL /some/path/to/logfile.log
WHENEVER SQLERROR EXIT SQL.CODE ROLLBACK

-- run your code or DML statements

COMMIT;
EXIT;

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 COMMITs (or statements that cause them, like CREATE, ALTER, DROP, GRANT, or REVOKE), then entire upgrade is all-or-nothing.

沐歌 2024-09-20 02:41:50

在我见过的大多数情况下,此类(重复)任务是使用脚本自动执行的。

我们目前的一种方法是使用 UNIX 脚本来运行给定目录中的所有 .sql 文件并生成 .log 文件。在该过程结束时,我们 grep 日志文件并查看是否有任何错误。

您不需要手动打印任何错误,因为 sqlplus 已经打印出错误并将其捕获在日志文件中。

下面是一个非常简单的示例...

.ksh 文件

#!/usr/bin/ksh
echo "Starting provisioning script.."

sqlplus scott/tiger@oracle102 > file1.log << !
@file1.sql
@file1.sql
!

echo "end of provisioning script"

以及 file1.sql 的内容(在本示例中位于同一目录中),

create table test123(
   id number,
   name varchar2(200)
);

当我运行此脚本时,第一个创建成功,第二个创建成功一个失败..日志文件将是..就像..

SQL*Plus: Release 10.2.0.4.0 - Production on Fri Aug 6 20:44:08 2010

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>
Table created.

SQL> create table test123(
             *
ERROR at line 1:
ORA-00955: name is already used by an existing object


SQL> Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

您可以按照这些方式准备一个脚本..然后在执行完成后在日志文件中查找任何错误..。您可以使用各种 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

#!/usr/bin/ksh
echo "Starting provisioning script.."

sqlplus scott/tiger@oracle102 > file1.log << !
@file1.sql
@file1.sql
!

echo "end of provisioning script"

And the contents of file1.sql (which is in the same directory for this example)

create table test123(
   id number,
   name varchar2(200)
);

when i run this script, the first create succeeds and the second one fails..and the log file would be .. like..

SQL*Plus: Release 10.2.0.4.0 - Production on Fri Aug 6 20:44:08 2010

Copyright (c) 1982, 2007, Oracle.  All Rights Reserved.


Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

SQL>
Table created.

SQL> create table test123(
             *
ERROR at line 1:
ORA-00955: name is already used by an existing object


SQL> Disconnected from Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文