简体   繁体   中英

ORA-00932 - inconsistent datatypes after creating table of custom type

I'm developing a query which return two columns as result: VARCHAR(256) and NUMBER . In order to store the query result for future use, I created the types below:

-- An object representing each of the records of my query
create or replace TYPE TP_SOME_TYPE IS OBJECT(
    SOME_TEXT VARCHAR2(256), 
    SOME_NUMBER NUMBER
);

-- A table of the objects created above:
create or replace TYPE TP_SOME_TABLE AS TABLE OF TP_SOME_TYPE;

Finally, I run a sample SQL trying to collect the result in a variable of type TP_SOME_TABLE :

DECLARE
    SOME_RESULT TP_SOME_TABLE;
BEGIN
    EXECUTE IMMEDIATE 'SELECT ''TEXT'' AS SOME_TEXT, 1 AS SOME_NUMBER FROM DUAL'
    BULK COLLECT INTO SOME_RESULT;
END;

And get the following error message:

ORA-00932: tipos de dados inconsistentes: esperava - obteve -
ORA-06512: em line 4
00932. 00000 -  "inconsistent datatypes: expected %s got %s"
*Cause:    
*Action:

I've tried defining a RECORD TYPE instead of OBJECT TYPE on the body of my function with the same fields and it worked, unfortunately, since I'm going to expose the collected result in a TABLE() call, I have to use something database level like OBJECT .

Any ideas to make the current structure work?

My database setup:

Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
"CORE   11.2.0.4.0  Production"
TNS for Linux: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production

Thanks in advance.

An object has to be explicitly constructed. Change the query in your test to:

EXECUTE IMMEDIATE 'SELECT tp_some_type(''TEXT'', 1) FROM DUAL'

you to convert the covert the column being referenced in select statement of TP_SOME_TYPE type.

   DECLARE
    SOME_RESULT TP_SOME_TABLE;
BEGIN
    EXECUTE IMMEDIATE 'SELECT TP_SOME_TYPE(''TEXT'' , 1) FROM DUAL'
    BULK COLLECT INTO SOME_RESULT;
END;

you can still be able to access this collection like select SOME_TEXT ,SOME_NUMBER from table(ur_collection);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM