2017-01-17 52 views
-1

我想在一個簡單的例子中應用表(函數)功能。我用從過程的SELECT,表(功能)方法時,收到以下錯誤WHERE語句Oracle PL/SQL:表(函數)錯誤

Error: PLS-00231: function 'GET_NUM' may not be used in SQL

對什麼是錯在這段代碼的任何見解。

CREATE OR REPLACE PACKAGE BODY TEST IS 
--COLLECTION DELCARATION 
TYPE num_list IS TABLE OF NUMBER; 
TYPE str_list IS TABLE OF NUMBER; 

FUNCTION get_num(id IN NUMBER) 
    RETURN num_list AS 
    num num_list; 
BEGIN 
    WITH xTable AS 
    ( 
     SELECT 1 ID, 111 NUM, 'ABC 111' DESCP FROM DUAL 
     UNION ALL 
     SELECT 2 ID, 222 NUM, 'ABC 222' DESCP FROM DUAL 
     UNION ALL 
     SELECT 1 ID, 333 NUM, 'ABC 333' DESCP FROM DUAL     
    ) 

    SELECT x.NUM 
    BULK COLLECT INTO num 
    FROM xTable x 
    WHERE x.ID = id; 

    RETURN num; 

END; 

PROCEDURE insert_object_id_control AS 
    i NUMBER:= 1; 
    str str_list; 
BEGIN 
    WITH yTable AS 
    ( 
     SELECT 1 ID, 111 NUM, 'ABC 111' DESCP FROM DUAL 
     UNION ALL 
     SELECT 2 ID, 222 NUM, 'ABC 222' DESCP FROM DUAL 
     UNION ALL 
     SELECT 3 ID, 333 NUM, 'ABC 333' DESCP FROM DUAL         
    ) 

    SELECT y.DESCP 
    INTO str /*BULL COLLECT INTO GET'S SAME RESULT*/ 
    FROM yTable y 
    WHERE y.NUM IN (SELECT * FROM TABLE(get_num(i))); 

    END insert_object_id_control;  
END TEST; 
+1

在此過程中'insert_object_id_control'你沒有INTO運行選擇。這不能編譯。如果您的軟件包已編譯,代碼與您發佈的代碼不同 – Aleksej

+1

是不是同一個問題http://stackoverflow.com/questions/41680033/oracle-pl-sql-function-cursor/41695029?noredirect=1#評論70587650_41695029 ...我已經在這裏提到過它不會起作用,但是如果你不接受任何東西,你就會接受錯誤的答案。 – XING

+0

此外,你甚至沒有按照提到那裏'select * from table(get_num(1))'應該從表中選擇column_value(get_num(1))' – XING

回答

1

您的代碼在程序insert_object_id_control中有一些編譯問題;鑑於這個過程對測試沒有意義,我已經刪除了它。

如果你想使用一個類型作爲函數的結果,並在查詢中使用這個函數,你需要一個存儲類型。

例如:

CREATE TYPE num_list IS TABLE OF NUMBER; 

CREATE OR REPLACE PACKAGE TEST IS 
    FUNCTION get_num(id IN NUMBER) 
     RETURN num_list; 
END TEST; 

CREATE OR REPLACE PACKAGE BODY TEST IS 
    FUNCTION get_num(id IN NUMBER) 
     RETURN num_list AS 
     num          num_list; 
    BEGIN 
     WITH xTable AS 
       (SELECT 1 ID, 111 NUM, 'ABC 111' DESCP FROM DUAL UNION ALL 
        SELECT 2 ID, 222 NUM, 'ABC 222' DESCP FROM DUAL UNION ALL 
        SELECT 1 ID, 333 NUM, 'ABC 333' DESCP FROM DUAL) 
     SELECT x.NUM 
      BULK COLLECT INTO num 
      FROM xTable x 
     WHERE x.ID = id; 

     RETURN num; 
    END; 
END TEST; 

惠特這些對象,此查詢將工作:

SQL> SELECT test.get_num(1) FROM DUAL; 

TEST.GET_NUM(1) 
------------------------------------------ 
NUM_LIST(111, 222, 333)