首页 > 代码库 > example for store procedure with both transcration and error handling
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 ONGOSET QUOTED_IDENTIFIER ONGOALTER proc [dbo].[SP_Biz_Approve_Import]@Id int,@IsImmediate bit,@ApprovedBy int,@OutMsg varchar(100) outputasbegin 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 CATCHend
the following function also are available in CATCH block:
ERROR_NUMBER()
ERROR_MESSAGE()
ERROR_SEVERITY()
ERROR_STATE()
ERROR_LINE()
ERROR_PROCEDURE()
example for store procedure with both transcration and error handling
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。