Suppose that we have a departments table (dep_id, dep_name,...) and another table with persons from every department (person_id, dep_id,...).
If many operations are required on the data from the persons table which are part of some departments (e.g. dep_name like ‘A%’), instead of applying all the time the condition "where dep_id in (select dep_id from dep_table where dep_name like 'A%')", or the "exist" condition, another technique can be used:
- a string with all the dep_ids needed and the usage of InStr function:
declare
dep_list varchar2(10000) := 'dep|';
begin
for i_rec in (select dep_id from dep_table where dep_name like 'A%')
loop
dep_list := dep_list || i_rec || '|';
end loop;
...............
-- when selecting the person according to the needed departments
for p_rec in (select ….. from pers_table a
where InStr(dep_list, '|' || a.dep_id || '|') > 0) -– this will select only the neded persons
loop
..................
end loop;
end;
If many operations are required on the data from the persons table which are part of some departments (e.g. dep_name like ‘A%’), instead of applying all the time the condition "where dep_id in (select dep_id from dep_table where dep_name like 'A%')", or the "exist" condition, another technique can be used:
- a string with all the dep_ids needed and the usage of InStr function:
declare
dep_list varchar2(10000) := 'dep|';
begin
for i_rec in (select dep_id from dep_table where dep_name like 'A%')
loop
dep_list := dep_list || i_rec || '|';
end loop;
...............
-- when selecting the person according to the needed departments
for p_rec in (select ….. from pers_table a
where InStr(dep_list, '|' || a.dep_id || '|') > 0) -– this will select only the neded persons
loop
..................
end loop;
end;