首页 技术 正文
技术 2022年11月15日
0 收藏 835 点赞 4,562 浏览 4941 个字

1.sqlplus 客户端正常退出

SQL> desc t;
名称 是否为空? 类型
----------------------------------------- -------- ----------------------------
TSP TIMESTAMP(6)
CNT NOT NULL NUMBER(38)SQL> insert into t values(sysdate,1);已创建 1 行。SQL> exit
从 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 断开C:\Users\zen>sqlplus hbkf_crm/hbkf_crm4testSQL*Plus: Release 11.2.0.1.0 Production on 星期日 11月 17 12:28:29 2013Copyright (c) 1982, 2010, Oracle. All rights reserved.连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing optionsSQL> select * from t;TSP
---------------------------------------------------------------------------
CNT
----------
17-11月-13 12.27.58.000000 下午
1SQL>

语句正常执行sqlplus正常退出,sqlplus commit修改。

SQL> insert into t values(sysdate,2);已创建 1 行。SQL> insert into t values(sysdate,2);
insert into t values(sysdate,2)
*
第 1 行出现错误:
ORA-00001: 违反唯一约束条件 (HBKF_CRM.SYS_C0011518)SQL> exit
从 Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options 断开C:\Users\zen>sqlplus hbkf_crm/hbkf_crm4testSQL*Plus: Release 11.2.0.1.0 Production on 星期日 11月 17 12:33:35 2013Copyright (c) 1982, 2010, Oracle. All rights reserved.连接到:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing optionsSQL> select * from t;TSP
---------------------------------------------------------------------------
CNT
----------
17-11月-13 12.27.58.000000 下午
117-11月-13 12.33.19.000000 下午
2SQL>

这里应该是显示的语句级原子性。

SQL> select name,text from user_source t where t.name ='P1';NAME  TEXT
----- ------------------------------------------------------------
P1 procedure p1(num INT)
P1 IS
P1
P1 BEGIN
P1
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+1);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+2);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+2);
P1 COMMIT;
P1NAME TEXT
----- ------------------------------------------------------------
P1 end p1;已选择12行。SQL> exec p1(6);
BEGIN p1(6); END;*
第 1 行出现错误:
ORA-00001: 违反唯一约束条件 (HBKF_CRM.SYS_C0011518)
ORA-06512: 在 "HBKF_CRM.P1", line 9
ORA-06512: 在 line 1SQL> select * from t;TSP
-------------------------------------------------------------------------
CNT
----------
17-11月-13 12.27.58.000000 下午
117-11月-13 12.49.08.000000 下午
317-11月-13 12.33.19.000000 下午
2TSP
-------------------------------------------------------------------------
CNT
----------
17-11月-13 12.46.55.000000 下午
417-11月-13 12.47.23.000000 下午
5SQL> select name,text from user_source t where t.name ='P1';NAME TEXT
----- ------------------------------------------------------------
P1 procedure p1(num INT)
P1 IS
P1
P1 BEGIN
P1
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+1);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+2);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+2);
P1 COMMIT;
P1 EXCEPTION WHEN OTHERS THENNAME TEXT
----- ------------------------------------------------------------
P1 dbms_output.put_line(sqlcode||':'||SQLERRM);
P1
P1 end p1;已选择14行。SQL> exec p1(6);PL/SQL 过程已成功完成。
SQL> select tsp,cnt from t;TSP CNT
---------------------------------- ----------
17-11月-13 01.00.20.000000 下午 6
17-11月-13 01.00.20.000000 下午 7
17-11月-13 01.00.20.000000 下午 8
17-11月-13 12.27.58.000000 下午 1
17-11月-13 12.49.08.000000 下午 3
17-11月-13 12.33.19.000000 下午 2
17-11月-13 12.46.55.000000 下午 4
17-11月-13 12.47.23.000000 下午 5已选择8行。SQL>

可以看出TOM大师为何对 when others 语句如此有看法了SQL> select name,text from user_source t where t.name =’P1′;


NAME TEXT
----- ------------------------------------------------------------
P1 procedure p1(num INT)
P1 IS
P1
P1 BEGIN
P1
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+1);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+2);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+2);
P1 COMMIT;
P1 EXCEPTION WHEN OTHERS THENNAME TEXT
----- ------------------------------------------------------------
P1 dbms_output.put_line(sqlcode||':'||SQLERRM);
P1 ROLLBACK;
P1 end p1;已选择14行。SQL> exec p1(10);PL/SQL 过程已成功完成。SQL> select * from t;TSP CNT
---------------------------------- ----------
17-11月-13 12.27.58.000000 下午 1
17-11月-13 12.49.08.000000 下午 3
17-11月-13 12.33.19.000000 下午 2
17-11月-13 12.46.55.000000 下午 4
17-11月-13 12.47.23.000000 下午 5--加上rollback 可以回滚整个事务。
SQL> select name,text from user_source t where t.name ='P1';NAME  TEXT
----- ------------------------------------------------------------
P1 procedure p1(num INT)
P1 IS
P1
P1 BEGIN
P1
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+1);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+2);
P1 INSERT INTO t(tsp,cnt) VALUES(sysdate,num+2);
P1 COMMIT;
P1 EXCEPTION WHEN OTHERS THENNAME TEXT
----- ------------------------------------------------------------
P1 dbms_output.put_line(sqlcode||':'||SQLERRM);
P1 --ROLLBACK;
P1 RAISE;
P1 end p1;已选择15行。
SQL> exec p1(10);
BEGIN p1(10); END;*
第 1 行出现错误:
ORA-00001: 违反唯一约束条件 (HBKF_CRM.SYS_C0011518)
ORA-06512: 在 "HBKF_CRM.P1", line 14
ORA-06512: 在 line 1SQL> select * from t;TSP CNT
---------------------------------- ----------
17-11月-13 12.27.58.000000 下午 1
17-11月-13 12.49.08.000000 下午 3
17-11月-13 12.33.19.000000 下午 2
17-11月-13 12.46.55.000000 下午 4
17-11月-13 12.47.23.000000 下午
5--抛出异常也会使事务回滚。

一个好的习惯是捕获到了错误,要么回滚,要么抛出异常。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,494
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,295