Skip to Main Content
  • Questions
  • Is there a way to bulk collect into associate array in 10G?

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question.

Asked: December 31, 2004 - 2:35 pm UTC

Last updated: June 07, 2018 - 1:44 am UTC

Version: 10.0.0

Viewed 10K+ times! This question is

You Asked

Hi Tom. First of all, I just want to say thank you for creating such a forum. I have just started using Oracle and have learnt a lot from you here.

Now, my question is as follows:

Let's say I have a table with 3 columns:
create table T (col1 number, col2 number, col3 varchar2(10));

Column col1 is the primary key in this table.

Now after writing PLSQL code for a few months now, I always find myself in a situation where I like to query a table (like table T above) and load the columns in an associate array of records with col2 and col3 as the member of the record and col1 as the index.
example:
array(col1).col2 := 3;
array(col1).col3 := 'abc';

With this data structure in place, I can make cache of such table in PLSQL.

Right now, what I do is I bulk collect into an array of records of 3 member (col1, col2, col3) and then use another FOR LOOP to construct the associative array that i wanted. The code is simple, but it's just not as convenient and elegant.

My question is is there a new syntax in 10G for building such associate array using SQL query (bulk collect) in one shot? If there is no such syntax yet, do you think you can get this as an enhancement request to the next release of Oracle? I think it will be a very convenient and nice feature.

Thank You and Best Regards!



and Tom said...

I think YOU can get it in as an enhancement request. In fact -- I know you can, enhancments are filed in metalink.oracle.com and they carry more weight when they come from you.

No, there is no syntax currently for that. You have to

select index_column, value_column bulk collect into array1, array2 from table;
for i in 1 .. array1.count
loop
correlated_array(array1(i)) := array2(i);
end loop;


fetching into the correlated array won't be much more efficient in plsql directly if they did implement it (guess here). For the correlated_array is a "sparse array" (not dense like a bulk collect array) and totally "disorganized" upon entry (the correlated_array has to sort the data).

The only thing that would be bought for us would be the removal of the loop -- they would still need to bulk collect it out and then put it into the correlated array in the background.


==============
Addenda: June 2018

There are some assignment improvements in 18c, eg

SQL> declare
  2    type rec is record ( x int, y int, z int );
  3    type reclist is table of rec index by pls_integer;
  4    r reclist;
  5  begin
  6    r := reclist(
  7          1=>rec(1,2,3),
  8          5=>rec(4,5,6),
  9          7=>rec(7,8,9)
 10          );
 11  end;
 12  /

PL/SQL procedure successfully completed.

Rating

  (1 rating)

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

Comments

Has this changed since 2004?

Matt, June 06, 2018 - 3:22 pm UTC

I was just wondering if BULK COLLECT into a sparely populated associated array has been implemented since 2004?

I don't see anything that would lead me to believe it has, but I can't count the number of times this forum has pointed me to something I hadn't seen before.

My current version is 12c
Connor McDonald
June 07, 2018 - 1:44 am UTC

Not in this particular case (ie, wanting to bulk collect but nominate one of the values as the array *index*).

But you can do this in 18c, where you nominate at assignment time the index you want to use

SQL> declare
  2    type rec is record ( x int, y int, z int );
  3    type reclist is table of rec index by pls_integer;
  4    r reclist;
  5  begin
  6    r := reclist(
  7          1=>rec(1,2,3),
  8          5=>rec(4,5,6),
  9          7=>rec(7,8,9)
 10          );
 11  end;
 12  /

PL/SQL procedure successfully completed.



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