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 |
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2009-05-27 : 09:19:25
|
| I want to count records in one table(orderrebatehistory) and update another table(orhrecordcount) with the count.I've tried this but the update is not working.update orhrecordcountset recordcount = (select count(*) from orderrebatehistory) |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-27 : 09:23:36
|
| You may need something like thisupdate t1set recordcount = (select count(*) from orderrebatehistory where keycol=t1.keycol)from orhrecordcount as t1MadhivananFailing to plan is Planning to fail |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-27 : 09:25:36
|
| orupdate t1set recordcount = t2.count from orhrecordcount as t1 inner join (select keycol,count(*) as count from orderrebatehistory group by keycol) as t2on keycol=t1.keycolMadhivananFailing to plan is Planning to fail |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2009-05-27 : 09:26:36
|
| I do not have a key in my orhrecordcount table. I was just using it as a place to store the count. One record one field. |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-05-27 : 09:31:27
|
| What was wrong with your query?Did you get error?MadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-27 : 09:31:33
|
quote: Originally posted by Vack I do not have a key in my orhrecordcount table. I was just using it as a place to store the count. One record one field.
is it for temporary storage ? Why use a table ? an integer variable will be able to do thatdeclare @cnt intselect @cnt = count(*)from orderrebatehistory KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
Vack
Aged Yak Warrior
530 Posts |
Posted - 2009-05-27 : 09:33:03
|
| I added a key column to the orhrecordcount and that seemed to have solved my problem. Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-05-27 : 09:42:38
|
quote: Originally posted by Vack I added a key column to the orhrecordcount and that seemed to have solved my problem. Thanks
why use a table just to store a count value? |
 |
|
|
|
|
|