sql—在oracle数据库中将两个insert查询作为单个事务处理

toiithl6  于 2021-07-29  发布在  Java
关注(0)|答案(2)|浏览(311)

我试图在oracle中将两个insert查询作为一个事务处理。
我试过以下方法

BEGIN
INSERT INTO table1 VALUES (27, TO_DATE('19-Jun-2018 05:47', 'DD-MON-YYYY HH24:MI'), '622222222222', 167, 15454015, '335218', '5623125454');
INSERT INTO table2 VALUES ('145218', TO_DATE('19-Jun-2018 05:47', 'DD-MON-YYYY HH24:MI'), ADD_MONTHS(TO_DATE('19-Jun-2018 05:47', 'DD-MON-YYYY HH24:MI'),1));
END;

但这给了我一个错误
错误报告-

ORA-06550: line 5, column 1:
PLS-00103: Encountered the symbol "INSERT" 
06550. 00000 -  "line %s, column %s:\n%s"

* Cause:    Usually a PL/SQL compilation error.
* Action:
tmb3ates

tmb3ates1#

指定要插入值的列。试试下面。

BEGIN
   INSERT INTO TABLE1 (COL1,
                       COL2,
                       COL3,
                       COL4,
                       COL5,
                       COL6,
                       COL7)
        VALUES (27,
                TO_DATE ('19-Jun-2018 05:47', 'DD-MON-YYYY HH24:MI'),
                '622222222222',
                167,
                15454015,
                '335218',
                '5623125454');

   INSERT INTO TABLE2 (COL1, COL2, COL3)
           VALUES (
                     '145218',
                     TO_DATE ('19-Jun-2018 05:47', 'DD-MON-YYYY HH24:MI'),
                     ADD_MONTHS (
                        TO_DATE ('19-Jun-2018 05:47', 'DD-MON-YYYY HH24:MI'),
                        1));
COMMIT;

END;
/
vmpqdwk3

vmpqdwk32#

如果您使用的是纯sql,那么oraclesql中就没有begintransaction语句(beginispl/sql关键字仅用于启动pl/sql块)。
事务从数据库中的第一次写入开始,以提交或回滚结束。
例子:

TEST@DB0>truncate table t1;

Table truncated.

TEST@DB0>truncate table t2;

Table truncated.

TEST@DB0>insert into t1 values(1, 'ONE');

1 row created.

TEST@DB0>insert into t2 values(2, 'TWO');

1 row created.

TEST@DB0>rollback;

Rollback complete.

TEST@DB0>select * from t1;

no rows selected

TEST@DB0>select * from t2;

no rows selected

TEST@DB0>insert into t1 values(1, 'ONE');

1 row created.

TEST@DB0>insert into t2 values(2, 'TWO');

1 row created.

TEST@DB0>commit;

Commit complete.

TEST@DB0>select * from t1;

    X1 Y1
---------- ----------
     1 ONE

TEST@DB0>select * from t2;

    X2 Y2
---------- ----------
     2 TWO

TEST@DB0>

相关问题