当前位置:网站首页>解决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>
三、总结
如果对你有帮助,请帮忙点个赞或者收藏吧!你的关注就是我最大的动力。边栏推荐
- What are the methods of array objects in Es5 and what are the new methods in ES6
- C language: realize the simple function of address book through structure
- 【CPU占用高】software_reporter_tool.exe
- Gan: generative advantageous nets -- paper analysis and the mathematical concepts behind it
- Flask Development & get/post request
- 7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
- HashSet add
- C language: some self realization of string functions
- Visual studio 2019 new OpenGL project does not need to reconfigure the environment
- 【ARXIV2204】Simple Baselines for Image Restoration
猜你喜欢

11. < tag dynamic programming and subsequence, subarray> lt.115. Different subsequences + Lt. 583. Deletion of two strings DBC

在ruoyi生成的对应数据库的代码 之后我该怎么做才能做出下边图片的样子

MySQL 默认隔离级别是RR,为什么阿里等大厂会改成RC?

7.<tag-字符串和API的取舍>补充: 剑指 Offer 05. 替换空格

Visual studio 2019 new OpenGL project does not need to reconfigure the environment

The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies

How about ink cloud?

Transformer -- Analysis and application of attention model

Mysql基本查询

多系统架构设计思考
随机推荐
The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies
2022 summer practice (first week)
7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces
【ARXIV2205】EdgeViTs: Competing Light-weight CNNs on Mobile Devices with Vision Transformers
Class class added in ES6
这种动态规划你见过吗——状态机动态规划之股票问题(中)
Implementation of simple upload function in PHP development
Gym 101911c bacteria (minimum stack)
From the basic concept of micro services to core components - explain and analyze through an example
Handling of web page image loading errors
regular expression
Making RPM packages with nfpm
Message forwarding mechanism -- save your program from crashing
ES6 new variable modifiers let and const, new basic data type symbol
php7.1 连接sqlserver2008r2 如何测试成功
阿里怎么用DDD来拆分微服务?
FreeRTOS个人笔记-任务通知
After ruoyi generates the code corresponding to the database, what should I do to make the following image look like
Reading notes of SMT practical guide 1
Antd setfieldsvalue warning problem cannot use 'setfieldsvalue' until you use 'getfielddecorator' or