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 |
|
bilencekic
Posting Yak Master
121 Posts |
Posted - 2006-01-17 : 19:34:02
|
| first TableID = primary key and identity(1,1)ID Food1 1002 3003 400second table.ID RID Food1 1 502 1 703 2 304 3 50...goes like this.how can i update table 1 by getting values from the table 2?firsttable will be like thatID Food1 220...MS BLESS US |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-17 : 20:04:54
|
If i understand you correctly, something like this ?update t1 set t1.Food = t1.Food + (select sum(t2.Food) from table2 t2 where t2.RID = t1.ID) from table1 t1 -----------------'KH' |
 |
|
|
bilencekic
Posting Yak Master
121 Posts |
Posted - 2006-01-17 : 20:36:21
|
| update [A] set YiyecekGeliri = YiyecekGeliri + (Select count([A].OyuncuID)* 5 from [B] inner join [A] on [A].OyuncuID =[B].OyuncuID where BinaID = 11 and [B].OyuncuID = [A].OyuncuID)that doesnt work it effects all rows.Table B containsID OyuncuID BinaID1 9 112 9 113 5 24 9 18on my table Ai only want to update the column YiyecekGeliri by getting the number of BinaID which has BinaID = 11MS BLESS US |
 |
|
|
bilencekic
Posting Yak Master
121 Posts |
Posted - 2006-01-17 : 20:37:29
|
| if i update [A] set YiyecekGeliri = YiyecekGeliri + (Select count(OyuncuID) from [B] where BinaID = 11 and [B].OyuncuID = [A].OyuncuID)use thisall rows in table A effectedi dont want thisMS BLESS US |
 |
|
|
bilencekic
Posting Yak Master
121 Posts |
Posted - 2006-01-17 : 20:49:30
|
| i tried this but when subquery returns more then 1 value it gives error.--declare @Oyun intset @Oyun = (select distinct [B].OyuncuID from [B] where BinaID = 11)update [A] set YiyecekGeliri = YiyecekGeliri + (Select count([A].OyuncuID)* 5 from [B] inner join [B] on [A].OyuncuID =[B].OyuncuID where BinaID = 11 and [A].OyuncuID = @Oyun) where [B].OyuncuID = @OyunMS BLESS US |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-17 : 21:10:45
|
use exists to limit the record for updatesupdate t1 set t1.Food = t1.Food + (select sum(t2.Food) from table2 t2 where t2.RID = t1.ID) from table1 t1 where exists (select * from table2 x where x.RID = t1.ID) -----------------'KH' |
 |
|
|
|
|
|