【发布时间】:2016-06-16 17:22:37
【问题描述】:
似乎比较运算符修饰符 ALL 没有按预期工作。我正在使用 Microsoft SQL Server 2008 R2 Management Studio (v10.50.4000.0)。
declare @AllTest table
(
ID int identity,
Crew int,
Iteration int,
Value varchar(200)
)
insert @AllTest
values
(1, 1, 'a'),
(2, 1, 'b'),
(3, 1, NULL),
(1, 2, 'd'),
(1, 3, 'e'),
(3, 2, NULL),
(2, 2, 'a'),
(2, 3, 'b'),
(1, 4, NULL),
(1, 5, 'f')
select
*
from
@AllTest
where
1 = 1
and Crew = 1
and Value is not NULL
select
*
from
@AllTest
where
1 = 1
and Crew = 1
and Value is not NULL
and Iteration = all(select v from (values (1),(2),(3)) as t(v))
select
*
from
@AllTest
where
1 = 1
and Crew = 1
and Value is not NULL
and Iteration != all(select v from (values (1),(2),(3)) as t(v))
'Iteration = all' 的查询不返回任何结果,但它应该返回。 'Iteration != all' 的查询按预期工作。 (然而,这样的结果似乎更容易通过 'Iteration not in' 来实现,并且不需要使用诸如表值构造函数或联合之类的子查询来呈现值。)
无论如何,ALL 不能像所示的那样工作似乎真的很奇怪,并且像 'Iteration = all(1,2,3)' 或 'Iteration != all(1,2,3)' 这样的简单表达式是无效!
select
*
from
@AllTest
where
1 = 1
and Crew = 1
and Value is not NULL
and Iteration != all(1,2,3)
这是我的 SQL Server 版本的问题还是我缺少什么?
如果 ALL 不起作用,那么构建查询以表现并返回应该使用 ALL 生成的结果的最佳替代方法是什么?
编辑: 对于没有让我的问题对预期结果更清楚,我深表歉意。我正在寻找一种方法,仅当存在 Value NULL 且 Iteration 等于 1 和 2 和 3 且没有丢失行的行时才返回结果。
我希望这是有道理的。
另外,我看到 'Iteration = all' 是如何寻找单行的,我尝试了 'Iteration in all' 这在语义上更有意义,但它被认为是不正确的语法。
【问题讨论】:
-
您需要解释您希望结果是什么以及为什么如果您想要替代方案,我想您可能想要某种关系除法查询。
标签: sql-server comparison-operators modifier