首页 > 解决方案 > 我可以在 Oracle SQL 参数中使用 NULL 或 NOT NULL 吗?

问题描述

假设我在代码的不同位置使用了两个 Oracle SQL 查询:

select *
from employees
where manager_id IS NULL
and etc etc etc...

select *
from employees
where manager_id IS NOT NULL
and etc etc etc...

这两个查询之间的唯一区别是 manager_id 在第一个查询中为 NULL,而在第二个查询中为 NOT NULL。

有没有办法把它写成一个通用的语句?是否可以在参数中传入 NULL 或 NOT NULL?像这样的东西:

select *
from employees
where manager_id = ?
and etc etc etc...

我知道上面的例子不起作用(我知道为什么)。我的问题更接近于是否有比管理两个 99% 相似的单独 SQL 字符串更优雅的解决方案?

标签: sqloracle

解决方案


您将需要使用更复杂的逻辑。也许:

where ( (? = 'not null' and manager_id is not null) or
        (? = 'null' and manager_id is null)
      )

推荐阅读