使用“IN"Oracle SQL 中 replace() 函数输出中带有逗号分隔字符串的子句

Using the quot;INquot; clause with a comma delimited string from the output of a replace() function in Oracle SQL(使用“INOracle SQL 中 replace() 函数输出中带有逗号分隔字符串的子句)
本文介绍了使用“IN"Oracle 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() 函数输出中带有逗号分隔字符串的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

SQL to Generate Periodic Snapshots from Transactions Table(用于从事务表生成定期快照的SQL)
MyBatis support for multiple databases(MyBatis支持多个数据库)
Oracle 12c SQL: Missing column Headers in result(Oracle 12c SQL:结果中缺少列标题)
SQL query to find the number of customers who shopped for 3 consecutive days in month of January 2020(查询2020年1月连续购物3天的客户数量)
How to get top 10 data weekly (This week, Previous week, Last month, 2 months ago, 3 month ago)(如何每周获取前十大数据(本周、前一周、上个月、2个月前、3个月前))
Select the latest record for an Id per day - Oracle pl sql(选择每天ID的最新记录-Oracle pl SQL)