Oracle 中,函數如何返回結果集,oracle返回

來源:互聯網
上載者:User

Oracle 中,函數如何返回結果集,oracle返回

  在Oracle中,用函數返回結果集有時候要用到,下面是demo:

create or replace type t_test as object
(
  id integer,
  create_time date,
  object_name varchar2(60)
);
create or replace type t_test_table as table of t_test;

1.用數組的方式
create or replace function f_test(n in number default null)
  return t_test_table as
  v_test t_test_table   := t_test_table();
begin
  for i in 1 .. n loop
    v_test.extend();
    v_test(v_test.count) := t_test(i, sysdate, 'name' || i);
  end loop;
  return v_test;
end f_test;
/

SQL> select * from table(f_test(5));
        ID CREATE_TIME    OBJECT_NAME
   -------- -------------- -------------
         1 07-4月 -15     name1
         2 07-4月 -15     name2
         3 07-4月 -15     name3
         4 07-4月 -15     name4
         5 07-4月 -15     name5

2.用管道函數
create or replace function f_test_pipe(n in number default null)
  return t_test_table
  PIPELINED as
  v_test t_test_table := t_test_table();
begin
  for i in 1 .. nvl(n, 100) loop
    pipe row(t_test(i, sysdate, 'name' || i));
  end loop;
  return;
end f_test_pipe;
/


SQL> select * from table(f_test_pipe(5));
        ID CREATE_TIME    OBJECT_NAME
---------- -------------- ----------------
         1 07-4月 -15     mc1
         2 07-4月 -15     mc2
         3 07-4月 -15     mc3
         4 07-4月 -15     mc4
         5 07-4月 -15     mc5

相關文章

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.