ORA-01795解决方法

a in ('1', '2', '3')
动态拼接的sql中,in的数据超过1000报错。
解决方法:

1.改写为a in ('1', '2') or a in ('3') 的形式

参考代码

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(installLongInSql("aa", new ArrayList(Arrays.asList("1", "2", "3", "11", "2", "2", "2", "1"))));
    }

    public static String installLongInSql(String col, List<String> inList){
        if(inList == null || inList.size() == 0 || col == null || col.length() == 0)
            return "";

        StringBuffer sb = new StringBuffer(" " + col + " in (");

        int len = 5;
        int i = 0;
        for(; i < inList.size(); i++){
            if(i % len == 0 && i != 0)
                sb.append(" or " + col + " in (");
            sb.append("'" + inList.get(i) + "',");
            if(i % len == len - 1)
                sb.deleteCharAt(sb.length() - 1).append(")");
        }
        if(i % len != len - 1)
            sb.deleteCharAt(sb.length() - 1).append(")");

        return sb.toString();
    }

2.在条件允许的情况下,创建临时表

拼接结果示例

create table temp_1800601001 as
select '1' a from dual union all
select '1' a from dual union all
select '1' a from dual union all
select '1' a from dual 

之后用临时表做表关联。

3.对整条语句进行拆分。

不看好,不建议。
select还能凑合。insert,update,delete可能会有业务逻辑,数据库事务,甚至死锁的问题。create直接没法做。

结论:建议用1.

猜你喜欢

转载自blog.csdn.net/nayi_224/article/details/80539185
今日推荐