zoukankan      html  css  js  c++  java
  • Oracle中函数如何返回结果集

    Oracle中,用函数返回结果集有时候要用到,下面是demo:

    1
    2
    3
    4
    5
    6
    7
    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.用数组的方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    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.用管道函数 

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    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
  • 相关阅读:
    随机-byte编码
    dataframe骚操作,待续
    oracle中的rowid
    java提高篇-----理解java的三大特性之继承
    staruml使用教程
    黑马程序员:HTML习题1
    Cocos2d-x-->CCSprite 动画
    地址栏传参中文乱码详解
    Qt学习第二天
    Lync 2010 升级到2013 之部署2013前端服务器!
  • 原文地址:https://www.cnblogs.com/fery/p/5608018.html
Copyright © 2011-2022 走看看