金仓数据库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)

更多信息,参见https://help.kingbase.com.cn/v8/index.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值