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

杀马特。学长 韩版系。学妹 提交于 2021-02-08 07:36:46

问题


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

回答1:


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;
/


来源:https://stackoverflow.com/questions/54894802/insert-into-table-from-collection-type-oracle-12c-ora-00902-invalid-datatype

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!