March 7, 2011 at 12:08 pm
I am maintaining an UPDATE query. The source of the update has more than one match for some of the values in the target table. How do I know (and control) what the ultimate value will be in the updated columns when there is an update with a multiplicity of possible values? Will the result always be the last update - which would depend upon the scan order of the data in the update-from table?
March 7, 2011 at 12:20 pm
Are you doing an update from and the join can have multiple returns? If that is the case then you need to find a way to isolate the values you want.
update tablea set col1 = b.col1
from tablea a
join tableb b on a.id = b.id
where 1 = 1
See how ambiguous that is? Which one does it find?
update tablea set col1 = b.col1
from tablea a
join tableb b on a.id = b.id
where 1 = 1
order by b.LastUpdateDate
Now we added clarity and we know exactly which value we will get.
_______________________________________________________________
Need help? Help us help you.
Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.
Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.
Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/
March 7, 2011 at 1:16 pm
Thank you! I just wanted confirmation of what I suspected.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply