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;