SQL过程 - 开始事务&承诺

时间:2013-02-13 06:59:56

标签: sql-server sql-server-2008

嗨我正在写一个程序“pro_reallocate_block”里面, 我正在调用另一个过程“pro_remove_allocation”。如果我将BEGIN TRANSACTION用于主程序“pro_reallocate_block”,如果主程序中发生任何错误,它将恢复子程序“pro_remove_allocation”。

Create procedure pro_reallocate_block(
@blockId int,
@blockName nvarchar(max)
)    
WITH EXECUTE AS CALLER      
AS      
Begin           

 SET XACT_ABORT, NOCOUNT ON;        
 BEGIN TRY          
 SET TRANSACTION ISOLATION LEVEL SERIALIZABLE        
 BEGIN TRANSACTION        

Declare @AllocatedBlockid int

 exec pro_remove_allocation @blockId 
 if((select COUNT(blockId) from blocks where blockId=@blockId)>0)
    select * from blocks where blockId=@blockId 
    else 
        select * from blocks where bdefault=1
 COMMIT TRANSACTION        
 SET TRANSACTION ISOLATION LEVEL READ COMMITTED        
 END TRY        

 BEGIN CATCH        
  IF XACT_STATE() <> 0        
  BEGIN        
   ROLLBACK TRANSACTION        

   DECLARE @ErrorMessage NVARCHAR(4000);        
   DECLARE @ErrorSeverity INT;        
   DECLARE @ErrorState INT;        

   SELECT @ErrorMessage = ERROR_MESSAGE(),        
       @ErrorSeverity = ERROR_SEVERITY(),        
       @ErrorState = ERROR_STATE();        

   RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);        
  END        
 END CATCH    
end

0 个答案:

没有答案