I have a Select like the one below in a stored procedure (shortened for brevity). @param is a parameter to the stored procedure which can be NULL.
SELECT name FROM Table1 WHERE EXISTS (select …. from table2 Where param = @param AND … AND …) AND … AND …
I would like the EXISTS statement (the part in bold) to be used only when @param has a value otherwise ignore it.
I don’t want to use dynamic SQL or temporary tables if possible. I am trying to use a CASE statement to work with the EXISTS statement but with not much luck.
Note that this isn’t a guarantee – the query optimizer will do what it wants. But is should be smart enough to optimize the exists clause away.