I have a procedure with a (slightly more complex) version of the below:
CREATE PROC sp_Find_ID (
@Match1 varchar(10),
@Match2 varchar(10)
) AS
DECLARE @ID int
SELECT @ID = ID
FROM Table1
WHERE Match1 = @Match1
AND Coalesce(Match2,@Match2,'') = Coalesce(@Match2,Match2,'')
SELECT @ID ID
Essentially Match1 is a mandatory match, but Match2 is both optional on the input to the procedure, and on the table being searched. The 2nd match succeeds where the input and/or the table Match2 values are null, or where they’re both the same (not null) value.
My question is: Is there a more efficient (or even more readable) way of doing this?
I’ve used this method a few times, and I feel slightly sullied each time (subjective dirtiness admittedly).
The example you provided, using COALESCE/etc is non-sargable. You need to separate things so only what needs to be present in the query is run:
If you want this to occur in a single SQL statement, dynamic SQL is the only real alternative. I highly recommend reading The curse and blessing of dynamic SQL before reading further: