I have a procedure with a (slightly more complicated) version 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 required match, but Match2 is optional at the entrance to the procedure and to the regular table. The second match is where the values of the input and / or Match2 table are zero, or where they are the same (non-zero) values.
My question is: is there a more efficient (or even more readable) way to do this?
I have used this method several times, and I feel slightly empty every time (subjective dirt, admittedly).
source
share