不寻常的SQL Server查询结果

时间:2013-03-07 10:12:36

标签: sql-server-2008 sql-server-2008-r2

在运行sql server查询时遇到了异常的查询结果。

需要注意的重要表格是 Code_Structure_Type 。 它包含 Code_Structure_Type_Id smallint,Name char(50)以及此处未使用的其他几列。

我的查询

SELECT DISTINCT A1.A1_Id                    AS Code_Unit_Attribute_Id,
                A1.Name                     AS Code_Unit_Attribute_Name,
                Code_Structure_Type.A1_Name AS Code_Attribute_Type
--, Code_Structure_Type.Code_Structure_Type_Id 
FROM   Defect
       INNER JOIN Defect_Code_Unit
         ON Defect.Defect_Id = Defect_Code_Unit.Defect_Id
       INNER JOIN Code_Unit
         ON Defect_Code_Unit.Code_Unit_Id = Code_Unit.Code_Unit_Id
       INNER JOIN A1
         ON Code_Unit.A1_Id = A1.A1_Id
       INNER JOIN ERA_Module
         ON Defect.ERA_Module_Id = ERA_Module.ERA_Module_Id
       INNER JOIN Code_Unit_Type
         ON Code_Unit.Code_Unit_Type_Id = Code_Unit_Type.Code_Unit_Type_Id
       INNER JOIN Code_Structure_Type
         ON Code_Unit_Type.Code_Structure_Type_Id = Code_Structure_Type.Code_Structure_Type_Id
WHERE  ERA_Module.ERA_Project_Id = 472
       AND ERA_Module.Is_Active = 1
       AND Defect.Is_Closed = 0
       AND Defect_Code_Unit.Is_Active = 1
       AND Code_Structure_Type.Name = 'Pega Rules'
       --AND Code_Structure_Type.Code_Structure_Type_Id = 2 
       AND A1.A1_Id > 0
       AND Code_Unit.Scope_Id = 1 

此查询返回一行不相同的行。此查询的预期结果应为none,因为我们没有'Pega Rules'Code_Structure_Type 的缺陷。

如果我取消注释此行

--, Code_Structure_Type.Code_Structure_Type_Id

我没有得到正确的结果。

此外,如果我从Code_Structure_Type_Id过滤“Pega规则”而不是Code_Structure_Type.Name,我也会得到正确的结果,也就是没有。 为此,我取消注释这一行

--AND Code_Structure_Type.Code_Structure_Type_Id = 2

并评论此行

AND Code_Structure_Type.Name = 'Pega Rules'

所以基本上我做的是同样的事情,表Code_Structure_Type中没有像'Pega Rules'这样的其他名称,甚至没有关闭。该表中只有五行。

我需要了解这里发生的事情。

提前致谢。

执行计划的链接

文件,仅在使用'pega规则'时 - incorrect_original.xml使用'pega rules'+ select - correct_with_added_select.xml使用时使用id - correct_with_Id.xml

0 个答案:

没有答案
相关问题