SAVEPOINT 由官方文檔提供http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_10001.htm#SQLRF01701
1.目的:
Use the SAVEPOINT statement to identify a point in a transaction to which you can later roll back.
使用檢查點語句標識一個事務點以便在後面可以復原。
2. 預備知識:
無。
3.文法:
SAVEPOINT savepoint ;
4.下面來看樣本:
1)首先看一下員工表中Banda的工資
SQL> select employee_id, last_name, salary from employees where last_name='Banda';
EMPLOYEE_ID LAST_NAME SALARY
----------- ------------------------- ----------
167 Banda 6200
2)我們來修改一下Banda的工資並建立檢查點banda_sal:
SQL> update employees
2 set salary = 7000
3 where last_name ='Banda';
已更新 1 行。
SQL> savepoint banda_sal;
儲存點已建立。
3)再對Banda的工資做一次修改並建立檢查點banda_sal2:
SQL> update employees
2 set salary = 8000
3 where last_name ='Banda';
已更新 1 行。
SQL> savepoint banda_sal2;
儲存點已建立。
SQL> select employee_id, last_name, salary from employees where last_name='Banda';
EMPLOYEE_ID LAST_NAME SALARY
----------- ------------------------- ----------
167 Banda 8000
4)下面進行後援動作,使回退到檢查點banda_sal:
SQL> rollback to savepoint banda_sal;
回退已完成。
SQL> select employee_id, last_name, salary from employees where last_name='Banda';
EMPLOYEE_ID LAST_NAME SALARY
----------- ------------------------- ----------
167 Banda 7000
5)下面進行提交操作,再進行後援動作:
SQL> commit;
提交完成。
SQL> rollback to savepoint banda_sal2;
rollback to savepoint banda_sal2
*
第 1 行出現錯誤:
ORA-01086: 從未建立儲存點 'BANDA_SAL2'
5. 總結:
1)通過建立檢查點我們可以在事務提交前回退到任意已建立檢查點的事務。
2)檢查點在事務提交後就不存在了,這點可以從樣本中看出。