java jdbc oracle ORA-01795: 列表中的最大表达式数为 1000

在操作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语句时稍微注意一下就好了。

posted @ 2016-08-04 18:36  东北大亨  阅读(4344)  评论(0编辑  收藏  举报