Oracle PLSQL之cursor取得是open時的資料

來源:互聯網
上載者:User

當我們定義了一個很複雜的cursor,這個cursor需要執行很長時間,在這個cursor執行的過程中,其它使用者又修改了該cursor所引用的表的資料時,cursor得到的是修改前還是修改後的資料呢? 

答案是cursor得到的始終是cursor在open時的資料,接下來我們將通過一個小實驗來驗證。

首先,session1執行以下匿名塊,該匿名塊通過cursor取得t1表的所有資料,不過在open cursor後將暫停30秒,在這30秒中我們將在session2中刪除t1表的所有資料: 

  1. DECLARE  
  2.   CURSOR c IS  
  3.     SELECT deptno  
  4.           ,dname  
  5.           ,loc  
  6.       FROM t1;  
  7.   TYPE dept_tab IS TABLE OF c%ROWTYPE;  
  8.   l_depts dept_tab;  
  9. BEGIN  
  10.   dbms_output.put_line('opening c: ' ||  
  11.                        to_char(SYSDATE, 'yyyy-mm-dd hh24:mi:ss'));  
  12.   OPEN c;  
  13.   dbms_lock.sleep(30);  
  14.   dbms_output.put_line('after sleep: ' ||  
  15.                        to_char(SYSDATE, 'yyyy-mm-dd hh24:mi:ss'));  
  16.   
  17.   FETCH c BULK COLLECT  
  18.     INTO l_depts;  
  19.   CLOSE c;  
  20.   
  21.   FOR i IN l_depts.FIRST .. l_depts.LAST  
  22.   LOOP  
  23.     dbms_output.put_line(l_depts(i).deptno || ', ' || l_depts(i)  
  24.                          .dname || ', ' || l_depts(i).loc);  
  25.   END LOOP;  
  26. END;  
  27.   
  28. 第二步,session2執行以下語句:  
  29. 22:35:21 SQL> select * from t1;  
  30.    
  31. DEPTNO DNAME          LOC  
  32. ------ -------------- -------------   
  33.     10 ACCOUNTING     NEW YORK  
  34.     20 RESEARCH       DALLAS  
  35.     30 SALES          CHICAGO  
  36.     40 OPERATIONS     BOSTON  
  37.    
  38. 22:35:29 SQL> delete from t1;  
  39.    
  40. 4 rows deleted  
  41.    
  42. 22:35:33 SQL> commit;  
  43.    
  44. Commit complete  
  45.    
  46. 22:35:35 SQL> select * from t1;  
  47.    
  48. DEPTNO DNAME          LOC  
  49. ------ -------------- -------------   
  50.    
  51. 22:35:38 SQL>   
  52.   
  53. 最後,觀察session1的輸出:  
  54. opening c: 2011-10-26 22:35:25  
  55. after sleep: 2011-10-26 22:35:55  
  56. 10, ACCOUNTING, NEW YORK  
  57. 20, RESEARCH, DALLAS  
  58. 30, SALES, CHICAGO  
  59. 40, OPERATIONS, BOSTON  
  60.   
  61. 由於在22:35:25我們就已經開啟了遊標,所以結果依然能輸出t1表的所有資料,儘管在22:35:35之前我們已經刪除了t1表的所有資料並提交,而cursor取資料(fetch)發生在22:35:55之後。  
  62.   
  63. ref:  
  64. The OPEN statement executes the query associated with a cursor. It allocates database resources to process the query and identifies the result set -- the rows that match the query conditions.   
  65. http://download.Oracle.com/docs/cd/B19306_01/appdev.102/b14261/open_statement.htm#i35173  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.