I have a Where Clause that checks the existence of rows in a subquery, but I only want to execute that check if a bit is set to 1. So for example:
Select * from Listing l
Where
l.IsDeleted=1
AND CASE WHEN @MustHasPicture = 1 THEN
(
EXISTS
(
SELECT NULL AS [EMPTY]
FROM [dbo].[ListingPictures] AS [lp]
INNER JOIN Listing l ON lp.ListingID=l.ID
)
)
ELSE 1 END = 1
This syntax is wrong, and I'm hoping someone can point me in the right direction. Thanks.