Using the quot;INquot; clause with a comma delimited string from the output of a replace() function in Oracle SQL(使用“INOracle SQL 中 replace() 函数输出中带有逗号分隔字符串的子句)
问题描述
我有一个逗号分隔的字符串,我想在语句的IN"子句中使用它.例如:100,101,102
I have an comma delimited string which I want to use in an "IN" clause of the statement. eg: 100,101,102
由于 In 和 "IN" 子句我必须引用单个字符串,所以我使用了替换函数:例如:select ''''||replace('100,101,102',',',''', ''')||'''' from dual;
Since In and "IN" clause I have to quote the individial strings, I use a replace function: eg: select ''''||replace('100,101,102',',',''', ''')||'''' from dual;
上述查询有效,但是,当我尝试将上述查询的输出用作IN"子句的输入时,它不返回任何数据.我只受 SQL 语句的限制,所以我不能使用 PL/SQL 代码.请帮忙.
The above query works, however, when I try to use the output of the above as an input to the "IN" clause, it returns no data. I am restricted by only SQL statements, so I cannot use PL/SQL code. Kindly help.
select * from employee where employee_number in (
select ''''||replace('100,101,102',',',''', ''')||'''' from dual);
以上方法无效.请让我知道我缺少什么.
The above does not work. Please let me know what I am missing.
推荐答案
这种情况下的一般方法是将逗号分隔的列表解析为 Oracle 集合,并在 SQL 语句中使用该集合.Tom Kyte 在他关于 变量 IN 列表.
The general approach in this case would be to parse the comma-separated list into an Oracle collection and to use that collection in your SQL statement. Tom Kyte has an example of this in his discussion on variable IN lists.
假设您从该线程创建 myTableType 类型和 in_list 函数,您应该能够做到
Assuming you create the myTableType type and the in_list function from that thread, you should be able to do
SELECT *
FROM employee
WHERE employee_number IN (
SELECT *
FROM TABLE( in_list( p_your_comma_separated_list ) )
)
这篇关于使用“IN"Oracle SQL 中 replace() 函数输出中带有逗号分隔字符串的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用“IN"Oracle SQL 中 replace() 函数输出中带有逗号分隔字符串的子句


- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- SQL 临时表问题 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01