ORA-01795问题的一个解决方法

   今天软件出了一个Bug,报出ORA-01795: maximum number of expressions in a list is 1000 错误,
才知道Oracle的In-list有1000个元素的限制。若把条件分成多个少于1000的IN:SELECT * FROM DUAL T WHERE T.DUMMY IN ('1', '2', '3',...,'1000') OR IN ('1001', '1002', ..., '2000') OR ... 即可解决这个异常:
  1.         StringBuffer sb = new StringBuffer();
  2.         for (int i = 0; i < columnContents.size(); i++) {
  3.             if (PluginConst.EMPTY_STRING.equals(columnContents.get(i))) {
  4.                 continue;
  5.             }
  6.             if ((i % 1000) == 0 && i > 0) {
  7.                 sb.deleteCharAt(sb.length() - 1);
  8.                 sb.append(") OR " + columnName + " IN ( '"
  9.                         + columnContents.get(i) + "',"); // resolved
  10.                 // resolved ORA-01795 problem.
  11.             } else {
  12.                 sb.append("'" + columnContents.get(i) + "',");
  13.             }
  14.         }
  15.         // delete the last comma
  16.         sb.deleteCharAt(sb.length() - 1);
  17.         String selectSQL = "SELECT * FROM " + tableName + " WHERE "
  18.                 + columnName + " IN ( " + sb.toString() + " )";

你可能感兴趣的:(数据库相关)