最近开始用到Sql Server的事务了,就总结下
ALTER proc [dbo].[testProc] as begin declare @ExtractionDate datetime, @logID varchar(50),@message varchar(2000) set @ExtractionDate=CONVERT(varchar(100),getdate(),23) set @logID=NEWID() Insert into Testlog(ID,LogContent) values(@logID,'无异常') Begin tran --开启事物 begin try Delete from TestContent where NowTime=@ExtractionDate-3 --删除前三天的记录 --新增数据信息 Insert into TestContent( ID, NowTime ) select NEWID(),GETDATE() if @@error<>0 begin rollback transaction goto errorSum end Commit Tran --递交 errorSum: declare @pageCOunt int select @pageCOunt=COUNT(1) from TestContent where NowTime>=@ExtractionDate end try begin catch --异常处理 set @message='现异常,异常方法:'+ERROR_PROCEDURE() +',异常行号:'+convert(varchar, ERROR_LINE()) +',异常编号:'+CONVERT(varchar,error_number()) +',异常消息:'+ERROR_MESSAGE()+'(' + convert(varchar, getdate()) + ')' --记录日常日志 update Testlog set LogContent=@message where ID=@logID Rollback tran--回滚事务 end catch end
浙公网安备 33010602011771号