简体   繁体   中英

Insert into table from collection type oracle 12c - ORA-00902: invalid datatype

I am using Oracle 12.1 I thought I can query the table types in 12c.I get error ORA-00902: invalid datatype when I try to execute this package. I even tried using cast multiset,but still same error.

I know we can create object at database level and then query, but I don't want to.

CREATE OR REPLACE PACKAGE test123 AS
 TYPE typ1 IS RECORD(col1 VARCHAR2(100),col2 VARCHAR2(100));
 TYPE tab_typ IS TABLE OF typ1 INDEX BY BINARY_INTEGER;
v_tab tab_typ;

PROCEDURE p1;
END;
/
CREATE OR REPLACE PACKAGE BODY test123 AS

PROCEDURE p1 IS
 BEGIN
  SELECT c1,c2 BULK COLLECT INTO v_tab FROM tabx;            
   INSERT INTO taby
     SELECT * FROM TABLE(v_tab);
  END;
END;
/

EXEC test123.p1;
--ORA-00902: invalid datatype

The select * from table works fine in Cursor or for Loop, but does not when I use it for INSERT Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production PL/SQL Release 12.1.0.2.0 - Production CORE 12.1.0.2.0 Production TNS for Linux: Version 12.1.0.2.0 - Production NLSRTL Version 12.1.0.2.0 - Production

After carefully revisiting your query, I found that you were correct. Insert doesnot work. And it looks correct as well. We already have FORALL INSERT to get data inserted from the collection to a table. Hence the necessasity of having a additional INSERT as Select Statement is ruledout. However you can use the SELECT statement using a collection in Where clause of the query. To make and insert you can simply follow the below steps.

CREATE OR REPLACE PACKAGE BODY test123 
AS
PROCEDURE p1 IS
 BEGIN
  SELECT c1,c2 BULK COLLECT INTO v_tab FROM tabx;  

  ForAll rec in 1..v_tab.count
   INSERT INTO taby
    values v_tab(rec);
     --SELECT * FROM TABLE(v_tab);
  END;
END;
/

Incase you want to use the Type declared under PLSQL scope in Select statement, you can use as below:

DECLARE
 TYPE typ1 IS RECORD(col1 VARCHAR2(100),col2 VARCHAR2(100));
 TYPE tab_typ IS TABLE OF typ1 INDEX BY BINARY_INTEGER;
 v_tab tab_typ;
BEGIN

 SELECT col1,col2 BULK COLLECT INTO v_tab FROM tabx;  

  DELETE FROM taby
    WHERE (col1,col2) in (Select * from table(v_tab)); 

END;
/

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