Hi,
I have the following sql statement where i'm trying to update multiple rows matching a select statement.
UPDATE Cars
SET engineSize = CASE specCode WHEN 1 THEN value ELSE engineSize END
FROM Cars
INNER JOIN CarSpecs ON CarsSpecs.carID = Cars.carID
My tables are as follows:
Cars
carID engineSize ...
1 0
2 0
CarSpecs
carID specCode value
1 1 1800
1 2 Blue
1 3 Petrol
2 1 2200
2 2 Green
2 3 Petrol
specCode relates to a type of specification I want to update in the Cars table (1 being the engine size)
When I run the query it comes back NULL everytime. The way I see it it should find the specCode = 1 and set the engineSize to 1800 then after it's set it just sets it to the first found value.
Any ideas?
Edit: I need to update multiple columns in Cars table. That's the reason for using CASE, ie:
UPDATE Cars
SET engineSize = CASE specCode WHEN 1 THEN value ELSE engineSize END,
colour = CASE specCode WHEN 2 THEN value ELSE colour END
FROM Cars
INNER JOIN CarSpecs ON CarsSpecs.carID = Cars.carID