0

是否可以停用在 Oracle 数据库上的创建、删除、重命名、更改语句之后调用的隐式提交?

简单的例子:

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

由于模式错误,alter 语句将失败,但现在已经创建了表。那么,在使用 create、alter 等时,是否有可能绕过这种行为,只提交全部或全部不提交?

4

2 回答 2

2

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

例如,以下CREATE SCHEMA语句尝试创建两个表,T1并且T2. 但是,DDLT2不正确。两个表最终都没有被创建。

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 TABLE,CREATE VIEWGRANT语句。

于 2013-04-23T15:57:07.297 回答
0

您可以在 plsql“开始/结束”块内运行这些命令。然后捕获并处理错误。

IE。

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;
于 2013-04-23T11:51:57.417 回答