可以在Oracle上停用隐式提交吗?

时间:2013-04-23 08:56:48

标签: oracle transactions

是否可以取消激活隐式提交,这是在Oracle数据库上创建,删除,重命名,更改语句之后调用的?

简单的例子:

CREATE TABLE TEST.test2x (id NUMBER(10,0));
ALTER TABLE TEST1.test2x ADD PRIMARY KEY (id);

这将导致alter语句失败,因为架构错误,但现在已经创建了表。 因此,在使用create,alter等时,以某种方式可以绕过这种行为并且只提交全部或全部内容吗?

2 个答案:

答案 0 :(得分:2)

有一种方法可以做“全有或全无”的DDL,但只能以非常有限的方式进行 - CREATE SCHEMA声明。

例如,以下CREATE SCHEMA语句尝试创建两个表T1T2。但是,T2的DDL不正确。这两个表都没有被创建。

SQL> REM Verify the tables do not already exist.
SQL> SELECT * FROM T1;
SELECT * FROM T1
              *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> SELECT * FROM T2;
SELECT * FROM T2
              *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> CREATE SCHEMA AUTHORIZATION TEST
  2      CREATE TABLE T1
  3      (
  4          X NUMBER PRIMARY KEY
  5      )
  6      CREATE TABLE T2
  7      (
  8          -- Try to reference a column that does not exist.
  9          X NUMBER REFERENCES T1(Y)
 10      );
        X NUMBER REFERENCES T1(Y)
                 *
ERROR at line 9:
ORA-02428: could not add foreign key reference
ORA-00904: "Y": invalid identifier


SQL> REM Verify the tables still don't exist.
SQL> SELECT * FROM T1;
SELECT * FROM T1
              *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL> SELECT * FROM T2;
SELECT * FROM T2
              *
ERROR at line 1:
ORA-00942: table or view does not exist

但是,CREATE SCHEMA仅限于CREATE TABLECREATE VIEWGRANT语句。

答案 1 :(得分:0)

您可以在plsql“begin / end”块中运行这些命令。然后捕捉并处理错误。

begin
  execute immediate 'create table xyz (x number)';
  execute immediate 'alter table bad.xyz add';
exception
  when others then
    execute immediate 'drop table xyz';
end;