日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
oracle管道函數(shù)的用法

oracle管道函數(shù)是一類特殊的函數(shù),oracle管道函數(shù)返回值類型必須為集合,下面就為您將介紹oracle管道函數(shù)的語法,供您參考學習。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供坡頭企業(yè)網(wǎng)站建設,專注與成都網(wǎng)站設計、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設、HTML5建站、小程序制作等業(yè)務。10年已為坡頭眾多企業(yè)、政府機構等服務。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進行中。

在普通的函數(shù)中,使用dbms_output輸出的信息,需要在服務器執(zhí)行完整個函數(shù)后一次性的返回給客戶端。如果需要在客戶端實時的輸出函數(shù)執(zhí)行過程中的一些信息,在oracle9i以后可以使用管道函數(shù)(pipeline function)。

關鍵字PIPELINED表明這是一個oracle管道函數(shù),oracle管道函數(shù)的返回值類型必須為集合,在函數(shù)中,PIPE ROW語句被用來返回該集合的單個元素,函數(shù)以一個空的RETURN 語句結束,以表明它已經(jīng)完成。

 
 
 
  1. create or replace type MsgType as table of varchar2(4000);  
  2. /  
  3.  
  4. create or replace function f_pipeline_test  
  5. return MsgType  
  6. PIPELINED  
  7. as  
  8. begin  
  9.    for i in 1 .. 10  
  10.    loop  
  11.    pipe row( 'Iteration ' || i || ' at ' || systimestamp );  
  12.    dbms_lock.sleep(1);  
  13.    end loop;  
  14.    pipe row( 'All done!' );  
  15.    return;  
  16. end;  
  17. /  
  18.  

在sql*plus中執(zhí)行該函數(shù),首先設置arraysize為1,否則服務器會按照默認的15來向客戶端返回信息,這會影響我們的測試效果。

 
 
 
  1. SQL> set arraysize 1  
  2. SQL> select * from table( f_pipeline_test );  
  3.  
  4. COLUMN_VALUE  
  5. --------------------------------------------------------------------------------  
  6. Iteration 1 at 14-FEB-08 02.13.18.273988000 PM +08:00  
  7. Iteration 2 at 14-FEB-08 02.13.19.275988000 PM +08:00  
  8. Iteration 3 at 14-FEB-08 02.13.20.277767000 PM +08:00  
  9. Iteration 4 at 14-FEB-08 02.13.21.279591000 PM +08:00  
  10. Iteration 5 at 14-FEB-08 02.13.22.281366000 PM +08:00  
  11. Iteration 6 at 14-FEB-08 02.13.23.283189000 PM +08:00  
  12. Iteration 7 at 14-FEB-08 02.13.24.283965000 PM +08:00  
  13. Iteration 8 at 14-FEB-08 02.13.25.285785000 PM +08:00  
  14. Iteration 9 at 14-FEB-08 02.13.26.286570000 PM +08:00  
  15. Iteration 10 at 14-FEB-08 02.13.27.288387000 PM +08:00  
  16. All done!  
  17.  
  18. 11 rows selected.  
  19.  

