首页 技术 正文
技术 2022年11月19日
0 收藏 792 点赞 4,553 浏览 1341 个字

    处理oracle sql 语句in子句中(where id in (1, 2, …, 1000, 1001)),如果子句中超过1000项就会报错。这主要是oracle考虑性能问题做的限制。如果要解决次问题,可以用 where id (1, 2, …, 1000) or id (1001, …)

package windy.learn;import java.util.Collection;import org.apache.commons.lang3.StringUtils;public class OracleSqlUtils {
/**
* x,y相除,向上取整
* @param x
* @param y
* @return x,y相除,向上取整
*/
private static int ceilDiv(int x, int y) {
int r = x / y;
return r * y == x ? r : r + 1;
} public static String getOracleSQLIn(Collection<String> ids, String field) {
return getOracleSQLIn(ids, 1000, field);
} /**
* oracle sql 语句in子句中如果子句中超过1000项就会报错。这主要是oracle考虑性能问题做的限制。 如果要解决次问题,可以用
* where id (1, 2, ..., 1000) or id (1001, ...)
*
* @param ids
* in语句中的集合对象
* @param count
* in语句中出现的条件个数
* @param field
* in语句对应的数据库查询字段
* @return 返回 field in (...) or field in (...) 字符串
*/
public static String getOracleSQLIn(Collection<String> ids, int count,
String field) {
String[] idsArr = ids.toArray(new String[0]);
count = Math.min(count, 1000);
int len = idsArr.length;
int size = ceilDiv(len, count);
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(idsArr, "','", fromIndex, toIndex), "");
if (i != 0) {
builder.append(" or ");
}
builder.append(field).append(" in ('").append(productId)
.append("')");
} return StringUtils.defaultIfEmpty(builder.toString(), field
+ " in ('')");
}
}

参考:http://www.cnblogs.com/hoojo/archive/2012/08/31/2665396.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905