zoukankan      html  css  js  c++  java
  • example for store procedure with both transcration and error handling

    USE [Limo_DB]
    GO
    /****** Object:  StoredProcedure [dbo].[SP_Biz_Approve_Import]    Script Date: 09/06/2014 17:59:52 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER proc [dbo].[SP_Biz_Approve_Import]
    @Id int,
    @IsImmediate bit,
    @ApprovedBy int,
    @OutMsg varchar(100) output
    as
    begin
    	
    	begin try
    	begin tran
    	declare @Type nvarchar(1)
    	
    	if(@IsImmediate is null)
    	begin
    		update dbo.Import_Info set Checker=@ApprovedBy,
    		Is_Immediate=null, Status='Cancelled' 
    		where Id=@Id and ([Status]='Pending' or [Status]='Confirmed')
    		SET @OutMsg='S'
    	end
    	else
    	begin
    		update dbo.Import_Info set Checker=@ApprovedBy,
    		Is_Immediate=@IsImmediate, Status=case @IsImmediate when 0 then 'Confirmed' 
    		when 1 then 'Complete' end,
    		Upload_To_Info_Date=case @IsImmediate when 0 then NULL
    		when 1 then GETDATE() end
    		where Id=@Id and ([Status]='Pending' or [Status]='Confirmed')
    	
    	select @Type=[Type] from Import_Info where Id=@Id
    	
    	if (@IsImmediate=1)
    	begin
    		exec [SP_Execute_Import] @Id,@Type
    	end
    	
    	SET @OutMsg='S'
    	end
    	
    
    	COMMIT TRAN
    	END TRY
    	
    	BEGIN CATCH
    		IF @@TRANCOUNT>0
    		BEGIN
    		   SET @OutMsg='F'	
    		   ROLLBACK TRAN
    		   INSERT INTO [dbo].[SYS_ServiceLog] VALUES(GETDATE(),'ERROR','SP_Biz_Approve_Import',ERROR_MESSAGE(),NULL)
    		END
    	END CATCH
    end
    

      the following function also are available in CATCH block:

     ERROR_NUMBER()
     ERROR_MESSAGE()
     ERROR_SEVERITY()
     ERROR_STATE()
     ERROR_LINE()
     ERROR_PROCEDURE()

  • 相关阅读:
    NOIP2016 蚯蚓 题解
    BZOJ 1294 围豆豆 题解
    POJ1852 Ants 题解
    BZOJ 1131 [POI2008] STA-Station 题解
    HDU 5963 朋友 题解
    Codeforces 1292C Xenon's Attack on the Gangs 题解
    Emergency Evacuation 题解
    P4408 逃学的小孩 题解
    UVA11300 Spreading the Wealth 题解
    P2882 Face The Right Way G 题解
  • 原文地址:https://www.cnblogs.com/Jenny90/p/3961578.html
Copyright © 2011-2022 走看看