日期:2014-05-19  浏览次数:20388 次

要疯了,,有好方法吗?
有个系统,用的SQLSERVER   ,目前某表(暂取:A)有100W条记录,A表里有个时间字段(DateTime),现在就想在A中保留3个月的数据,其他数据自动移到A_His表中.

有好的办法吗?数据要有安全保障哦?

------解决方案--------------------
insert into A_His
select * from A
where datediff(month,时间字段,getdate()) <=3

delete from A where datediff(month,时间字段,getdate()) <=3
------解决方案--------------------
可以用作业做.先更新一个表,再删除表中的数据
------解决方案--------------------
把 gahade(与君共勉) 的语句写成一个数据库作业,定期执行即可.

------解决方案--------------------
create proc sp_proc_insert
as
BEGIN TRANSACTION
insert into A_His
select * from A
where datediff(month,时间字段,getdate()) <=3

delete from A where datediff(month,时间字段,getdate()) <=3
COMMIT TRANSACTION
安全问题,那就加一个事务吧!

------解决方案--------------------
补充下楼上的.
要对@@error进行判断.

------解决方案--------------------
每次执行前先备份数据下,
------解决方案--------------------
up
------解决方案--------------------
路过学习,多多交流!
------解决方案--------------------
定期备份
如果还不放心就在备份以后再烧到盘上
------解决方案--------------------

insert into A_His
select * from A
where datediff(month,时间字段,getdate()) <=3

delete from A where datediff(month,时间字段,getdate()) <=3

------------------------
按此君的做
------解决方案--------------------
二中方法:

第一种,对时间的概念不是很严格,那么就可以做一个作业,让他在一定的时间内执行把超过时间的纪录移到另一个表

第二中,时时移动。在这个表中增加触发器,在INSERT、UPDATE(如果时间的字段不参与更新的话,这个可以去掉)时执行语句把符合时间条件的数据移动到另一个表


比较2种做法,利弊显然易见,楼主自己衡量~
------解决方案--------------------
在触发器中这样写
Create trigger 触发器名 on A
FOR INSERT
as
insert into A_His
select * from A
where datediff(month,时间字段,getdate()) <=3
delete from A where datediff(month,时间字段,getdate()) <=3