Вопрос

I have following declaration for collection

TYPE T_TABLE1 IS TABLE OF TABLE_1%ROWTYPE INDEX BY BINARY_INTEGER;
tbl1_u             T_TABLE1;
tbl1_i             T_TABLE1;

This table will keep growing and at the end, will be used in FORALL loop to do insert or update on TABLE_1.

Now there might be cases, where I want to delete a certain element. So i am planning to create a procedure, which will take the KEY (unique) and matched the element if that key is found

PSEDUO CODE

FOR i in tbl1_u.FIST..tbl1_u.LAST 
LOOP
   if tbl1_u(i).key = key then
     tbl1.delete(i);
   end if;

END LOOP;

My question is,

  1. Once i delete the particular element, would be collection adjust automatically i.e., the index i would be replaced by next element or would that particular index will remain null/invalid and could possibly give me exception if i use it in FORALL INSERT/UPDATE?

  2. I don't think that i can pass TABLE_1%ROWTYPE object to a procedure, do i have to create a record type ?

  3. Any other tip regarding managing collection for bull delete/update/insert would be appreciate. Remeber, I would be dealing with 2 tables, if i am inserting/updating in table_1 then it means i am deleting it from table_2 and vice-versa.
Это было полезно?

Решение

Given that TABLE_1.KEY is unique you might consider using that as the index to your associative arrays. That way you can delete from the collections using the KEY value, which according to the pseudocode is available when doing the deletions. This would also save you having to iterate through the table to find the KEY you want, as the KEY would be the index - so your "deletion" pseudo-code would become:

tbl1_u.delete(key);

To answer your questions:

  1. Since you're using associative arrays, when an element is deleted there is no "empty" space in the collection. The indexes for the elements, however, don't actually change. Therefore you need to use the collection.PRIOR and collection.NEXT methods to loop through the collection. But again, if you use the KEY value as the index you may not need to loop through the collections at all.

  2. You can pass a TABLE_1%ROWTYPE as a parameter to a PL/SQL procedure or function.

  3. You might want to consider using a MERGE statement which could handle doing the inserts and updates in one step. This might allow you to maintain only a single collection. Might be worth looking in to.

Share and enjoy.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top