[英]NOT EXISTS is returning unexpected results
I am running a query to select approver credentials based on location, building number, etc. I want to have a default approver for a specific location, so an entry is made in the table with a Site value and NULL for Building and AcctDept. 我正在运行一个查询,以根据位置,建筑物编号等选择批准者凭证。我希望有一个特定位置的默认批准者,因此在表中输入了一个Site值,Building and AcctDept的值为NULL。 The result set is expected to be a single row. 结果集应为单行。
Here is the query: 这是查询:
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)
My thought was that if the first SELECT returns a value, then the default is not needed, and the NOT EXISTS statement will evaluate false. 我的想法是,如果第一个SELECT返回一个值,则不需要默认值,并且NOT EXISTS语句将得出false。 (The first select is just copy/pasted into the NOT EXISTS of the last statement) (第一次选择只是复制/粘贴到最后一条语句的NOT EXISTS中)
However, when I run this, it is always evaluating to TRUE. 但是,当我运行此命令时,它始终会评估为TRUE。
There must be something about the behavior of NOT EXISTS that I am missing? 关于我不存在的“不存在”的行为一定有一些遗漏吗?
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
default ordering in oracle is asc, nulls last. oracle中的默认顺序为asc,最后为null。 With the top 1 clause this will give you one record back with non-null AcctDept and Building if available. 有了前1个子句,这将为您提供一条非null的AcctDept和Building(如果可用)的记录。 Untested as no SQLFiddle was provided. 未经测试,因为未提供SQLFiddle。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.