using INSTR function for obtaining data from a table, based on a selection from the master table (FK relation)

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;
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值