转自http://blogold.chinaunix.net/u3/107027/showart_2308954.html
在SQL*PLUS下执行一个脚本 ,里面是INSERT 语句,如果遇到主键重复错误,后面的SQL语句是否还继续执行??
后面的SQL语句能否执行,是可以通过SQLPLUS的设置的。
默认是不退出 ,继续执行下面的SQL语句。
C:\>sqlplus scott/tiger@test
SQL*Plus: Release 11.2.0.1.0 Production on 星期二 8月 24 17:57:38 2010
Copyright (c) 1982, 2010, Oracle. All rights reserved.
连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining Scoring Engine
and Real Application Testing options
SQL> host type test.sql
insert into dept(deptno) values(10);
insert into dept(deptno) values(50);
SQL> select * from dept;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> @test.sql
insert into dept(deptno) values(10)
*
第 1 行出现错误:
ORA-00001: 违反唯一约束条件 (SCOTT.PK_DEPT)
已创建 1 行。
SQL> select * from dept;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
50
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> rollback;
回退已完成。
SQL> SELECT * FROM DEPT;
DEPTNO DNAME LOC
---------- ---------------------------- --------------------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON
SQL> WHENEVER SQLERROR EXIT
SQL> @test.sql
insert into dept(deptno) values(10)
*
第 1 行出现错误:
ORA-00001: 违反唯一约束条件 (SCOTT.PK_DEPT)
从 Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining Scoring Engine
and Real Application Testing options 断开
设置 了WHENEVER SQLERROR EXIT 那么当出现错误的时候,SQLPLUS将会退出。
默认是 WHENEVER SQLERROR CONTINUE
SQL> whenever --help
用法: WHENEVER SQLERROR
{ CONTINUE [ COMMIT | ROLLBACK | NONE ]
| EXIT [ SUCCESS | FAILURE | WARNING | n |
[ COMMIT | ROLLBACK ] }
用法: WHENEVER OSERROR
{ CONTINUE [ COMMIT | ROLLBACK | NONE ] | EXIT [ SUCCESS | FAILURE |
WARNING | n |
[ COMMIT | ROLLBACK ] }
这个选项还蛮多的,还可以设置whenever sqlerror continue rollback; 遇到错误回滚前面的。下面的还可以继续提交。类似
ROLLBACK TO SAVEPOINT