处理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
以上就是处理 Oracle SQL in 超过1000 的解决方案的详细内容,更多关于处理 Oracle SQL in 超过1000 的解决方案的资料请关注九品源码其它相关文章!