[SQL]分组排练进行更新

时间:2023-03-09 16:51:13
[SQL]分组排练进行更新
--方法(一):分组排练进行更新
----------------------------------------------------------------------------------------------------------------
create table a(id int identity,aid varchar(),name varchar())
insert into a select , 'a '
insert into a select , 'a '
insert into a select , 'a '
insert into a select , 'b '
insert into a select , 'b '
insert into a select , 'c '
insert into a select , 'a '
insert into a select , 'a ' create table b(id int identity,aid varchar(),name varchar())
insert into b select , 'a '
insert into b select , 'a '
insert into b select , 'b '
insert into b select , 'b '
insert into b select , 'b '
insert into b select , 'd ' select px=(select count() from a where aid = t.aid and name= t.name and id<t.id)+,* into #tb1 from a t
select px=(select count () from b where aid = t.aid and name= t.name and id<t.id)+,* into #tb2 from b t select * from #tb1
select * from #tb2
delete a where id in
(select id from #tb1 a where not exists(select * from #tb2 b where b.px=a.px and b.aid=a.aid and b.name=a.name)
and a.aid=)
insert into a
select aid,name from #tb2 a where not exists(select * from #tb1 b where b.px=a.px and b.aid=a.aid and b.name=a.name) -------------------------------------------------------------------------------------------------------------------
--方法(二):分组排练进行更新 create table tb1(ID int,Account int)
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, )
insert into tb1 values(, ) create table tb2(ID int,Account int)
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, )
insert into tb2 values(, ) select px=identity(int, ,),* into #t1 from tb1 order by account,id
select px=identity(int ,,),* into #t2 from tb2 order by account,id select a.id,a.account from #t1 a where not exists(select * from #t2 b where a.px=b.px and a.account=b.account)
-----------------------------------------------------------------------------------------------------------------
--删除临时表 drop table tb1,tb2,#t1,#t2