Oracle PL / SQL:表(函数)错误

时间:2017-01-17 20:43:59

标签: sql oracle plsql

我试图在一个简单的例子中应用表(功能)功能。从Procedure的SELECT,WHERE语句中使用Table(function)方法时出现以下错误

  

错误:PLS-00231:功能' GET_NUM'可能不会在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 个答案:

答案 0 :(得分: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)