阅读下面的PL/SQL程序块:
BEGIN
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(35000,'WANG','FRED');
SAVEPOINT SAVE_A;
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(40000,'WOO','DAVID');
SAVEPOINT SAVE_B;
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(50000,'LDD','FRIK');
SAVEPOINT SAVE_C;
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(45000,'LHR','DAVID');
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(25000,'LEE','BERT');
ROLLBACK TO SAVEPOINT SAVE_C;
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(32000,'CHUNG','MIKE');
ROLLBACK TO SAVEPOINT SAVE_B;
COMMIT;
END;
运行上面的程序,下面选项中哪几个更改永久保存到数据库()(多选题)
A、INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(50000,'LDD','FRIK');
B、INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(32000,'CHUNG','MIKE');
C、INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(35000,'WANG','FRED');
D、INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(40000,'WOO','DAVID');
答案:C、D。
保存点(SAVEPOINT)是事务处理过程中的一个标志,与回滚命令(ROLLBACK)结合使用。其主要用途是允许用户将某一段处理进行回滚而不必回滚整个事务。
1)执行SAVEPOINT SAVE_A的时候创建了一个保存点SAVE_A;
2)执行SAVEPOINT SAVE_B的时候创建了一个保存点SAVE_B;
3)执行SAVEPOINT SAVE_C的时候创建了一个保存点SAVE_C;
4)在执行ROLLBACK TO SAVEPOINT SAVE_C后,SAVEPOINT SAVE_C到当前语句之间所有的操作都被回滚;也就是说回滚到了3)的状态;
5)在执行ROLLBACK TO SAVEPOINT SAVE_B后,SAVEPOINT SAVE_B到当前语句之间所有的操作都被回滚;也就是说回滚到了2)的状态;
6)在执行COMMIT后,只有SAVEPOINT SAVE_B之前的操作会被提交从而永久保存到数据库。
使用程序验证以上的分析,如下所示:
SYS@lhrdb> create table EMPLOYEE
(
first_name VARCHAR2(20),
last_name VARCHAR2(25),
salary NUMBER(8,2)
);
Table created.
SYS@lhrdb> BEGIN
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(35000,'WANG','FRED');
SAVEPOINT SAVE_A;
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(40000,'WOO','DAVID');
SAVEPOINT SAVE_B;
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(50000,'LDD','FRIK');
SAVEPOINT SAVE_C;
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(45000,'LHR','DAVID');
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(25000,'LEE','BERT');
ROLLBACK TO SAVEPOINT SAVE_C;
INSERT INTO EMPLOYEE(SALARY,LAST_NAME,FIRST_NAME) VALUES(32000,'CHUNG','MIKE');
ROLLBACK TO SAVEPOINT SAVE_B;
COMMIT;
END;
/
PL/SQL procedure successfully completed.
SYS@lhrdb> SELECT * FROM EMPLOYEE;
FIRST_NAME LAST_NAME SALARY
-------------------- ------------------------- ----------
FRED WANG 35000
DAVID WOO 40000
综上分析,题目中的程序块在执行完毕后,只有SAVEPOINT SAVE_B之前的操作会被提交从而永久保存到数据库,显然,C和D选项正确。
所以,本题的答案为C、D。