存储过程Case语句相互覆盖

时间:2016-11-19 05:03:06

标签: sql sql-server stored-procedures

这是我之前帖子的修改版本。请帮我解决一下这个。 如果子查询我得到正确的结果。但是当我运行完整sp警报案例和Field案件相互重叠时。

Getting null value in status field, can anyone explain what I am going wrong?

(CASE       
    WHEN EXISTS(SELECT [GunSerialNo] FROM [dbo].[ArmouryIssueGun] 
                WHERE aig.ModifiedOn IS NOT NULL 
                  AND aig.CreatedOn IS NOT NULL)
        THEN 'In Armory'

    WHEN EXISTS(SELECT aig.GunSerialNo 
                FROM [dbo].[ArmouryIssueGun] AS aig 
                INNER JOIN (SELECT * 
                            FROM 
                                (SELECT GunSerialNo, DATEADD(HOUR, EstimatedTime, CreatedOn) AS TIME_ADDED 
                                 FROM [ArmouryIssueGun]) ag 
                            WHERE ag.TIME_ADDED<GETUTCDATE()) abd ON abd.GunSerialNo = aig.GunSerialNo
                WHERE aig.ModifiedOn IS NULL
                  AND aig.CreatedBy IS NOT NULL)
        THEN 'Alert' 

    WHEN EXISTS (SELECT aig.GunSerialNo 
                 FROM [dbo].[ArmouryIssueGun] AS aig 
                 INNER JOIN (SELECT * 
                             FROM 
                                 (SELECT GunSerialNo, DATEADD(HOUR, EstimatedTime, CreatedOn) AS TIME_ADDED 
                                  FROM [ArmouryIssueGun]) ag 
                             WHERE ag.TIME_ADDED>GETUTCDATE()) abd ON abd.GunSerialNo = aig.GunSerialNo
                 WHERE aig.ModifiedOn IS NULL
                   AND aig.CreatedBy IS NOT NULL)
        THEN 'In Field'
 END) AS [Status],
FROM 
    [dbo].[CarryAndUseLicence] cl 
INNER JOIN
    [dbo].[Branch] b ON b.[BranchId] = cl.[BranchId]
INNER JOIN
    [dbo].[Gun] gun ON cl.[GunSerialNo] = gun.[SerialNo]
INNER JOIN
    [dbo].[ArmouryIssueGun] aig ON aig.[StaffId] = cl.[StaffId]     

1 个答案:

答案 0 :(得分:0)

问题是别名 aig 的双重使用。 Exists-query中的别名会覆盖顶部查询中的别名。因此,Exists-query不包括当前记录,但会查看整个表。

工作原理:

WHEN EXISTS(SELECT aig2.GunSerialNo 
            FROM [dbo].[ArmouryIssueGun] AS aig2 
            INNER JOIN (SELECT * 
                        FROM 
                            (SELECT GunSerialNo, DATEADD(HOUR, EstimatedTime, CreatedOn) AS TIME_ADDED 
                             FROM [ArmouryIssueGun]) ag 
                        WHERE ag.TIME_ADDED<GETUTCDATE()) abd ON abd.GunSerialNo = aig.GunSerialNo
            WHERE aig2.ModifiedOn IS NULL
              AND aig2.CreatedBy IS NOT NULL)
    THEN 'Alert' 

WHEN EXISTS (SELECT aig2.GunSerialNo 
             FROM [dbo].[ArmouryIssueGun] AS aig2 
             INNER JOIN (SELECT * 
                         FROM 
                             (SELECT GunSerialNo, DATEADD(HOUR, EstimatedTime, CreatedOn) AS TIME_ADDED 
                              FROM [ArmouryIssueGun]) ag 
                         WHERE ag.TIME_ADDED>GETUTCDATE()) abd ON abd.GunSerialNo = aig.GunSerialNo
             WHERE aig2.ModifiedOn IS NULL
               AND aig2.CreatedBy IS NOT NULL)
    THEN 'In Field'

或更短

WHEN EXISTS(SELECT *
              FROM [ArmouryIssueGun]
             WHERE GunSerialNo = aig.GunSerialNo
               AND DATEADD(HOUR, EstimatedTime, CreatedOn) < GETUTCDATE() 
               AND ModifiedOn IS NULL
               AND CreatedBy  IS NOT NULL
              )
    THEN 'Alert' 

WHEN EXISTS(SELECT *
              FROM [ArmouryIssueGun]
             WHERE GunSerialNo = aig.GunSerialNo
               AND DATEADD(HOUR, EstimatedTime, CreatedOn) > GETUTCDATE() 
               AND ModifiedOn IS NULL
               AND CreatedBy  IS NOT NULL
              )
    THEN 'In Field'