SQL Server 2012 – Update Statements Show Count but Do Not Update Table

sql-server-2012t-sqlupdate

I am facing a strange issue which I have never faced before. I have a update query like below

Update A
Set A.Col10 = E.Col10, A.Col11 = E.Col11
FROM TableA A 
INNER JOIN TableB B on A.Col1 = B.Col1
INNER JOIN TableC C on C.Col1 = B.Col2
INNER JOIN TableD D on D.Col1 = C.Col2
INNER JOIN TableE E on E.Col1 = D.Col2
WHERE D.Col3 BETWEEN @StartDate AND @ENDDate
AND E.Col10 <> A.Col10

When i am running above statement it is giving message that 810 rows are updated. but when I am selecting based on the same criteria I am still getting those 810 rows.

SELECT A.Col10 , E.Col10
FROM TableA A 
INNER JOIN TableB B on A.Col1 = B.Col1
INNER JOIN TableC C on C.Col1 = B.Col2
INNER JOIN TableD D on D.Col1 = C.Col2
INNER JOIN TableE E on E.Col1 = D.Col2
WHERE D.Col3 BETWEEN @StartDate AND @ENDDate
AND E.Col10 <> A.Col10

no matter how many times I update the table it is still showing the same count. now, my question is where I am going wrong and what are those records which got updated when actually no records are updated.

Best Answer

If the result of those joins is not unique, sql server will update A.Col10 with every E.Col10 value that matches your query, so at the end there might be the same amount of rows in the second query as in the first statement
For example - there are 81 A values and 11 E values, one A value match one E value, so sql server will update each A value 10 times = 810. The final result is the same(one A value match one E value), so when you select those values you still get 810 results.