Say I have a simple query like this:
SELECT * from A JOIN B on B.refId = A.id
WHERE B.restriction = 123
I would like to change the behaviour as follows:
- If
B.restriction
exists, the query acts like the above - If
B.restriction
isNULL
(or possibly some specified 'magic' value) the clause is ignored
Is there a short, neat way to do this that doesn't rely on IF/ELSE
type logic? I'm specifically using SqlServer (2012 at the oldest).
To clarify, it is allowed for B.restriction
to be Null
. A restriction can be added but if not specified, shouldn't get in the way hence wanting to 'ignore' the where
clause.