应用场景:有一个用逗号分隔的字符串,需要查询出A表中字段b否包含该字符串的数据行。
代码实例:
select * from A where A.b in (select column_value from table(split('1,2,3,4',',')));
这里的重点是 select column_value from table(split('1,2,3,4',','))
把一个字符串分割,生成一个表,然后就可以对表数据进行操作。
split是自定义的一个function,内容如下:
create or replace function split (p_list clob, p_sep varchar2 := ',')
return tabletype
pipelined
is
l_idx pls_integer;
v_list varchar2 (32676) := to_char(p_list);
begin
loop
l_idx := instr (v_list, p_sep);
if l_idx > 0
then
pipe row (substr (v_list, 1, l_idx - 1));
v_list := substr (v_list, l_idx + length (p_sep));
else
pipe row (v_list);
exit;
end if;
end loop;
end;
函数split中return的返回值类型为tabletype,tabletype是一个自定义的类型。创建的脚本如下:
create or replace type tabletype as table of varchar2(32676);