Click here to Skip to main content
15,885,842 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
SELECT * FROM ABC WHERE IsActive = 1 AND 
CASE @RETVAL
WHEN 1 THEN (ListingType = 'P')
WHEN 2 THEN (@thisDate BETWEEN SearchStartDate AND SearchEndDate)
WHEN 3 THEN 
(
 (
    (@Country = 96 AND iCountry In (1, 2, 3, 96))
  OR
   (@Country <> 96 AND iCountry = @Country)
  )
)       
END


How to resolve error "incorrect syntax near ="?
Posted

1 solution

You can't use a CASE statement like that in a where clause. The CASE statement can only be on 1 side of the = in a WHERE, so you can't have "ListingType = 'P'" for example.

You just need to do ORs with your @RetVal variable. Something like:
SQL
SELECT * 
FROM ABC 
WHERE IsActive = 1
AND 
(
  (@RetVal = 1 AND ListingType = 'P')
  OR 
  (@RetVal = 2 AND @thisDate BETWEEN Search AND EndDate)
  ...
)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900