oracle in list 问题解析

解决where in list 问题,首要考虑的就是解决列表长度问题和效率问题,效率问题首要考虑绑定变量问题,另外还要考虑比如cardinality对执行计划的影响等

declare

  v_condition varchar2(100);

  v_sql varchar2(1000);

  v_count number(10);

 begin

  v_condition :='''XY'''||','||'''YZ''';

 select count(*)

 into v_count

 from t

 where object_name in(v_condition);

 DBMS_OUTPUT.PUT_LINE(v_count);

 v_sql:='select count(*) from t where object_name in ('||v_condition||')';

 DBMS_OUTPUT.PUT_LINE(v_sql);

 end;

 /

declare

  v_condition varchar2(100);

  v_sql varchar2(1000);

  v_count number(10);

 begin

  v_condition :='''XY'''||','||'''YZ''';

 v_sql:='select count(*) from t where object_name in ('||v_condition||')';

 EXECUTE IMMEDIATE v_sql into v_count;

 DBMS_OUTPUT.PUT_LINE(v_count);

 DBMS_OUTPUT.PUT_LINE(v_sql);

 end;

 /


可以使用正则表达式regexp_substr,将按指定分隔符组成的字符串转为中间查询结果集,然后使用子查询(in,exists)或者join解决where in list问题

猜你喜欢

转载自phrmgb.iteye.com/blog/1522464