USE NORTHWIND;
GO
SELECT SHIPCOUNTRY,[ORDERS_ID] =
CASE ORDERID
WHEN ORDERID = 10300
THEN 'I AM FROM 10300'
WHEN ORDERID = 10400
THEN 'I AM FROM 10400'
WHEN ORDERID = 10500
THEN 'I AM FROM 10500'
ELSE 'I AM OUT OF RANGE'
END
FROM ORDERS;
GO
Error - Msg 102, Level 15, State 1, Line 3 Incorrect syntax near '='.
If you have northwind Database in your sql server,you can execute this query.
I don't understand where the issue is.Can anyone help me to resolve this issue?
Try this:
When you are explicit in your initial
CASE xxxx
statement, you can't repeat the variable in yourWHEN
clauses. This would equally work as well:You also can't assign aliases like that. Use the
AS
keyword after yourCASE
block.EDIT: As per Adam Wenger's comment, you can indeed write aliases like
[alias] = fieldname
in your query. Thanks for the correction!