SQL将一张表的部分数据查询后插入到另一张表并同时删除从原表查询的数据

时间:2022-07-13 00:45:53
SQL将一张表的部分数据查询后插入到另一张表并同时删除从原表查询的数据,比如说是A、A_his两张表,其中A_his是A表的历史表,记录一些历史记录。同时Id,name,scores字段。要将从A表查询出来的部分数据放到历史表A_his中,并要同时删除原表的数据。如何写存储过程,规定凌晨执行!新手求教...急...

2 个解决方案

#1


create proc test
as
set nocount on

insert into A_his(Id,name,scores)
select Id,name,scores
from A
where .................

delete from A
where .................

go

然后定时运行job,规定每天0点定时运行

#2


如果A和A_His结构一致,可用
delete A output deleted.* into A_His from A where ...

#1


create proc test
as
set nocount on

insert into A_his(Id,name,scores)
select Id,name,scores
from A
where .................

delete from A
where .................

go

然后定时运行job,规定每天0点定时运行

#2


如果A和A_His结构一致,可用
delete A output deleted.* into A_His from A where ...