当前位置:数据库 > SQLServer >>

sql in语句最大上限1000拼接方法

sql in语句最大上限1000拼接方法
 
   当我们用sql in语句进行查询时候。Oracle默认支持in中的数量为1000. 
这也是为了sql的性能处理,因为in 的性能本来就不很好。所以遇到这种情况最好是通过关联查询进行处理。如果关联查询不能实现你的逻辑,就抛弃性能吧。拼接sql吧。可以通过 or in 进行实现(不推荐) 
  其中进行拼接方法: 
Java代码  
private static String getOracleSQLIn(List<?> ids, int count, String field) {  
    count = Math.min(count, 1000);  
    int len = ids.size();  
    int size = len % count;  
    if (size == 0) {  
        size = len / count;  
    } else {  
        size = (len / count) + 1;  
    }  
    StringBuilder builder = new StringBuilder();  
    for (int i = 0; i < size; i++) {  
        int fromIndex = i * count;  
        int toIndex = Math.min(fromIndex + count, len);  
        String productId = StringUtils.defaultIfEmpty(StringUtils.join(ids.subList(fromIndex, toIndex), "','"), "");  
        if (i != 0) {  
            builder.append(" or ");  
        }  
        builder.append(field).append(" in ('").append(productId).append("')");  
    }  
    return StringUtils.defaultIfEmpty(builder.toString(), field + " in ('')");  
}  
 
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,