你好,游客 登录 注册 搜索
背景:
阅读新闻

Oracle 中函数如何返回结果集

[日期:2015-04-07] 来源:Linux社区  作者:stevendbaguo [字体:大 中 小]

在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

更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-04/115912.htm

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款