在操作SQL中存在In的數量如果超過1000條會提示 ORA-01795: 列表中的最大表達式數為 1000
歸納有幾種方式出現的:
第一種是:我在上一個 [jdbc 同時執行 查詢和刪除操]作中提到 在一個事務中在了in操作超出了 1000條,修改代碼如下:
Connection conn = null;
try {
// 創建連接實例
conn = JdbcUtility.GetFactory().CreateConn();
conn.setAutoCommit(false);
conn.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
StringBuilder _strbd = new StringBuilder();
_strbd.append(" delete from table1 where id in ( ");
_strbd.append(" select sp_1.table1 from table2 ");
_strbd.append(" sp_1,sp_tcontentinfo sp_2 ");
_strbd.append(" where sp_1.id=sp_2.id");
_strbd.append(" ) ");
Statement stmt = conn.createStatement();
// 執行語句
int r = stmt.executeUpdate(_strbd.toString());
conn.commit();
stmt.close();
log.info("清理數據成功!");
} catch (Exception ex) {
if (null != conn) {
try {
conn.rollback();
} catch (SQLException se) {
log.error("清理數據回滾失敗!");
}
}
log.error("清理數據失敗,錯誤信息為:"+ex.getMessage());
} finally {
if (null != conn) {
try {
conn.close();
} catch (SQLException se) {
log.error("清理數據,關閉數據庫失敗!");
}
}
}
第二種: 在單獨的查詢中
SQL里面的IN中的數據量不能超過1000條
解決辦法:
例如
Select * from table_name where col in (‘col1’,’col2’ ……..)
如果in 后面的Item過多的話,超過1000就會出現這種錯誤。
解決方法是:
Select * from tablename where col in (‘col1’,’col2’ …….., ‘col1000’) or col in (‘col1001’, …………)
在構建SQL語句時稍微注意一下就好了。