单个存储过程中的多个独立事务

时间:2016-02-15 17:48:36

标签: sql-server-2008 transactions sql-server-2012

我必须从多个表中插入和删除,并且我想在一个存储过程中完成所有这些操作,因此我可以拥有如下所示的多个事务块,以及在每个事务中处理错误的最佳方法。请告知

      BEGIN TRANSACTION [TRANS1]
     BEGIN TRY
 Insert into  ArchiveTable select * from Completed (nolock) 

 delete from Completed(nolock) where convert(date,LastModified,101) > getdate() 
 COMMIT TRANSACTION [TRANS1]
    END TRY
BEGIN CATCH
 ROLLBACK TRANSACTION [TRANS1]
PRINT ERROR_MESSAGE() 
 END CATCH 
     BEGIN TRANSACTION [TRANS2]
     BEGIN TRY
 Insert into  ArchiveTable2 select * from New (nolock) 

 delete from New (nolock) where convert(date,LastModified,101) > getdate() 
 COMMIT TRANSACTION [TRANS2]
    END TRY
BEGIN CATCH
 ROLLBACK TRANSACTION [TRANS2]
PRINT ERROR_MESSAGE() 
 END CATCH 

0 个答案:

没有答案