当前位置:操作系统 > Unix/Linux >>

PreparedStatement中in子句的处理

PreparedStatement中in子句的处理
 
测试环境: Oracle 10g
 
1.原理
 
1
select * from table(split('a,b,c'))
结果:
1
a
2
b
3
c
2.结论
对于类似:
 
1
select * from xxx_table where xxx_column in ('xxa', 'xxb', 'xxc')
可动态传入in子句参数
1
select * from xxx_table where xxx_column in (select * from table(split(?)))
1
PreparedStatement stmt = conn.prepareStatement(sql);
2
stmt.setObject(1, "xxa,xxb,xxc");
 
附split函数:
 
 
01
create or replace type split_tbl as table of varchar(32767);
02
/
03
 
04
create or replace function split
05
(
06
  p_list varchar2,
07
  p_del varchar2 := ','
08
) return split_tbl pipelined
09
is
10
  l_idx    pls_integer;
11
  l_list    varchar2(32767) := p_list;
12
  l_value    varchar2(32767);
13
begin
14
  loop
15
    l_idx := instr(l_list,p_del);
16
    if l_idx > 0 then
17
      pipe row(substr(l_list,1,l_idx-1));
18
      l_list := substr(l_list,l_idx+length(p_del));
19
    else
20
      pipe row(l_list);
21
      exit;
22
    end if;
23
  end loop;
24
  return;
25
end split;
26
/
 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,