当前位置:网站首页>解决Oracle使用in语句不能超过1000问题
解决Oracle使用in语句不能超过1000问题
2022-07-28 05:16:00 【Ctrl练习生-谢哥哥blog】
在oracle中,使用in方法查询记录的时候,如果in后面的参数个数超过1000个,那么会发生错误,JDBC会抛出“java.sql.SQLException: ORA-01795: 列表中的最大表达式数为 1000”这个异常。
我的解决方案是:
一、建立临时表
ORACLE临时表有两种类型:会话级的临时表和事务级的临时表。
1、ON COMMIT DELETE ROWS
它是临时表的默认参数,表示临时表中的数据仅在事务过程(Transaction)中有效,当事务提交(COMMIT)后,临时表的暂时段将被自动截断(TRUNCATE),但是临时表的结构 以及元数据还存储在用户的数据字典中。如果临时表完成它的使命后,最好删除临时表,否则数据库会残留很多临时表的表结构和元数据。
2、ON COMMIT PRESERVE ROWS
它表示临时表的内容可以跨事务而存在,不过,当该会话结束时,临时表的暂时段将随着会话的结束而被丢弃,临时表中的数据自然也就随之丢弃。但是临时表的结构以及元数据还存储在用户的数据字典中。如果临时表完成它的使命后,最好删除临时表,否则数据库会残留很多临时表的表结构和元数据。
create global temporary table test_table
(id varchar2(50), name varchar2(10))
on commit preserve rows; --创建临时表(当前会话生效)
--添加数据
insert into test_table VALUES('ID001', 'xgg');
insert into test_table VALUES('ID002', 'xgg2');
select * from test_table; --查询数据
TRUNCATE TABLE test_table; --清空临时表数据
DROP TABLE test_table; --删除临时表
建立临时表之后,in语句里面就可以使用子查询,这样就不会有超过1000报错的问题了
select * from table_name where id in(select id from test_table);
二、使用in() or in()
官方说: A comma-delimited list of expressions can contain no more than 1000 expressions. A comma-delimited list of sets of expressions can contain any number of sets, but each set can contain no more than 1000 expressions这里使用oracle tuple( A comma-delimited list of sets of expressions) 也就是元组,语法如下:
SELECT * FROM TABLE_NAME WHERE (1, COLUMN_NAME) IN
((1, VALUE_1),
(1, VALUE_2),
...
...
...
...
(1, VALUE_1000),
(1, VALUE_1001));
比如我们想要从用户表里通过用户id 查询用户信息可以这样写:
select * from user u where (1, u.id) in ((1, 'id001'),(1,'id002'),(1,'id003'))
上面的语句其实等同于:
select * from user u where (1=1 and u.id='id001') or (1=1 and u.id='id002') or (1=1 and u.id='id003')
大家的工程多数会用ORM框架如MyBatis 我们可以借助MyBatis的foreach 原来是这写:
where u.id in
<foreach collection="userIds" item="item" separator="," open="(" close=")" index="">
#{item}
</foreach>
现在改成:
where (1, u.id) in
<foreach collection="userIds" item="item" separator="," open="(" close=")" index="">
(1, #{item})
</foreach>
三、总结
如果对你有帮助,请帮忙点个赞或者收藏吧!你的关注就是我最大的动力。边栏推荐
- Making RPM packages with nfpm
- Table image extraction based on traditional intersection method and Tesseract OCR
- DELL远程控制卡 使用ipmitools设置ipmi
- How to send and receive reports through outlook in FastReport VCL?
- The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?
- Transformer -- Analysis and application of attention model
- 这种动态规划你见过吗——状态机动态规划之股票问题(中)
- 面试了一位38岁程序员,听说要加班就拒绝了
- Struct模块到底有多实用?一个知识点立马学习
- HDU 2874 connections between cities
猜你喜欢

The default isolation level of MySQL is RR. Why does Alibaba and other large manufacturers change to RC?

【CVPR2022】On the Integration of Self-Attention and Convolution

How about ink cloud?

阿里怎么用DDD来拆分微服务?
![[high CPU consumption] software_ reporter_ tool.exe](/img/3f/2c1ecff0a81ead0448e1215567ede7.png)
[high CPU consumption] software_ reporter_ tool.exe

MySQL(5)

【SLAM】LVI-SAM解析——综述

7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces

【CPU占用高】software_reporter_tool.exe

HashSet add
随机推荐
HDU 3666 the matrix problemdifferential constraint + stack optimization SPFA negative ring
DELL远程控制卡 使用ipmitools设置ipmi
Read the paper -- a CNN RNN framework for clip yield prediction
POJ 3417 network (lca+ differential on tree)
SSLError
Advanced assignment method of ES6 -- Deconstruction assignment
【CVPR2022】On the Integration of Self-Attention and Convolution
【ARXIV2205】Inception Transformer
C language classic 100 question exercise (1~21)
Struct模块到底有多实用?一个知识点立马学习
MySQL date and time function, varchar and date are mutually converted
MySQL practice 45 lectures
Flink mind map
block yandex bot
HDU 1522 marriage is stable
类和对象【中】
C language characters and strings
[paper notes] - low illumination image enhancement - zeroshot - rrdnet Network - 2020-icme
Confused, I'm going to start running in the direction of [test]
How about ink cloud?