日期:2014-05-18  浏览次数:20446 次

请教一个备份删除的存储过程怎么写
需求:库中包含2个表,一个是A,一个是B。(两个结构一样)
其中A会通过一个程式不停的往里面插入数据,B是用来备份A表的数据的,当A的记录数(即A表共有多少条记录)大于或等于某值时(如10000)时,则自动将按時間排的前10000條記錄备份到B中,且将已备份的数据在A删除。


------解决方案--------------------
insert B
select top 1000 * from A order by timefield

delete A from (select top 1000 * from A order by timefield) as c where c.keyfield = A.keyfield
------解决方案--------------------
begin tran

insert B
select top 1000 * from A order by timefield

if @@error <> 0
begin
rollback tran
return
end

delete A from (select top 1000 * from A order by timefield) as c where c.keyfield = A.keyfield

if @@error <> 0
begin
rollback tran
return
end

commit tran
------解决方案--------------------
declare cou int
set @cou=count(*) from tablename
if (@cou=1000)
print '1000了 '

------解决方案--------------------
你把它們放在job里執行

declare @i int
set @i=select count(*)from A
if @i> =1000
begin

insert B
select top 1000 * from A order by timefield

delete A from (select top 1000 * from A order by timefield) as c where c.keyfield = A.keyfield

end