2014-10-05 48 views
-1

如何使過程或函數返回SELECT的結果集,就像我直接進行SELECT操作一樣?返回表的Oracle過程或函數

+2

做一些流水線功能的研究,有一個去回發任何問題。 – 2014-10-05 01:28:19

+0

是否可以使用某種動態表類型?我想爲這個函數的輸入設置不同的SELECT。 – trob 2014-10-05 01:52:11

+0

您使用的是哪個版本的Oracle? – 2014-10-05 03:21:47

回答

0

因爲,

  1. 功能:創建一個pipelined功能,並使用它作爲表函數。

實施例:

-- 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

  • 步驟:SYS_REFCURSOR作爲OUT參數。
  • +0

    假設你的Oracle版本支持'pipeline'函數和'sys_refcursor'。 – 2014-10-05 15:45:32