隨筆-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 閱讀(5006) 評論(0)  編輯  收藏 所屬分類: DataBase 、JavaEE 、JavaSE 、Oracle
          主站蜘蛛池模板: 色达县| 巴林左旗| 兴山县| 达孜县| 丁青县| 奎屯市| 凤庆县| 大连市| 平乡县| 四川省| 慈利县| 河北省| 诸暨市| 康平县| 鹤峰县| 读书| 洪洞县| 山东省| 漠河县| 海丰县| 汕尾市| 额尔古纳市| 清新县| 佳木斯市| 巴中市| 博白县| 东台市| 石景山区| 广南县| 鄱阳县| 西贡区| 罗源县| 屏东县| 常州市| 扶绥县| 隆尧县| 海伦市| 乳山市| 临沧市| 酒泉市| 青冈县|