读书人

请问一个备份删除的存储过程如何写

发布时间: 2012-01-23 21:57:28 作者: rapoo

请教一个备份删除的存储过程怎么写
需求:库中包含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

读书人网 >SQL Server

热点推荐