开发者

SQL Server: WHERE clause to ignore blank GUID

SELECT ...
FROM ...
WHERE CASE WHEN @ActivityUID = '00000000-0000-0000-0000-00000开发者_如何学JAVA0000000' 
    THEN
        -- do nothing
    ELSE
        (Activity.ActivityUID = @ActivityUID)
    END

When a blank Guid is passed in via ActivityUID then I want the WHERE statement to ignore Activity.ActivityUID = @ActivityUID


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


This removes the need for an OR clause because NULL comparisons fail.

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

See if there is a performance difference...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