in(boothid,boothid,boothid,boothid..........)这种方式组装sql语句 oracle最大支持1000个 问题解决

问题原因是由于sql中运用了 in(boothid,boothid,boothid,boothid..........)这种方式组装sql语句的方式。这种方式在boothid数量比较小的情况下没有问题。但是oracle最大支持1000个。所以当某会员名下商铺大于1000时就会出现错误。报错是:ORA-01795: 列表中的最大表达式数为 1000

这种情况下的一种解决方式是将 in 语句分成多个in语句来实现 eg:in(boothid,boothid) or in (boothid,boothid) 而每个in中的列数最多不能超过1000. 这次解决就是采取的这种方式.做到每500个分一组实现. 

下面是我的简单实现:

 /**
  * <p>
  * 构造SQL语句的In子句。

  * <p>
  * 作者:yulm <br>
  * 日期:2007-7-17
  *
  * @param values,boothid
  * @return
  */
 public static String getInClause(long[] values,String boothid) {
  StringBuffer inClause = new StringBuffer();
  int length = values.length;
  if(length>500){
   int loopnum = length / 500 ;
   int lessnum = length % 500;
   int begin = 0;
   int end = 500 ;
   for(int j = 0; j < loopnum; j++){
    if(j==0){
     inClause.append(" in (");
    }else{
     
     inClause.append(" or ");
     inClause.append(boothid);
     inClause.append(" in (");
    }
    
    for(int m = begin; m< end; m++){
     inClause.append(values[m]).append(",");
    }
    inClause.setLength(inClause.length() - 1);
    inClause.append(")");
    begin = begin + 500 ;
    end = end + 500 ;

   }
   
   if(lessnum>0){
    inClause.append(" or ");
    inClause.append(boothid);
    inClause.append(" in (");
    int totalnum = begin + lessnum;
    for(int n = begin; n< totalnum; n++){
     inClause.append(values[n]).append(",");
    }
    inClause.setLength(inClause.length() - 1);
    inClause.append(")");
   }
   
   
  }else{
   inClause.append(" in (");
   for (int i = 0; i < length; i++) {
    inClause.append(values[i]).append(",");
   }
   inClause.setLength(inClause.length() - 1);
   inClause.append(") ");
   
  }
  inClause.append(") ");
  return inClause.toString();
 }
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值