How to initialize a varray table of {TABLE}%ROWTYPE?

dr jerry picture dr jerry · Nov 8, 2010 · Viewed 8.9k times · Source

I have a varray defined like:

declare
    TYPE tnr_l IS VARRAY(30) of lve%ROWTYPE;

I want this varray to be initialized with a fetch from the database:

select * into tnr_l from lve where type = 'TNR' order by value;

But this fails with:

.ORA-06550: line 6, column 23:
PLS-00321: expression 'TNR_L' is inappropriate as the left hand side of an
assignment statement

How can I make this work?

Answer

Tony Andrews picture Tony Andrews · Nov 8, 2010

You need to declare a variable of the type tnr_l, and then you need to use bulk collect in the select like this example:

declare
  type t_dept is varray(100) of dept%rowtype;
  l_dept t_dept;
begin
  select * bulk collect into l_dept from dept;
  for i in 1..l_dept.count loop
    dbms_output.put_line(l_dept(i).dname);
  end loop;
end;