如何 select 从 dual in oracle SQL 中获取 10,000 个唯一 ID 的列表

how to select a list of 10,000 unique ids from dual in oracle SQL

所以我无法创建或编辑 tables(我是具有只读权限的用户)并且我想查找 10,000 个唯一 ID。我不能将它们放在 IN() 语句中,因为 oracle 限制超过 1000 个项目。

是否可以从 oracle 中的 DUAL table select 整个列表?类似于:

select  
'id123,id8923,id32983,id032098,id308230,id32983289'  
from DUAL

Oracle 仍然不支持 VALUES 行构造函数,因此只有两个丑陋的解决方法:

1000 项限制不适用于多列 IN 条件

Expression Lists

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.

所以你可以这样做:

where (1,id) in ( (1,'id123'),
                  (1,'id8923'),
                  (1,'id32983'), 
                  (1,'id032098'), .... )

或者使用一个又大又丑的 UNION ALL:

with idlist (xid) as (
  select 'id123' from dual union all 
  select 'id8923' from dual union all 
  .....
  select 'id32983' from dual 
)
select ...
from some_table
where id in (select xid from idlist);

一个解决方案是 WITH 子句:

with ids as (
   select 'id123' as uid from dual union all
   select 'id8923' as uid from dual union all
   select 'id32983' as uid from dual union all
   select 'id032098' as uid from dual union all
   select 'id308230' as uid from dual union all
   select 'id32983289' as uid from dual 
)
select *
from ids
     join your_table yt
     on yt.id = ids.uid  

这可能看起来有点繁琐,但大概您在电子表格或其他文件中有 UID 列表。如果是这样,使用正则表达式生成那些 select 语句是小菜一碟。只需将列剪切并粘贴到支持正则表达式搜索和替换的编辑器中即可。

另一个解决方法

select  *

from    t

where   id in ('id1','id2','id3',...,'id1000')
     or id in ('id1001','id1002','id1003',...,'id2000')
     or id in ('id2001','id2002','id2003',...,'id3000')
     or ...

使用 collection(它们不像 IN 子句那样限制为 1000 个项目):

SELECT COLUMN_VALUE AS id
FROM   TABLE(
         SYS.ODCIVARCHAR2LIST(
           'id123', 'id8923', 'id32983', 'id032098', 'id308230', 'id32983289'
         )
       )

SYS.ODCIVARCHAR2LISTSYS.ODCINUMBERLISTSYS 架构中提供的集合类型。

您可以直接将此连接到您 SELECT 来自的任何 table,而无需使用 DUAL table:

SELECT y.*
FROM   your_table y
       INNER JOIN TABLE(
         SYS.ODCIVARCHAR2LIST(
           'id123', 'id8923', 'id32983', 'id032098', 'id308230', 'id32983289'
         )
       ) i
       ON (y.id = i.COLUMN_VALUE);

如果可以创建一个集合类型,那么您甚至不需要 TABLE 表达式,可以使用 MEMBER OF 运算符直接在 WHERE 子句中使用它:

CREATE OR REPLACE TYPE stringlist IS TABLE OF VARCHAR2(200);
/
SELECT *
FROM   yourtable
WHERE  id MEMBER OF stringlist(
                      'id123', 'id8923', 'id32983', 'id032098', 'id308230', 'id32983289'
                    );

您甚至可以将值作为绑定参数传递 - 请参阅