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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 UPDATE TABLE

Author  Topic 

ben_53
Yak Posting Veteran

67 Posts

Posted - 2011-07-14 : 21:06:34
Hi Experts !!

I have two tables, Table A, TableB. both have same column colA. I need to update tableB ,for matching IDs (colA). table A is populated my other method.
data example:
table A
sysID XYZID
1 100
2 110
3 120

Table B
SysID XYZID
** **
** **
1 (should populate 100)





I have written a query like:

insert into TableB (XYZID)
select XYZID from TableA
inner join TableB
ON TableA.sysID = TableB.sysID


Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-07-15 : 02:14:35
If you want to UPDATE then don't use INSERT.

UPDATE b
SET XYZID = a.XYZID
FROM tableA as a
join tableB as b on a.sysID = b.sysID


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

ben_53
Yak Posting Veteran

67 Posts

Posted - 2011-07-15 : 13:50:28
Hi WebFred,

It worked but its updating every other value in tableB

original data for A
sysID XYZID
1 100
2 110
3 120
4 130

But in TableB, I am getting

SysID XYZID
1 NULL
2 110
3 NULL
4 130


Can you please correct the method.
Thanks
Go to Top of Page

ben_53
Yak Posting Veteran

67 Posts

Posted - 2011-07-15 : 15:07:27
oh, I got it, Thanks Anyways
Go to Top of Page
   

- Advertisement -