事务与锁定-嵌套事务和自治事务

/*
drop table  emp; --if exists?
create table emp(
	empid number(5),
	empname varchar2(100),
	empage number(5)
);
*/
--可以通过 PRAGMA AUTONOMOUS_TRANSACTION;来声明当前事务为自治事务
--可以独立对本事务进行提交,不会外层事务。及时外层事务没有提交,自治事务也是可以被提交的。
delete from emp;
--定义一个嵌套的自治事务(存储过程)
create or replace procedure insertchenzw
as
	PRAGMA AUTONOMOUS_TRANSACTION;
begin
	insert into emp values(1,'chenzw',27);
	commit;
end;
/
--定义一个执行的存储过程:
create or replace procedure testtransaction
as
begin
	insert into emp values(2,'chenzz',27);
	insertchenzw;
	insert into emp values(2,'chenzz',27);
	rollback;
end;
/

execute testtransaction;
col empname format a20;
select * from emp;

drop procedure insertchenzw;
drop procedure testtransaction;

--示例程序执行如下:


SQL> create or replace procedure insertchenzw
  2  as
  3   PRAGMA AUTONOMOUS_TRANSACTION;
  4  begin
  5   insert into emp values(1,'chenzw',27);
  6   commit;
  7  end;
  8  /

过程已创建。

SQL> create or replace procedure testtransaction
  2  as
  3  begin
  4   insert into emp values(2,'chenzz',27);
  5   insertchenzw;
  6   insert into emp values(2,'chenzz',27);
  7   rollback;
  8  end;
  9  /

过程已创建。

SQL> execute testtransaction;

PL/SQL 过程已成功完成。

SQL> col empname format a20;
SQL> select * from emp;

     EMPID EMPNAME                  EMPAGE
---------- -------------------- ----------
         1 chenzw                       27

SQL> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值