First, I would add a check to see if all parameters were zero at runtime, i.e.
IF(COALESCE(@userId, @noteType, @aCode, @bCode, @cCode) IS NULL) BEGIN -- do something here, log, print, return, etc. END
Then, after you confirm that the user has passed something, you can use something like this in your WHERE clause
WHERE userId = COALESCE(@userId, userId) AND noteType = COALESCE(@noteType, noteType) AND aCode = COALESCE(@aCode, aCode) AND bCode = COALESCE(@bCode, bCode) AND cCode = COALESCE(@cCode, cCode)
EDIT: Perhaps I missed the intention that if the parameter was passed as null, then you explicitly want to test the column for null. My suggestion is above where it is assumed that the null parameter means "skip the test in this column".
Alternatively, I believe you can use your original query and add the ANSI_NULLS set parameter at the time the stored procedure is created. For example,
SET ANSI_NULLS OFF GO CREATE PROC sp_myDuplicateCheck....
Effectively this should allow your code to then evaluate the column = null, not the column. I think Kalen Delaney once came up with the ANSI_NULLS and QUOTED_IDENTIFIER options as βsticky options,β because if they are configured at the time the procedure was created, they remain with the procedure at run time, regardless of how the connection is established at that time.
Eric sabine
source share