In my app, I pass in a CSV list of IDs to include in processing and store them in a temp table. I then include a conditional on the WHERE clause as such:
WHERE Amount > 0
AND ID IN ( SELECT ID FROM #tblIDs )
Is there a way to do something like this:
WHERE Amount > 0
IF LEN( @IDList ) > 0
AND ID IN ( SELECT ID FROM #tblIDs )
ELSE
-- No filtering on ID; include all
The only way I can think of is to build a query string in a VARCHAR and EXEC it, but this is grossly inefficient.
WHERE Amount > 0
AND ID IN ( SELECT ID FROM #tblIDs )
Is there a way to do something like this:
WHERE Amount > 0
IF LEN( @IDList ) > 0
AND ID IN ( SELECT ID FROM #tblIDs )
ELSE
-- No filtering on ID; include all
The only way I can think of is to build a query string in a VARCHAR and EXEC it, but this is grossly inefficient.