quote:
Originally posted by frank43
Hi. Does it matter how the code is ordered in a simple CASE statement e.g. does it matter if X comes after Y or 1 comes after 2?
CASE X
WHEN 1 THEN...
WHEN 2 THEN...
CASE Y
WHEN 1 THEN...
WHEN 2 THEN...
END;
thanks
The way you have written the case expression is incorrect. It is eitherCASE X
WHEN 1 THEN ...
WHEN 2 THEN ...
END
or
CASE
WHEN X = 1 THEN ...
WHEN X = 2 THEN ...
END
The order in which the logical comparisons are made matters. Once it finds a comparison that is true, then it won't evaluate the remaining expressions. For example this will print A and not B:declare @x INT = 1;
SELECT CASE
WHEN @x = 1 THEN 'A'
WHEN @x = 1 THEN 'B'
END