SQL Server:WHERE子句忽略空白GUID

时间:2011-06-15 23:54:30

标签: sql sql-server

SELECT ...
FROM ...
WHERE CASE WHEN @ActivityUID = '00000000-0000-0000-0000-000000000000' 
    THEN
        -- do nothing
    ELSE
        (Activity.ActivityUID = @ActivityUID)
    END

当通过ActivityUID传入空白Guid时,我希望WHERE语句忽略Activity.ActivityUID = @ActivityUID

2 个答案:

答案 0 :(得分:3)

WHERE (
         Activity.ActivityUID = @ActivityUID OR 
         @ActivityUID = '00000000-0000-0000-0000-000000000000'
      )

答案 1 :(得分:0)

这消除了对OR子句的需要,因为NULL比较失败。

WHERE
    Activity.ActivityUID = NULLIF(@ActivityUID, '00000000-0000-0000-0000-000000000000')

查看是否存在性能差异......