2016-11-19 59 views
1

這是我以前的帖子的修改版本。請幫我解決一下這個。 如果子查詢我得到正確的結果。但是當我運行完全SP警報案例和現場案件相互覆蓋。存儲過程Case語句互相覆蓋

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]  
+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'