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

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

          /**
           * <b>function:</b> 處理oracle sql 語句in子句中(where id in (1, 2, ..., 1000, 1001)),如果子句中超過1000項就會報錯。
           * 這主要是oracle考慮性能問題做的限制。如果要解決次問題,可以用 where id (1, 2, ..., 1000) or id (1001, ...)
           * @author hoojo
           * @createDate 2012-8-31 下午02:36:03
           * @param ids in語句中的集合對象
           * @param count in語句中出現的條件個數
           * @param field in語句對應的數據庫查詢字段
           * @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
          本文版權歸作者和博客園共有,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責任的權利。


          版權所有,轉載請注明出處 本文出自:
          分享道版權所有,歡迎轉載,轉載請注明出處,謝謝
          posted on 2012-08-31 14:51 hoojo 閱讀(5000) 評論(0)  編輯  收藏 所屬分類: DataBaseJavaEEJavaSEOracle
          主站蜘蛛池模板: 双柏县| 监利县| 澄江县| 射洪县| 上犹县| 衡南县| 大洼县| 项城市| 光山县| 正宁县| 平定县| 太原市| 宁海县| 柳林县| 静乐县| 偏关县| 贵溪市| 望江县| 乌鲁木齐县| 平利县| 辰溪县| 新巴尔虎左旗| 闻喜县| 台中市| 德钦县| 景德镇市| 方山县| 华容县| 抚顺县| 高唐县| 门源| 洛阳市| 南投县| 云和县| 大足县| 新宾| 开原市| 伊金霍洛旗| 民权县| 思茅市| 盈江县|