隨筆-57  評論-117  文章-1  trackbacks-0

          處理oracle sql 語句in子句中(where id in (1, 2, ..., 1000, 1001)),如果子句中超過1000項(xiàng)就會報(bào)錯(cuò)。
          這主要是oracle考慮性能問題做的限制。如果要解決次問題,可以用 where id (1, 2, ..., 1000) or id (1001, ...)

          /**
           * <b>function:</b> 處理oracle sql 語句in子句中(where id in (1, 2, ..., 1000, 1001)),如果子句中超過1000項(xiàng)就會報(bào)錯(cuò)。
           * 這主要是oracle考慮性能問題做的限制。如果要解決次問題,可以用 where id (1, 2, ..., 1000) or id (1001, ...)
           * @author hoojo
           * @createDate 2012-8-31 下午02:36:03
           * @param ids in語句中的集合對象
           * @param count in語句中出現(xiàn)的條件個(gè)數(shù)
           * @param field in語句對應(yīng)的數(shù)據(jù)庫查詢字段
           * @return 返回 field in (...) or field in (...) 字符串
           */
          private 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);
                  //System.out.println(ids.subList(fromIndex, toIndex));
                  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 ('')");
          }


          作者:hoojo
          出處:
          blog:http://blog.csdn.net/IBM_hoojo
                   http://hoojo.cnblogs.com
          本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。


          版權(quán)所有,轉(zhuǎn)載請注明出處 本文出自:
          分享道版權(quán)所有,歡迎轉(zhuǎn)載,轉(zhuǎn)載請注明出處,謝謝
          posted on 2012-08-31 14:51 hoojo 閱讀(5000) 評論(0)  編輯  收藏 所屬分類: DataBaseJavaEEJavaSEOracle
          主站蜘蛛池模板: 繁昌县| 社旗县| 南部县| 九台市| 阳城县| 临桂县| 阿巴嘎旗| 鸡泽县| 盐源县| 那曲县| 邻水| 永善县| 隆子县| 贞丰县| 横山县| 思南县| 广饶县| 丹东市| 民勤县| 贵港市| 泰宁县| 台东市| 全南县| 凌源市| 四川省| 永济市| 文昌市| 保定市| 加查县| 大宁县| 沂南县| 比如县| 青河县| 北宁市| 利辛县| 德化县| 岱山县| 穆棱市| 晋中市| 汤原县| 馆陶县|