use*_*123 1 oracle plsql function
我有以下oracle函数,其中num_list作为逗号分隔列表的输入,然后检查此列表中的数字是否存在于某个表中:
create or replace FUNCTION get_num_exist (num_list varchar2,separator varchar2)
RETURN nbs PIPELINED
as
row_type nb;
begin
with numbers as
(select regexp_substr(num_list,'[^'||separator||']+', 1, level)num_
from dual
connect by regexp_substr(num_list, '[^'||separator||']+', 1, level) is not null)
for r_row in (select * from numbers
where num_ in (select phone_number
from phone_numbers) )
loop
PIPE ROW(nb(r_row.num_));
end loop;
return;
end;
Run Code Online (Sandbox Code Playgroud)
但它在for循环级别给出语法错误.
任何帮助,将不胜感激.
您似乎在混合使用SQL和PL/SQL.
for r_row in (
with numbers as
(select regexp_substr(num_list,'[^'||separator||']+', 1, level) num_
from dual
connect by regexp_substr(num_list, '[^'||separator||']+', 1, level)
is not null )
select * from numbers where num_ in (
select phone_number from phone_numbers) )
loop
PIPE ROW(nb(r_row.num_));
end loop;
Run Code Online (Sandbox Code Playgroud)
但我曾经以另一种方式做到这一点(我认为更快,但如果phone_numbers.phone_number上有索引,情况就不会如此).
for r_row in (
select phone_number num_ from phone_numbers
where instr(separator || num_list || separator,
separator || phone_number || separator) > 0
) loop
PIPE ROW(nb(r_row.num_));
end loop;
Run Code Online (Sandbox Code Playgroud)
或许可以解释一下.我在搜索列表之前和之后以及数据库列之前和之后放置分隔符(以避免误报).instr> 0时,我模拟Oracle(或任何数据库)"其中phone_number IN(...,...,...)".