首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2012-01-23 
请教一个备份删除的存储过程怎么写需求:库中包含2个表,一个是A,一个是B。(两个结构一样)其中A会通过一个程

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

热点排行