Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2006-12-27 : 16:28:37
|
Hi All,I need help with a simple update statement using inner join.Lets say I have a table called Test with following fields:ProductID(varchar type), Name(varchar type)I have another Table called Test2 with following fields:ProductID(varchar type), NewProductID(nvarchar type), Name(varchar type)Now I want to update all the ProductID in Test1 table with the NewProductID from Test2 table, where both table ProductID are similar.Let say in Test1 I have the following records.ProductID,Name123,prod1456,prod2789,prod3Let say in Test2 I have the following records.ProductID,NewProductID,Name123,123NN,prod1456,456UU,prod2789,789TT,prod3I want to update the Test1 records as follows:ProductID,Name123NN,prod1456UU,prod2789TT,prod3Since i have thousands of records, I want to create an update statement using inner join.How can I achieve this. Please let me know.Thanks a million in advance. |
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2006-12-27 : 16:51:35
|
I know the soultion.update test1set test1.productid=test2.newproductidfrom test1 join test2on test1.productid=test2.productid |
 |
|
BrianArmstrong
Starting Member
6 Posts |
Posted - 2006-12-27 : 16:54:01
|
Is this what you want to do?UPDATE Test1SET ProductID = T2.NewProductIDFROM Test1 T1 INNER JOIN Test2 T2 ON T1.ProductID = T2.ProductID |
 |
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2006-12-27 : 17:13:37
|
Yes. Its the samething...isn't it?The only difference is, that you used alias. Right?But thanks for your help. |
 |
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-01-01 : 03:49:06
|
quote: Originally posted by zeeshan13 Yes. Its the samething...isn't it?The only difference is, that you used alias. Right?But thanks for your help.
Yes. Alias can be useful if the table name is long Also if you use table variable, you cant directly refer that as you specified and alias is the way to goMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|