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

2023-09-20数据库问题
34

本文介绍了使用“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() 函数输出中带有逗号分隔字符串的子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)...
2024-04-16 数据库问题
13

如何在MySQL中为每个组选择第一行?
How to select the first row for each group in MySQL?(如何在MySQL中为每个组选择第一行?)...
2024-04-16 数据库问题
13

MySQL - 获取最低值
MySQL - Fetching lowest value(MySQL - 获取最低值)...
2024-04-16 数据库问题
8

在 cmakelist.txt 中添加和链接 mysql 库
Add and link mysql libraries in a cmakelist.txt(在 cmakelist.txt 中添加和链接 mysql 库)...
2024-04-16 数据库问题
41

考勤数据库的良好数据库设计(架构)是什么?
What is a good database design (schema) for a attendance database?(考勤数据库的良好数据库设计(架构)是什么?)...
2024-04-16 数据库问题
7