Oracle savepoint介紹

來源:互聯網
上載者:User

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)檢查點在事務提交後就不存在了,這點可以從樣本中看出。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.