java.sql.SQLSyntaxErrorException: ORA-01795: 列表中的最大表达式数为 1000

后台报了一些异常日志,查阅后发现在 oracle 数据库中使用 in 关键字条件不能超过 1000 个,当时写查询语句时没有关注这个问题

 

总结一下解决方法

1.分多次查询,对查询要求不高的话。把入参的集合按照每个最大1000个来处理,分几次查询,然后把结果进行汇总,这样就只用改动代码,不用改动SQL。

2.把参数分开但还是一次查询,使用 or 连接

select * from table where id in (1, 2, …, 1000) or id in (1001, …, 1999)

3.在 in 后面接一个查询语句

select * from A where id in (select id from B)

4.与 3 类似,使用 with  as 语法,把条件封装成一个表

with temp as (select * from A)
select * from B where id in (select id from temp)

 

posted @ 2019-05-08 10:57  江湖小小白  阅读(4343)  评论(0编辑  收藏  举报