有人给我邮寄了一个包含代码的列表,我需要检查每个代码是否确实存在于oracle 10数据库表中。
列表如下所示:
code1, code2, code3
当然,对于列表中的每个项目,我都可以做
select id from my_table where code = 'code1'.
但这将很耗时,而且不是很优雅。我想报告一个列表,例如:
code1 present
code2 X
code3 X
code4 present
我隐约知道 oracle 的 With 语句,但我不确定如何将它与值列表而不是子查询一起使用。
反向可以像这样实现(查看数据库中的所有代码,并检查它们是否在同事提供的列表中):
select code, case when code in ('code1', 'code2', 'code3')
then 'present'
else 'X' end
from my_table;
如果这对您不起作用,您可以尝试以下操作:
-- you need a special type for your request. Adapt dimensions if necessary
create type codes as varray(100) of varchar(100);
select c.column_value, case when exists (
select 1
from my_table m
where m.code = c.column_value
) then 'present' else 'X' end
from table(codes('code1', 'code2', 'code3')) c;
-- drop that type again
drop type codes;