Loading

PLSQL事务

 1 使用set transaction设置事务属性
 2 只读事务 set transaction read only
 3 读写事务 set transaction write;
 4 在进行数据统计分析工作时,一般都会查询数据库中的多个表,此时可以将查询统计
 5 工作定义为只读事务, 防止进行DML操作
 6 --只读事务示例
 7 declare
 8       v_1981 number(2);
 9       v_1982 number(2);
10       v_1983 number(2);
11 begin
12   commit;
13   set transaction read only name '统计年度入职数据';
14   select count(empno) into v_1981 from emp where 
15   to_char(hiredate,'yyyy')='1981';
16   select count(empno) into v_1982 from emp where 
17   to_char(hiredate,'yyyy')='1982';
18   select count(empno) into v_1983 from emp where 
19   to_char(hiredate,'yyyy')='1983';
20   commit;
21   dbms_output.put_line('1981年入职人数:'||v_1981);
22   dbms_output.put_line('1982年入职人数:'||v_1982);
23   dbms_output.put_line('1983年入职人数:'||v_1983);
24   end;      
25 
26 
27   --不同块中的异常作用域级别
28   declare
29        e_outerexception exception;
30        begin
31        declare  
32          e_innerexception exception;
33         begin 
34           raise e_innerexception;
35           raise e_outerexception;
36           end;
37           --raise e_innerexception;        --在外层异常出发内存块异常时非法的 
38           --外层异常覆盖内层异常
39       exception raise e_outerexception; 
40          when others then
41               dbms_output.put_line('出现了错误'||'错误编号'
42               ||SQLCODE||'错误名称'||sqlerrm); 
43          end;
44 --exception_init使用示例   把Oracle系统异常关联到我们自定义异常
45 declare
46   e_missingnull exception;
47   pragma exception_init (e_missingnull,-1400);
48 begin
49   insert into emp(empno) values(null);
50   commit;
51 exception
52   when e_missingnull then
53     dbms_output.put_line('触发了ORA-1400错误!'||sqlerrm);  
54     rollback; 
55 end;

 

 
posted @ 2017-12-23 17:13  火柴天堂  阅读(1740)  评论(0编辑  收藏  举报