oracle管道函数的用法

数据库 Oracle
oracle数据库中的管道函数以一个空的RETURN 语句结束,以表明它已经完成。下文对oracle管道函数的用法作了详细的说明,供您参考。

oracle管道函数是一类特殊的函数,oracle管道函数返回值类型必须为集合,下面就为您将介绍oracle管道函数的语法,供您参考学习。

在普通的函数中,使用dbms_output输出的信息,需要在服务器执行完整个函数后一次性的返回给客户端。如果需要在客户端实时的输出函数执行过程中的一些信息,在oracle9i以后可以使用管道函数(pipeline function)。

关键字PIPELINED表明这是一个oracle管道函数,oracle管道函数的返回值类型必须为集合,在函数中,PIPE ROW语句被用来返回该集合的单个元素,函数以一个空的RETURN 语句结束,以表明它已经完成。

  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中执行该函数,首先设置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中执行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导致了自治事务的行为有所改变。如果系统从9205之前的版本 升级到之后的版本,需要保证pipeline function的行为和以前版本一致,oracle提供了一个10946事件来设置和以前版本的兼容性,如果在管道函数中使用了select for update的cursor,则必须设置event回归以前的特性,否则即使在pipe row之前commit也会导致ORA-1002错误。

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

 

 

【编辑推荐】

oracle自定义函数的使用

计算时间差的Oracle函数

Oracle日期函数简介

创建Oracle包的语法

Oracle TRIM函数语法介绍

责任编辑:段燃 来源: 互联网
相关推荐

2010-10-25 14:28:53

oracle trun

2010-04-27 12:51:49

Oracle 函数de

2010-05-10 18:22:28

Oracle deco

2010-04-30 17:58:55

Oracle trun

2010-04-30 10:47:26

Oracle Nvl函

2010-05-04 12:10:08

Oracle over

2010-10-25 16:13:31

Oracle to_d

2010-04-28 16:53:20

Oracle 函数

2010-10-25 17:22:24

oracle add_

2010-10-27 15:03:47

Oracle with

2010-04-29 16:06:47

Oracle rown

2011-03-16 09:42:27

Oracle临时表

2010-04-08 17:17:03

Oracle to_c

2010-04-02 15:22:02

Oracle join

2010-04-01 13:09:12

Oracle中join

2010-04-08 16:41:29

Oracle存储过程

2010-04-28 14:46:38

Oracle Copy

2010-04-16 16:41:53

rownum用法

2010-04-26 15:30:45

Oracle join

2010-04-28 16:30:52

Oracle case
点赞
收藏

51CTO技术栈公众号