我正在运行查询以根据位置,建筑物编号等选择审批者凭据。我想要为特定位置设置默认审批者,因此在表中创建一个条目,其中包含Site值和NULL AcctDept。结果集预计是一行。
以下是查询:
select distinct CS, Primary, Backup1, Backup2
from Approvals
where AcctDept = #ApprovalInformation.Account#
and Building = #ApprovalInformation.Building#
and Site = #ApprovalInformation.Site#
and AcctDept is not null
and Building is not null
union
select distinct CS, Primary, Backup1, Backup2
from Approvals
where AcctDept = #ApprovalInformation.Account#
and Site = #ApprovalInformation.Site#
and AcctDept is not null
and Building is null
union select distinct CS, Primary, Backup1, Backup2
from Approvals
where Building = #ApprovalInformation.Building#
and Site = #ApprovalInformation.Site#
and AcctDept is null
and Building is not null
union
select distinct CS, Primary, Backup1, Backup2
from Approvals
where Site = #ApprovalInformation.Site#
and AcctDept is null
and Building is null
and NOT EXISTS (
select *
from Approvals
where AcctDept = #ApprovalInformation.Account#
and Building = #ApprovalInformation.Building#
and Site = #ApprovalInformation.Site#
and AcctDept is not null
and Building is not null)
我的想法是,如果第一个SELECT返回一个值,则不需要默认值,NOT EXISTS语句将评估为false。 (第一个选择只是复制/粘贴到最后一个语句的NOT EXISTS中)
但是,当我运行它时,它总是评估为TRUE。
我遗漏的NOT EXISTS的行为一定有什么意义吗?
答案 0 :(得分:1)
select top 1 CS, Primary, Backup1, Backup2
from Approvals
where (AcctDept = #ApprovalInformation.Account# or AcctDept is null)
and (Building = #ApprovalInformation.Building# or Building is null)
and Site = #ApprovalInformation.Site#
order by AcctDept, Building
oracle中的默认排序是asc,nulls last。使用前1个子句,这将为您提供一条记录,其中包含非空的AcctDept和Building(如果可用)。未经测试,因为没有提供SQLFiddle。