Reputation: 26226
I have two different tables in the same SQL Server database. In my table T1
I have 4 columns with 10 rows. I have a table T2
with 5 columns. I want to copy the contents of the fifth column in my table T2
to fifth column of table T1
.
However, when I do a cross table update as the following
update T1
set T1.fifthColumn = T2.fifthColumn
from T1, T2
It is updating the 10 rows in T1 with the first-row value (data) of the fifth column in T2. Is there any way that I can copy the values of the fifth column in T2 to T1? I have tried a lot of examples but none of them worked right.
PS: I am using SQL Server 2008.
Upvotes: 0
Views: 422
Reputation: 7997
You have created the extra (5th) column already in T1 and it is the correct datatype, but just nulls, correct? What are you other 4 columns? I assume there is some unique key you can join on? Are the other 4 columns identical across the two tables already?
I would do something like this:
update T1 set T1.fifthColumn = T2.fifthColumn
from T1 left join T2 on T1.keyField = T2.keyField
Upvotes: 1