I am trying to put IF..ELSE in WHERE clause as below but getting error message
“Incorrect syntax near the keyword ‘IF’. Incorrect syntax near ‘)’.”
DECLARE @categoryID int
SET @categoryID = 0
SELECT * from SE_Auctions
WHERE ItemCategoryID IN
(
IF @categoryID = 0
SELECT CategoryID from SE_ItemCategory
ELSE
SELECT CategoryID from SE_ItemCategory
WHERE ParentID = @categoryID
OR CategoryID = @categoryID
)
You cannot use an
IFinside a subselect in that manner. Instead, use anANDto test for the value of the variable@categoryIDin theWHEREclause. This will require two()groups with a logicalORbetween them.