I have the following query:
UPDATE #Temp_SessionItem SET [Status] =
CASE
WHEN ([AddressFK] IS NULL OR [StatusFK] IS NULL) AND [Status] = 1
THEN 3
WHEN [AddressFK] IS NOT NULL AND [StatusFK] IS NOT NULL AND [Status] = 1
THEN 2
END
The problem is that when it’s executed it doesn’t actually give me the result I am expecting. i.e. it doesn’t set the value of [Status] field to 3 in case [AddressFK] or [StatusFK] is NULL. Instead, it just tries to add NULL to Status when it should add 3.
If I remove the AND [Status] = 1 part of the expression it does return 3 but then it updates also the values that are not [Status] = 1.
Is there anything obvious that I’m missing here?
I’m using SQL Server 2008.
EDIT:
After Royi Namir pointed out to me I realized that the problem must be further down the stored procedure that this query is being executed in. Specifically, #Temp_SessionItem is just a temporary table. After the upper mentioned query I’m trying to update the original table with the following query:
-- update the status of the original session item table
UPDATE UploadSessionItem SET [Status] = T.[Status]
FROM #Temp_SessionItem AS T
WHERE UploadSessionItem.UploadSessionItemId = T.ID
..which is obviously not working since all the [Status] items there are still 1
If you only want to affect rows with a current
STATUSof1, you need aWHEREclause:The update you’ve shown works fine:
Result:
So if the problem is elsewhere, it’s not in a code sample you’ve shown us yet.