oracle pipeline實驗

perfychi發表於2014-05-27

(一)
--建立行級型別
create or replace type test_type as object(user_id int, username varchar2(30),  age int);
--建立表級型別
create or replace type test_list is table of test_type;

--建立測試表並插入測試資料
create table test(id int ,name varchar(30) , age int, sex int);

insert into test values(1, 'tom1', 10, 1);
insert into test values(2, 'tom2', 20, 0);
insert into test values(3, 'tom3', 30, 1);

--編寫帶pipelined的儲存過程
create or replace function test_func(v_id int)  return test_list pipelined
as
v_test test_type;
begin
  for i in  (select * from test where id=v_id ) loop
      v_test :=test_type(i.id,i.name,i.age);
      pipe row(v_test);
  end loop;
return;
end;

--輸出結果:
select test_func(1) from dual;

(二)
create type lookup_row as   object( idx number, text varchar2(20) );
  create type lookups_tab as table of lookup_row;
  create or replace function Lookups_Fn  return lookups_tab pipelined
  is
  v_row lookup_row;
  begin
  for j in 1..10
  loop
  v_row :=
  case j
  when 1 then lookup_row ( 1, 'one' )
  when 2 then lookup_row ( 2, 'TWO' )
  when 3 then lookup_row ( 3, 'three' )
  when 4 then lookup_row ( 4, 'FOUR' )
  when 5 then lookup_row ( 5, 'five' )
  when 6 then lookup_row ( 6, 'SIX' )
  when 7 then lookup_row ( 7, 'seven' )
  else        lookup_row ( j, 'other' )
  end;
  pipe row ( v_row );

  end loop;
  return;
  end Lookups_Fn;

  很多人都知道,在普通的函式中,使用dbms_output輸出的資訊,需要在伺服器執行完整個函式後一次性的返回給客戶端。但你如果需要在客戶端實時的輸出函式執行過程中的一些資訊,在Oracle 9i以後則可以使用管道函式(pipeline function)。


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


  create or replace type MsgType as table of varchar2(4000);
  /

  create or replace function f_pipeline_test   return MsgTypeb   PIPELINED
  as
  begin
  for i in 1 .. 10
  loop
 pipe row( 'Iteration ' || i || ' at ' || systimestamp );
  dbms_lock.sleep(1);
 end loop;
  pipe row( 'All done!' );
  return;
  end;
  /


  在sql*plus中執行該函式,大家需要首先設定arraysize為1,否則伺服器會按照預設的15來向客戶端返回資訊,這會影響我們的測試效果。


  SQL> set arraysize 1


  SQL> select * from table( f_pipeline_test );


  COLUMN_VALUE


  -----------------------------------------------------


  Iteration 1 at 14-FEB-08 02.13.18.273988000 PM +08:00


  Iteration 2 at 14-FEB-08 02.13.19.275988000 PM +08:00


  Iteration 3 at 14-FEB-08 02.13.20.277767000 PM +08:00


  Iteration 4 at 14-FEB-08 02.13.21.279591000 PM +08:00


  Iteration 5 at 14-FEB-08 02.13.22.281366000 PM +08:00


  Iteration 6 at 14-FEB-08 02.13.23.283189000 PM +08:00


  Iteration 7 at 14-FEB-08 02.13.24.283965000 PM +08:00


  Iteration 8 at 14-FEB-08 02.13.25.285785000 PM +08:00


  Iteration 9 at 14-FEB-08 02.13.26.286570000 PM +08:00


  Iteration 10 at 14-FEB-08 02.13.27.288387000 PM +08:00


  All done!


  11 rows selected.

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


  create or replace function f_pipeline_testdml
  return MsgType
  PIPELINED
  as
  begin
  for i in 1 .. 10
  loop
  insert into test values(1);
  pipe row( 'insert into test values( ' || i || ') success at ' || systimestamp );
  dbms_lock.sleep(1);
  end loop;
  pipe row( 'All done!' );
  return;
  end;
  /


  SQL>  select * from table( f_pipeline_testdml );


  select * from table( f_pipeline_testdml )


  *


  ERROR at line 1:


  ORA-14551: cannot perform a DML operation inside a query


  ORA-06512: at "NING.F_PIPELINE_TESTDML", line 8


  create or replace function f_pipeline_testdml
  return MsgType
  PIPELINED
 as
  pragma autonomous_transaction;
  begin
  for i in 1 .. 10
  loop
  insert into test values(1);
  commit;
  pipe row( 'insert values ' || i || ' success at ' || systimestamp );
  dbms_lock.sleep(1);
  end loop;
  pipe row( 'All done!' );
  return;
  end;


  /


  SQL> select * from table( f_pipeline_testdml );


  COLUMN_VALUE


  --------------------------------------------------------------------------------


  insert values  1  success at 14-FEB-08 02.16.47.855158000 PM +08:00


  insert values  2  success at 14-FEB-08 02.16.48.865559000 PM +08:00


  insert values  3  success at 14-FEB-08 02.16.49.867377000 PM +08:00


  insert values  4  success at 14-FEB-08 02.16.50.873154000 PM +08:00


  insert values  5  success at 14-FEB-08 02.16.51.874942000 PM +08:00


  insert values  6  success at 14-FEB-08 02.16.52.880781000 PM +08:00


  insert values  7  success at 14-FEB-08 02.16.53.882543000 PM +08:00


  insert values  8  success at 14-FEB-08 02.16.54.894348000 PM +08:00


  insert values  9  success at 14-FEB-08 02.16.55.896153000 PM +08:00


  insert values  10 success at 14-FEB-08 02.16.56.901904000 PM +08:00


  All done!


  11 rows selected.


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


  create or replace function f_pipeline_testdml
  return MsgType
  PIPELINED
  as
  pragma autonomous_transaction;
  begin
  for i in 1 .. 10
  loop
  insert into test values(1);
  pipe row( 'insert values ' || i || ' success at ' || systimestamp );
  dbms_lock.sleep(1);
  end loop;
  pipe row( 'All done!' );
  commit;
  return;
  end;
  /


  SQL> select * from table( f_pipeline_testdml );


  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;


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27042095/viewspace-1171221/,如需轉載,請註明出處,否則將追究法律責任。

相關文章