如果要在pipeline中執(zhí)行DML操作,則必須使用自治事務,否則會報ORA-14551錯誤

 
 
 
  1. create or replace function f_pipeline_testdml  
  2. return MsgType  
  3. PIPELINED  
  4. as  
  5. begin  
  6.    for i in 1 .. 10  
  7.    loop  
  8.    insert into test values(1);  
  9.    pipe row( 'insert into test values( ' || i || ') success at ' || systimestamp );  
  10.    dbms_lock.sleep(1);  
  11.    end loop;  
  12.    pipe row( 'All done!' );  
  13.    return;  
  14. end;  
  15. /  
  16.  
 
 
 
  1. SQL> select * from table( f_pipeline_testdml );  
  2. select * from table( f_pipeline_testdml ) 
 
 
 
  1.  *  
  2. ERROR at line 1:  
  3. ORA-14551: cannot perform a DML operation inside a query  
  4. ORA-06512: at "NING.F_PIPELINE_TESTDML", line 8create or replace function f_pipeline_testdml  
  5. return MsgType  
  6. PIPELINED  
  7. as  
  8. pragma autonomous_transaction;  
  9. begin  
  10.    for i in 1 .. 10  
  11.    loop  
  12.    insert into test values(1);  
  13.    commit;  
  14.    pipe row( 'insert values ' || i || ' success at ' || systimestamp );  
  15.    dbms_lock.sleep(1);  
  16.    end loop;  
  17.    pipe row( 'All done!' );  
  18.    return;  
  19. end;  
  20. /  
  21.  
  22. SQL> select * from table( f_pipeline_testdml );  
  23.  
  24. COLUMN_VALUE  
  25. --------------------------------------------------------------------------------  
  26. insert values 1 success at 14-FEB-08 02.16.47.855158000 PM +08:00  
  27. insert values 2 success at 14-FEB-08 02.16.48.865559000 PM +08:00  
  28. insert values 3 success at 14-FEB-08 02.16.49.867377000 PM +08:00  
  29. insert values 4 success at 14-FEB-08 02.16.50.873154000 PM +08:00  
  30. insert values 5 success at 14-FEB-08 02.16.51.874942000 PM +08:00  
  31. insert values 6 success at 14-FEB-08 02.16.52.880781000 PM +08:00  
  32. insert values 7 success at 14-FEB-08 02.16.53.882543000 PM +08:00  
  33. insert values 8 success at 14-FEB-08 02.16.54.894348000 PM +08:00  
  34. insert values 9 success at 14-FEB-08 02.16.55.896153000 PM +08:00  
  35. insert values 10 success at 14-FEB-08 02.16.56.901904000 PM +08:00  
  36. All done!  
  37.  
  38. 11 rows selected.  

在oracle9205及其之后的版本中,在pipeline function中使用自治事務,則必須在pipe row之前提交或者回滾事務,否則會報ORA-06519錯誤

 
 
 
  1. create or replace function f_pipeline_testdml  
  2. return MsgType  
  3. PIPELINED  
  4. as  
  5. pragma autonomous_transaction;  
  6. begin  
  7.    for i in 1 .. 10  
  8.    loop  
  9.    insert into test values(1);  
  10.    pipe row( 'insert values ' || i || ' success at ' || systimestamp );  
  11.    dbms_lock.sleep(1);  
  12.    end loop;  
  13.    pipe row( 'All done!' );  
  14.    commit;  
  15.    return;  
  16. end;  
  17. /  
  18.  
  19. SQL> select * from table( f_pipeline_testdml );  
  20. select * from table( f_pipeline_testdml )  

  *
ERROR at line 1:
ORA-06519: active autonomous transaction detected and rolled back
ORA-06512: at "NING.F_PIPELINE_TESTDML", line 10
這是由于在9205中修復Bug 2711518導致了自治事務的行為有所改變。如果系統(tǒng)從9205之前的版本 升級到之后的版本,需要保證pipeline function的行為和以前版本一致,oracle提供了一個10946事件來設置和以前版本的兼容性,如果在管道函數(shù)中使用了select for update的cursor,則必須設置event回歸以前的特性,否則即使在pipe row之前commit也會導致ORA-1002錯誤。

ALTER SYSTEM SET EVENT = "10946 trace name context forever, level 8" scope=spfile;

【編輯推薦】

oracle自定義函數(shù)的使用

計算時間差的Oracle函數(shù)

Oracle日期函數(shù)簡介

創(chuàng)建Oracle包的語法

Oracle TRIM函數(shù)語法介紹


本文名稱:oracle管道函數(shù)的用法
轉載來于:http://www.dlmjj.cn/article/dphiohe.html