Question

I have declared the following types in a package.

TYPE l_task_type_rec is record (task_id number, creation_date date);

TYPE tas_tab is table of l_task_type_rec;

l_task_rec tas_tab;

I am trying to use a for loop to iterate through the objects in the table type objects like this. I am not sure if it is possible.

for counter in l_task_rec.first ..l_task_rec.last

loop

    select task_id into l_task_id from l_task_rec where rowid = counter;

    select location_id into p_location_id from csf_ct_tasks where task_id = l_task_id;

    OPEN c_location_rec (p_location_id);

                    FETCH c_location_rec

                    BULK COLLECT INTO x_location_rec;

     CLOSE c_location_rec;

end loop;

What I want is to call a cursor from inside the for loop so that I can get a list of all the locations one by one and fetch and store all the data in a record type. I know the query to search the table type is wrong because it says the table or view l_task_rec does not exist! Can anyone tell me what I am doing wrong here or point me in the right direction? Thanks in advance..

Was it helpful?

Solution

Here is an example of how you can iterate through objects in the table type:

declare
  l_task_rec tas_tab;
  p_location_id number;//Assuming number
begin
  --Example data
  l_task_rec:=tas_tab( l_task_type_rec (10, sysdate), l_task_type_rec (21, sysdate), l_task_type_rec (35, sysdate));--This should come from somewhere else

  for counter in l_task_rec.first .. l_task_rec.last
  loop
    --You can get the data like this
    DBMS_OUTPUT.PUT_LINE('TASK_ID: '||l_task_rec(counter).TASK_ID);
    DBMS_OUTPUT.PUT_LINE('CREATION_DATE: '||l_task_rec(counter).CREATION_DATE);

    select location_id into p_location_id from csf_ct_tasks where task_id = l_task_rec(counter).TASK_ID;
   --More code
   --OPEN  ...
   --CLOSE ...
  end loop;
end;

In order for this to work, I changed how l_task_type_rec type is created:

create TYPE l_task_type_rec is object (task_id number, creation_date date)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top