sql - 我可以在 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 字符串更优雅的解决方案?
解决方案
您将需要使用更复杂的逻辑。也许:
where ( (? = 'not null' and manager_id is not null) or
(? = 'null' and manager_id is null)
)
推荐阅读
- javascript - For循环在javascript中初始化新实例
- python - 有没有一种方法可以暂停一个函数但不影响 Python 中的其他函数?
- actions-on-google - Google Assistant - 数据存储(对话历史)
- python - 如何使用python中的docx或python-docx包将多个绘图图保存在一个文档文件中
- database - MongoDb,克隆数据库错误:非法参数组合:无法指定 --db 和 --uri
- mysql - MySql 8 连接一个字符串
- django - 为什么 django TestCase 不创建测试数据库?
- python - Xpath 选择只返回第一个响应结果
- kubernetes - Kubernetes REST API - 使用适当的示例(java 或 groovy)在容器中执行命令
- python - Qt5 - 在全屏期间强制停留在顶部