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 |
|
Happiq
Starting Member
1 Post |
Posted - 2010-10-27 : 17:12:04
|
| Hello All,What I would like to achieve is to update one table based on rows in different table.Table AALET1 LET2 (column names)A 1B 2C nullD nullE 5Table BBLET1 LET2 (column names)1 X2 YI would like to run an update query to replace values in table AA in column LET2 with values from table BB column LET2 where AA.LET2 = BB.LET1so I expect to see sth like that:Table AALET1 LET2 A XB YC nullD nullE 5Here is what I have written and it works:update aa set aa.let2 = (select bb.let2 from bb where bb.let1 = aa.let2)where aa.let2 in (select distinct bb.let1 from bb)Is there any way to modify this query in a way so I do not have to use that last part where aa.let2 in (select distinct bb.let1 from bb)as this probably will cause some performance issue in case it is run against big tablesThanks in advance |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2010-10-27 : 17:52:00
|
| DECLARE @AA Table (Let1 varchar(5),Let2 varchar(5) )DECLARE @BB TABLE (LET1 varchar(5),Let2 varchar(5))INSERT INTO @AASELECT 'A', 1 UNION SELECT 'B', 2 UNION SELECT 'C', null UNION SELECT 'D', null UNION SELECT 'E', 5INSERT @BBSELECT 1,'X' UNIONSELECT 2,'Y'UPDATE ASET A.LET2 = B.LET2FROM @AA as AINNER JOIN @BB as BONA.LET2 = B.LET1JimEveryday I learn something that somebody else already knew |
 |
|
|
|
|
|