0

我一直在尝试在以下脚本中添加一个开关。
如果@IgnoreExclusions = 1那时我不想排除Controltb_AssocAccounts_ExcludedSurnameDOB或 中的任何值Controltb_AssocAccounts_ExcludedDOB

我已经包含了我的一项尝试,但我认为这不是很可读,而且我不确定它是否可靠地工作,因为它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;
4

1 回答 1

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;
于 2013-03-12T18:08:11.107 回答