oracle 存储过程中,解决变量使用 in条件时,查询无效问题

oracle 存储过程中,定义变量之后,使用变量进行 in 条件查询时,会出现查询条件无效的问题

  • 表结构
    oracle 存储过程中,解决变量使用 in条件时,查询无效问题_第1张图片
  • 表数据
    oracle 存储过程中,解决变量使用 in条件时,查询无效问题_第2张图片
  • 解决方法
create or replace type strsplit_type is table of varchar2(30000);
create or replace function strsplit(para_str varchar2, para_split varchar2 := ',')

return strsplit_type

pipelined is

  do_idx integer;
  do_str varchar2(500);
  do_last varchar2(4000) := para_str;

begin

  if substr(para_str, length(para_str)) = '|' then

    do_last := substr(para_str, 1, length(para_str) - 1);

  end if;

  loop

    do_idx := instr(do_last, para_split);
    exit when do_idx = 0;
    do_str := substr(do_last, 1, do_idx - 1);
    do_last := substr(do_last, do_idx + 1);
    pipe row(do_str);
  end loop;

  pipe row(do_last);

  return;

end strsplit;
  • 传参方式
select * from table(strsplit('n1|n2|n3|n4', '|'));

oracle 存储过程中,解决变量使用 in条件时,查询无效问题_第3张图片

  • 调用
select * from DEPT t where t.dname in (select * from table(strsplit('n1|n2|n3|n4', '|')));

oracle 存储过程中,解决变量使用 in条件时,查询无效问题_第4张图片

你可能感兴趣的:(数据库,oracle,数据库)