开发者

SQL Server: WHERE clause to ignore blank GUID

开发者 https://www.devze.com 2023-03-13 03:05 出处:网络
SELECT ... FROM ... WHERE CASE WHEN @ActivityUID = \'00000000-0000-0000-0000-00000开发者_如何学JAVA0000000\'
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

精彩评论

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