22,210
社区成员
发帖
与我相关
我的任务
分享
CREATE PROCEDURE TestAAA
AS
BEGIN
SET NOCOUNT ON;
begin transaction
declare @errorSum int
insert into TB_B(ID,NAME,[TIME])
SELECT ID,NAME,[TIME] from TB_A
where [TIME]<GETDATE()-1
set @errorSum+=@@error
delete from TB_A
where [TIME]<GETDATE()-1
set @errorSum+=@@error
if @errorSum>0
begin
print 'error'
rollback transaction
end
else
begin
print 'ok'
commit transaction
end
END
GO
delete from TB_A
where [TIME] <= dateadd(day,-7,getdate()) --删除7天前数据
CREATE PROCEDURE TestAAA
AS
BEGIN
SET NOCOUNT ON;
begin transaction
declare @errorSum int
insert into TB_B(ID,NAME,[TIME])
SELECT ID,NAME,[TIME] from TB_A
where [TIME]<dateadd(dd,-1,GETDATE())
set @errorSum+=@@error
delete from TB_A
where [TIME] between dateadd(dd,-7,GETDATE()) and dateadd(dd,-1,GETDATE())
set @errorSum+=@@error
if @errorSum>0
begin
print 'error'
rollback transaction
end
else
begin
print 'ok'
commit transaction
end
END
GO
--前一个礼拜的数据,也就是说从今天往前推七天的数据?
CREATE PROCEDURE TestAAA
AS
BEGIN
SET NOCOUNT ON;
begin transaction
declare @errorSum int
insert into TB_B(ID,NAME,[TIME])
SELECT ID,NAME,[TIME] from TB_A
where [TIME]<dateadd(dd,-1,GETDATE())
set @errorSum+=@@error
delete from TB_A
where [TIME]<dateadd(dd,-1,GETDATE())
set @errorSum+=@@error
if @errorSum>0
begin
print 'error'
rollback transaction
end
else
begin
print 'ok'
commit transaction
end
END
GO
--试试
CREATE PROCEDURE TestAAA
AS
BEGIN
SET NOCOUNT ON;
begin transaction
declare @errorSum int
insert into TB_B(ID,NAME,[TIME])
SELECT ID,NAME,[TIME] from TB_A
where [TIME]<DATEDIFF(DAY,-1,GETDATE())
set @errorSum =@errorSum + @@error
delete from TB_A
where [TIME]<DATEDIFF(DAY,-1,GETDATE())
set @errorSum = @errorSum + @@error
if @errorSum>0
begin
print 'error'
rollback transaction
end
else
begin
print 'ok'
commit transaction
end
END
GO