I have a SP that has a very complex SQL statement(s) where I need to be able to compare some column to NULL e.g.
...
FROM Categories
WHERE PID = @parentID
@parentID
is a SP parameter which can be valid NULL.
PID
(parent ID) is uniqueidentifier
which can also be valid NULL (top level category).
I could use SET ANSI_NULLS OFF
but the documentation says:
In a future version of SQL Server, ANSI_NULLS will always be ON and any applications that explicitly set the option to OFF will generate an error. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.
What can be an elegant way instead of repeating the same query(s) with IS NULL
in case @parentID=NULL
(and also not using dynamic SQL):
IF @parentID IS NULL
SELECT...WHERE PID IS NULL
ELSE
SELECT...WHERE PID = @parentID
EDIT: I want to avoid an IF
because I hate repeating (huge) code.