金仓数据库KingbaseES PLSQL 支持语句级回滚

KingbaseES默认如果在PLSQL-block 执行过程中的任何SQL 语句导致错误,都会导致该事务的所有语句都被回滚,而Oracle 则是语句级的回滚。KingbaseES 为了更好的与 Oracle兼容,新增参数ora_statement_level_rollback 。当 ora_statement_level_rollback 为 on时,表示启用了语句级的回滚。

1、Oracle 与 KingbaseES 的差异

Oracle :在遇到 exception 时,只有触发异常的操作被回滚。

SQL> create table t(id integer);
Table created.
SQL> begin
  2    insert into t values(123);
  3    insert into t values('a');
  4  exception
  5    when others then
  6      commit;
  7  end;
  8  /
PL/SQL procedure successfully completed.
SQL> select * from t;
        ID
----------
       123

KingbaseES : 未启用语句级回滚。

test=# create table t(id integer);
CREATE TABLE
test=# begin
test-#   insert into t values(123);
test-#   insert into t values('a');
test-# exception
test-#   when others then
test-#     commit;
test-# end;
test-# /
ANONYMOUS BLOCK
test=# select * from t;                                                                                                     
 id
----
(0 rows)

2、启用语句级回滚

设置语句级回滚后,其行为与oracle 一致。

test=# set ora_statement_level_rollback=on;
SET
test=# begin
test-#   insert into t values(123);
test-#   insert into t values('a');
test-# exception
test-#   when others then
test-#     commit;
test-# end;
test-# /
ANONYMOUS BLOCK
test=# select * from t;
 id 
-----
 123
(1 row)

注意,PLSQL 的语句级回滚只发生在异常被正确捕获的场景下才有效,如果exception 没有被捕获或没有捕获exception,则还是整个事务回滚。如以下例子:

test=# set ora_statement_level_rollback=on;
test=# delete from t;
DELETE 1
test=# begin
test-#   insert into t values(123);
test-#   insert into t values('a');
test-# exception
test-#   when no_data_found then
test-#     commit;
test-# end;
test-# /
ERROR:  invalid input syntax for type integer: "a"
LINE 1: insert into t values('a')
QUERY:  insert into t values('a')
CONTEXT:  PL/SQL function inline_code_block line 3 at SQL statement
test=# select * from t;
 id
----
(0 rows)

你可能感兴趣的:(数据库,数据库,kingbase,金仓数据库,oracle,人大金仓)