【发布时间】:2013-12-12 01:05:42
【问题描述】:
我有下面的代码,它不应该返回任何值。有没有人可以发现它有什么问题?当我运行脚本时,我得到 0 个值作为回报(0 行受影响),即使我知道应该选择一些行。
这只是整个脚本的一部分,当我运行所有内容时,我收到错误消息“警告:空值被聚合或其他 SET 操作消除”。但我不知道这对我的脚本意味着什么。我在脚本中只有 1 个“分组依据”(由于该编码有效,因此我不在这里发布)。有人知道吗?
下面的代码应该创建一个临时表并向临时表插入满足以下条件的表“StatusHistorik”中的行:
1) [NyStatus]=4(NyStatus 是“StatusHistorik”表中的一列),
2) 变量“EnhetsId”不应存在于表“SKL_AdminKontroll_SkaÅtgärdas”(F) 中。代码:“F.EnhetsId 为空”。
我在这两种情况下都使用了内连接。这应该会在临时表中产生许多观察/行,但什么也不返回。有没有人发现任何可以解释结果缺失的编码错误?
我应该提到,当我“注释掉”最后一个内部连接和 where 语句的最后一部分时,脚本会按预期工作。所以我怀疑这是最后一个内部连接语句在某种程度上是错误的。
declare @temp2 table (
EnhetsId varchar(50),
TjanstId Int,
Tabell varchar(50),
Kommentar ntext,
Uppdaterad datetime
);
WITH ENHET_AVSLUT AS
(
SELECT DISTINCT A.[EnhetsId]
FROM [StatistikinlamningDataSKL].[dbo].[StatusHistorik] A
inner join (
select [EnhetsId], max(SenastUppdaterad) as SenastDatum
from [StatistikinlamningDataSKL].[dbo].[StatusHistorik]
group by [EnhetsId]
) B
on A.[EnhetsId] = B.[EnhetsId] and A.[SenastUppdaterad] = B.SenastDatum
INNER JOIN
StatistikinlamningDataSKL.dbo.SKL_AdminKontroll_SkaÅtgärdas F ON A.EnhetsId = F.EnhetsId
WHERE [NyStatus] = 4 AND F.EnhetsId is null
)
insert into @temp2
(EnhetsId, TjanstId, Tabell, Kommentar, Uppdaterad)
SELECT
EnhetsId, 1, ''GR_PS09_1'', ''OK'', getdate()
from ENHET_AVSLUT
select * from @temp2
最好的问候, 汉内斯
【问题讨论】:
-
对表 SKL_AdminKontroll_SkaÅtgärdas 使用 LEFT OUTER JOIN。
-
谢谢!!它解决了这个问题。虽然,我注意到完整的脚本仍然不起作用,但我认为,由于错误消息:警告:空值被聚合或其他 SET 操作消除。你对这个问题有什么想法吗?
标签: sql sql-server select inner-join where