| Author |
Topic |
|
fmardani
Constraint Violating Yak Guru
433 Posts |
Posted - 2005-06-24 : 11:12:32
|
| Hi,Please let me know how to do this update...table1 has several fields (ID, field1, field2, field3)I would like to run an update to update field3 such that when field1 and field3 match then use the ID value and place it in Field3I think this is to do with self join.Thanks |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Seventhnight
Master Smack Fu Yak Hacker
2878 Posts |
Posted - 2005-06-24 : 11:17:52
|
Update yourTableSet field3 = idFrom yourTableWhere field1=field3orUpdate yourTableSet field3 = case when field1=field3 then id else field3 endFrom yourTableCorey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." |
 |
|
|
fmardani
Constraint Violating Yak Guru
433 Posts |
Posted - 2005-06-24 : 11:27:09
|
quote: Originally posted by Seventhnight Update yourTableSet field3 = idFrom yourTableWhere field1=field3orUpdate yourTableSet field3 = case when field1=field3 then id else field3 endFrom yourTableHi,I don't think this would work because:lets say field3 has a value of 12345Then you need to look for 12345 in field1and then get the ID valueThanksCorey Co-worker on The Wizard of Oz "...those three midgets that came out and danced, the freaked me out when I was little. But they are ok now." 
|
 |
|
|
fmardani
Constraint Violating Yak Guru
433 Posts |
Posted - 2005-06-24 : 11:30:57
|
| Hi,I don't think this would work because:lets say field3 has a value of 12345Then you need to look for 12345 in field1and then get the ID value |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-06-24 : 11:41:18
|
are we missing your objective? Looks like it works to me:set nocount ondeclare @tb table ([id] int, col1 int, col2 int, col3 int)insert @tb select 1,20,3,40 union allselect 2,30,3,30 union allselect 3,20,3,20 union allselect 4,21,3,40select * from @tbupdate @tb set col3 = [id] where col1=col3select * from @tb Be One with the OptimizerTG |
 |
|
|
fmardani
Constraint Violating Yak Guru
433 Posts |
Posted - 2005-06-24 : 11:51:56
|
| Hi,Apologiese if my question is not quite clear.I think I should make this more clear.let say you have:ID FeedID ManagerID1 65 962 45 673 96 12As you can see, ManagerID = 96 is as same as the FeedID = 96 and because of this I would like to take the ID = 3 and place 3 instead of 96 in field ManagerIDHope this is more clear.Thank you all |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-06-24 : 13:04:35
|
ok, hows this?set nocount ondeclare @tb table ([id] int, feedid int, Managerid int)insert @tbselect 1, 65, 96 unionselect 2, 45, 67 unionselect 3, 96, 12select * from @tbupdate a set a.ManagerID = b.idfrom @tb ajoin @tb b on a.ManagerID = b.FeedIDselect * from @tb Be One with the OptimizerTG |
 |
|
|
|