T-sql – update statement with self join

join;sybaset-sqlupdate

I needed to copy information from one row of a table to another.
I noticed I could do the following:

update address
set col1 = a2.col1,
col2 = a2.col2,
.....etc
from address a1, address a2
where a1.id = @to and a2.id = @from

However, the above SQL seems kind of ambiguous.
How do I know it's going to update row @to with data of @from and not the otherway around?

Edit: Note: while I have not done extensive testing, this query does seem to work as I try it against our development database. It simply updates the one @to row with results from the @from row.

Best Answer

To be less ambiguous you can prepend the alias in the set clause. (Taken from 孔夫子 in the comments, who did not submit an answer.)

update address
set a1.col1 = a2.col1,
a1.col2 = a2.col2,
.....etc
from address a1, address a2
where a1.id = @to and a2.id = @from