首页 > 解决方案 > PL/SQL 循环遍历列表并查找数据库表中不存在的值

问题描述

我有一个名单。但是这个列表很大(10000+ 项)。我想浏览这个值列表并列出表中不存在的值。

我能够想出这个,

 select i.column_value as country_code
 from table(SYS.DBMS_DEBUG_VC2COLL('AU', 'IN', 'ZA', 'DK', 'CH', 'NL')) i
 where not exists (select null
                   from country c
                   where c.country_code = i.column_value)

但它将提供给函数的值的数量限制为 1000。因此我无法立即提供完整列表

ORA-00939: too many arguments for function

有谁知道解决这个问题。

标签: sqlplsqloracle11g

解决方案


如果您真的被固定的值列表所困扰,您可以在子查询(内联视图)中将几个表集合表达式联合在一起:

select i.column_value as country_code
from (
  select * from table(SYS.odcivarchar2list(
    'AU', 'IN', 'ZA', 'DK', 'CH', 'NL' -- up to 999 entries
  ))
  union all
  select * from table(SYS.odcivarchar2list(
    'AU', 'IN', 'ZA', 'DK', 'CH', 'NL' -- up to 999 entries
  ))
) i
where not exists (select null
                  from country c
                  where c.country_code = i.column_value)

我倾向于使用odcivarchar2list而不是dbms_debug_vc2coll,但它应该适用于任何一个。

如果数据在文件中,并且您可以在数据库服务器上使其可用,则可以将其作为外部表加载,这将减少手动工作。


推荐阅读