I've created a sample table in Oracle 11.2.0.1.0
:
create table my_table ( id number , val varchar2(5));
insert into my_table
select level , dbms_random.string('a', 1) from dual
connect by level <= 10000;
commit;
select * from my_table where LOWER(VAL) = 'h'; --returns 350 rows
Now, I want to update several tables which have the id of having lower(val) = 'h'
So, I've used:
begin
for rec in(select id from my_table where lower(val) = 'h')
loop
update my_table1 set val1 = 'XX' where id1 = rec.id;
update my_table2 set val2 = 'XX' where id2 = rec.id;
update my_table3 set val3 = 'XX' where id3 = rec.id;
update my_table4 set val4 = 'XX' where id4 = rec.id;
... --some more tables to update with the same rec.id
end loop;
end;
My colleague asked me to do the same with using TYPE
, and I've tried with:
CREATE OR REPLACE TYPE my_number_type IS TABLE OF NUMBER;
DECLARE
my_number_obj my_number_type;
BEGIN
SELECT ID
BULK COLLECT INTO my_number_obj
FROM my_table
WHERE lower(val) = 'h';
UPDATE my_table1
SET val1 = 'XX'
WHERE ID1 IN (SELECT COLUMN_VALUE FROM TABLE(CAST(my_number_obj AS my_number_type)));
UPDATE my_table2
SET val2 = 'XX'
WHERE ID2 IN (SELECT COLUMN_VALUE FROM TABLE(CAST(my_number_obj AS my_number_type)));
... --some more tables to update with the id in my_number_obj
END;
Which of the above is better to use (in any aspect)? Or is there any other better way to update for my requirement?