I have the following SQL statement in which I am 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:
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 refers to the type of specification that I want to update in the Cars table (1 - engine size)
When I run the query, it returns NULL every time. As I see it, it should find specCode = 1 and set the engineSize value to 1800, after which it sets it only to the first value found.
Any ideas?
Edit: I need to update several columns in the Cars table. This is the reason for using CASE, i.e.:
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
source share