0

嗨,我正在其中编写一个过程“pro_reallocate_block”,我正在调用另一个过程“pro_remove_allocation”。如果我为主程序“pro_reallocate_block”放置BEGIN TRANSACTION,如果主程序发生任何错误,它将恢复子程序“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
4

0 回答 0