We hope that itβs more efficient than using the OR condition:
SELECT * FROM TaskTicket t WHERE t.[UserName]=@userName AND t.[ID] LIKE COALESCE(@ID,'%')
NB: will only work if the identifier is a non-NULLable field. (You can use CAST and COALESCE on t. [ID] otherwise, but then it is unlikely to be more effective than the OR condition.)
Alternatively, use dynamic SQL in your stored procedure to completely omit the condition t. [ID] if @ID is NULL.
Mark bannister
source share