The following code is intended to update particular fields only when there’s a value that relates to that field.
It returns an incomplete set of values. Millions of rows are correct, but several thousand rows have values incorrectly set to NULL.
Is this is SQL limitation, or am I missing something?
UPDATE a
SET ResultType1 = CASE WHEN b.[Type] = 'type1' THEN b.value END
,ResultType2 = CASE WHEN b.[Type] = 'type2' THEN b.value END
FROM tableA AS a
INNER JOIN tableB AS b ON a.ID = b.ID
makes me think that you actually want to do this:
So, ResultType1/2 will be set to their existing values if the b.Type conditions are not met, INSTEAD of being set to NULL.
I am assuming that when you say “incomplete set of values”, you mean that some are being set to NULL.
What is causing this behaviour is the implied
ELSE NULLinCASEexpressions.If you actually want to update
ResultType1andResultType2with nulls, but only for those rows that are of different'type', a slightly different query will do: