六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 43|回复: 0

oracle group in 绑定变量 使用

[复制链接]

升级  48.67%

33

主题

33

主题

33

主题

秀才

Rank: 2

积分
123
 楼主| 发表于 2013-1-26 15:13:51 | 显示全部楼层 |阅读模式
1.SQL> create or replace type vartabletype as table of varchar2(1000);
  2  /
Type created.
2.SQL> create or replace function str2varList( p_string in varchar2 ) return VarTableType
  2  as
  3  v_str long default p_string || ',';
  4  v_n varchar2(2000);
  5  v_data VarTableType := VarTableType();
  6  begin
  7     loop
  8        v_n :=instr( v_str, ',' );
  9      exit when (nvl(v_n,0) = 0);
 10      v_data.extend;
 11     v_data( v_data.count ) := ltrim(rtrim(substr(v_str,1,v_n-1)));
 12      v_str := substr( v_str, v_n+1 );
 13     end loop;
 14     return v_data;
 15  end;
 16  /
Function created.
执行上面的函数后,用变量S进行测试:
SQL> var s varchar2(100)
SQL> exec :s:='a,b,c';
PL/SQL procedure successfully completed.
SQL> print s
S
--------------------------------------------------------------------------------
a,b,c

SQL> select * from table(str2varList(:s)) a;
COLUMN_VALUE
--------------------------------------------------------------------------------
a
b

使用绑定变量的时候可以用IN (select * from table(str2varList(:s)) a;)

现在要做一些修改
1.在 ARTICLECONTENT 的ARTICLEID  和
ANALYZE TABLE ARTICLECONTENT  COMPUTE STATISTICS FOR ALL INDEXED COLUMNS;

create or replace function str2varList( p_string in varchar2 ) return VarTableType
  2  as
  3  v_str long default p_string || ',';
  4  v_n varchar2(2000);
  5  v_data VarTableType := VarTableType();
  6  begin
  7     loop
  8        v_n :=instr( v_str, ',' );
  9      exit when (nvl(v_n,0) = 0);
 10      v_data.extend;
 11     v_data( v_data.count ) := ltrim(rtrim(substr(v_str,1,v_n-1)));
 12      v_str := substr( v_str, v_n+1 );
 13     end loop;
 14     return v_data;
 15  end;
在程序中使用方法:
select v.articleid, v.zsturl, v.content, v.groupid, v.arr_gcid, v.path from v_csteel_news v where v.path in(select * from table(str2varList('xyzx,fdc,jx,sh,zc,dl,jd,dj,jcjs,xysj,xyzx,gjbz,jj,byq,qc,xyzs') )a) and v.rootgroupid =395 and rownum<=5  
 根据实际测试,速度是提高不少!
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表