--改一点 update 表A set sum=b.num from (select id,count(*) as num from 表B group by id) b where 表A.id=b.id
update 表A a set sum = (select count(*) from 表B where id = a.id)
declare @v_id int,@v_sum float declare v_cursor cursor for select id from bopen v_cursor fetch next from v_cursor into @v_idwhile @@fetch_status = 0 begin set @v_sum = 0 set @v_sum = ( select count(id) from b where id = @v_id ) if not exists ( select id from a where id = @v_id ) insert into a(id,sum) values(@v_id,@v_sum) else update a set sum = @v_sum where id = @v_id fetch next from v_cursor into @v_id end close v_cursor deallocate v_cursor
declare @v_id int,@v_sum float,@v_times declare v_cursor cursor for select id,count(*) as times from b group by id begin tran open v_cursor fetch next from v_cursor into @v_id,@v_timeswhile @@fetch_status = 0 begin if not exists ( select id from a where id = @v_id ) insert into a(id,sum) values(@v_id,@v_times) else update a set sum = @v_times where id = @v_id fetch next from v_cursor into @v_id,@v_times end close v_cursor deallocate v_cursor commit tran
update 表A set sum=b.num from (select id,count(*) as num from 表B group by id) b
where 表A.id=b.id
declare v_cursor cursor for
select id from bopen v_cursor
fetch next from v_cursor into @v_idwhile @@fetch_status = 0
begin
set @v_sum = 0
set @v_sum = ( select count(id) from b where id = @v_id )
if not exists ( select id from a where id = @v_id )
insert into a(id,sum) values(@v_id,@v_sum)
else
update a set sum = @v_sum where id = @v_id
fetch next from v_cursor into @v_id
end
close v_cursor
deallocate v_cursor
declare v_cursor cursor for
select id,count(*) as times from b group by id
begin tran
open v_cursor
fetch next from v_cursor into @v_id,@v_timeswhile @@fetch_status = 0
begin
if not exists ( select id from a where id = @v_id )
insert into a(id,sum) values(@v_id,@v_times)
else
update a set sum = @v_times where id = @v_id
fetch next from v_cursor into @v_id,@v_times
end
close v_cursor
deallocate v_cursor
commit tran