返回表的Oracle过程或函数

时间:2014-10-05 01:15:17

标签: oracle plsql

如何创建一个返回SELECT结果集的过程或函数,就像我直接进行SELECT一样?

1 个答案:

答案 0 :(得分:0)

有关,

  1. 功能:创建pipelined函数并将其用作表函数。
  2. 示例:

    -- Create the types to support the table function.
    DROP TYPE t_tf_tab;
    DROP TYPE t_tf_row;
    
    CREATE TYPE t_tf_row AS OBJECT (
      id           NUMBER,
      description  VARCHAR2(50)
    );
    /
    
    CREATE TYPE t_tf_tab IS TABLE OF t_tf_row;
    /
    
    -- Build the table function itself.
    CREATE OR REPLACE FUNCTION get_tab_tf (p_rows IN NUMBER) RETURN t_tf_tab AS
      l_tab  t_tf_tab := t_tf_tab();
    BEGIN
      FOR i IN 1 .. p_rows LOOP
        l_tab.extend;
        l_tab(l_tab.last) := t_tf_row(i, 'Description for ' || i);
      END LOOP;
    
      RETURN l_tab;
    END;
    /
    
    -- Test it.
    SELECT *
    FROM   TABLE(get_tab_tf(10))
    ORDER BY id DESC;
    
            ID DESCRIPTION
    ---------- --------------------------------------------------
            10 Description for 10
             9 Description for 9
             8 Description for 8
             7 Description for 7
             6 Description for 6
             5 Description for 5
             4 Description for 4
             3 Description for 3
             2 Description for 2
             1 Description for 1
    
    10 rows selected.
    

    Example Source

    1. 过程:SYS_REFCURSOROUT参数。