PreparedStatement IN clause alternatives?

前端 未结 30 4486
情歌与酒
情歌与酒 2020-11-21 05:19

What are the best workarounds for using a SQL IN clause with instances of java.sql.PreparedStatement, which is not supported for multiple values du

30条回答
  •  耶瑟儿~
    2020-11-21 05:31

    My workaround is:

    create or replace type split_tbl as table of varchar(32767);
    /
    
    create or replace function split
    (
      p_list varchar2,
      p_del varchar2 := ','
    ) return split_tbl pipelined
    is
      l_idx    pls_integer;
      l_list    varchar2(32767) := p_list;
      l_value    varchar2(32767);
    begin
      loop
        l_idx := instr(l_list,p_del);
        if l_idx > 0 then
          pipe row(substr(l_list,1,l_idx-1));
          l_list := substr(l_list,l_idx+length(p_del));
        else
          pipe row(l_list);
          exit;
        end if;
      end loop;
      return;
    end split;
    /
    

    Now you can use one variable to obtain some values in a table:

    select * from table(split('one,two,three'))
      one
      two
      three
    
    select * from TABLE1 where COL1 in (select * from table(split('value1,value2')))
      value1 AAA
      value2 BBB
    

    So, the prepared statement could be:

      "select * from TABLE where COL in (select * from table(split(?)))"
    

    Regards,

    Javier Ibanez

提交回复
热议问题