2013-03-12 56 views
0

我一直在嘗試將開關添加到以下腳本中。
如果@IgnoreExclusions = 1那麼我不想排除Controltb_AssocAccounts_ExcludedSurnameDOBControltb_AssocAccounts_ExcludedDOB中的任何值?將開關添加到WHERE clausse

我已經包含了我的一個嘗試,但我不認爲這是非常可讀的,而且我不確定它是否可靠地工作,因爲NULL可能是排除列表中的一個值。

DECLARE @IgnoreExclusions TINYINT = 1; 

SELECT ua.UserAccountKey,   
FROM #Accounts x 
     INNER JOIN WH.dbo.vw_DimUserAccount ua  
     ON 
     (--surname and DOB need to match 
     x.Surname = ua.Surname AND 
     x.DOB = ua.DOB 
     ) 
     AND 
     x.UserAccountKey <> ua.UserAccountKey 
WHERE EXISTS 
      (
      SELECT x.Surname, x.DOB 
      EXCEPT 
      SELECT ExcludedSurname,ExcludedDOB 
      FROM WH.dbo.Controltb_AssocAccounts_ExcludedSurnameDOB 
     ) 
     AND 
     EXISTS 
      (
      SELECT x.DOB 
       --SELECT CASE WHEN @IgnoreExclusions = 1 THEN NULL ELSE x.DOB END --<<<<ATTEMPT 
      EXCEPT 
      SELECT ExcludedDOB 
      FROM WH.dbo.Controltb_AssocAccounts_ExcludedDOB 
     ) 
GROUP BY ua.UserAccountKey; 

回答

2

我不知道你正在使用的變種SQL的,但不能簡單OR條款做的伎倆?

DECLARE @IgnoreExclusions TINYINT = 1; 

SELECT ua.UserAccountKey,   
FROM #Accounts x 
    INNER JOIN WH.dbo.vw_DimUserAccount ua  
     ON x.Surname = ua.Surname 
      AND x.DOB = ua.DOB 
      AND x.UserAccountKey <> ua.UserAccountKey 
WHERE EXISTS 
    (
     SELECT x.Surname, x.DOB 
     EXCEPT 
     SELECT ExcludedSurname,ExcludedDOB 
     FROM WH.dbo.Controltb_AssocAccounts_ExcludedSurnameDOB 
    ) 
    AND 
    (
     @IgnoreExclusions = 1 OR EXISTS 
     (
       SELECT x.DOB 
       EXCEPT 
       SELECT ExcludedDOB 
       FROM WH.dbo.Controltb_AssocAccounts_ExcludedDOB 
     ) 
    ) 
GROUP BY ua.UserAccountKey; 
+0

+1 look bob on - I'll give it a go – whytheq 2013-03-12 18:41:22