试图从Oracle函数中获取表。获取不一致的数据类型

时间:2012-10-04 22:22:43

标签: oracle plsql stored-functions ora-00932

我正在尝试从Oracle中的存储函数返回查询。这个简单的尝试失败了:

CREATE OR REPLACE TYPE GetObjectsForFolderObjectType IS OBJECT (
       FOLDER_ID NUMBER(9), OBJECT_ID NUMBER(9)
);

CREATE OR REPLACE TYPE GetObjectsForFolderTableType IS
       TABLE OF GetObjectsForFolderObjectType;

CREATE OR REPLACE FUNCTION GetObjectsForFolder
       (FOLDER_ID INTEGER, FOLDER_DATA_TABLE VARCHAR2)
RETURN GetObjectsForFolderTableType IS 
       THE_RESULT GetObjectsForFolderTableType := GetObjectsForFolderTableType ( );
BEGIN 
       EXECUTE IMMEDIATE 'SELECT * FROM '||FOLDER_DATA_TABLE BULK COLLECT INTO THE_RESULT;
       RETURN THE_RESULT;
END; 

SELECT * FROM TABLE(GetObjectsForFolder(1, 'MY_TABLE_NAME'));

以上结果是

ORA-00932: inconsistent datatypes: expected - got -
ORA-06512: at "USERNAME.GETOBJECTSFORFOLDER", line 6
00932. 00000 -  "inconsistent datatypes: expected %s got %s"
*Cause:    
*Action:

注意:虽然我在示例中屏蔽了一些内容,但ORA-00932行确实显示“expected - got - ”。

1 个答案:

答案 0 :(得分:2)

在动态PL / SQL语句中,您无法将表列直接批量收集到对象表中。您必须首先在对象中选择列,然后将对象批量收集到表类型中:

    CREATE OR REPLACE TYPE GetObjectsForFolderObjectType IS OBJECT (
           FOLDER_ID NUMBER(9), OBJECT_ID NUMBER(9)
    );

    CREATE OR REPLACE TYPE GetObjectsForFolderTableType IS
           TABLE OF GetObjectsForFolderObjectType;

    CREATE OR REPLACE FUNCTION GetObjectsForFolder
           (FOLDER_ID INTEGER, FOLDER_DATA_TABLE VARCHAR2)
    RETURN GetObjectsForFolderTableType IS 
           THE_RESULT GetObjectsForFolderTableType := GetObjectsForFolderTableType ( );
    BEGIN 
           EXECUTE IMMEDIATE 'SELECT GetObjectsForFolderObjectType(folder_id, object_id) FROM '||FOLDER_DATA_TABLE BULK COLLECT INTO THE_RESULT;
           RETURN THE_RESULT;
    END;