Skip to Main Content
  • Questions
  • Can you insert select from an associative array?

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, Marcel.

Asked: July 16, 2020 - 7:54 am UTC

Last updated: July 17, 2020 - 8:41 am UTC

Version: 18.5

Viewed 1000+ times

You Asked

I am trying to use an associative array to insert the contents in a table. We have an 18c database so I thought it should be possible to use an associative array in a SQL statement. Before 12c I used database nested table types for this purpose.

The LiveSQL test demonstrates the problem I am experiencing: ORA-00902: invalid datatype after running the code. So my question is: are associative arrays allowed in an insert select from table(associative_array_name) statement and if so how do I prevent the error I am experiencing?

Thank you for your help.

with LiveSQL Test Case:

and Chris said...

No, you can't sadly:

If the PL/SQL-only data type is an associative array, it cannot be used within a non-query DML statement (INSERT, UPDATE, DELETE, MERGE) nor in a subquery.

https://docs.oracle.com/database/121/LNPLS/release_changes.htm#GUID-57E439FB-B196-46CB-857C-0ADAB32D9EA0

I don't see any need to in this case though. You can use forall insert instead:

    forall i in 1 .. pa_test_tab.count
      insert into test_tab 
      values pa_test_tab(i);

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Moving over DBLink

Raj, July 17, 2020 - 7:11 am UTC

Hello Chris,

Asking a dblink question under this may be irrelevant, but need your suggestion in choosing one of the method for moving the data from One DB to Another DB through DBlink

Source db: Need to join two tables to get the result set that needs to transfer

option1: create a view at source db and querying that view from remote DB as below from remote

declare
cursor c is select * from view@dblink;
begin
loop
fetch c into collection limit 10k;
forall
insert into table;
end loop;
end;
option2: create a GTT in Source DB with result set and performing remote insert from same DB as below
declare
begin
insert into GTT (result set of joining the tables);
insert into remote table@dblink
select * from GTT;
end;

option3: writing a procedure with collection as out parameter and calling that procedure from remote DB
and use that collection for loading data in remote table.

can you help me in knowing pros and cons of above options and your suggestion from the options or out of the options ;)?
thanks in advance
Chris Saxon
July 17, 2020 - 8:41 am UTC

Yes, it is irrelevant (or extremely loosely related at best).

Please submit a new question about this - this makes the site easier to for others to use and follow. You can use the "notify me" option if we're not currently accepting new questions.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library