USE StuDB
GO
/****** Object: StoredProcedure [dbo].[proc_live_send_answer_v4] Script Date: 06/20/2017 14:44:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: YJ
-- Create date: 2017-05-11
-- Description: 事务
-- =============================================
ALTER PROCEDURE [dbo].[proc_name]
@answer_id varchar(50),
@answer varchar(10),
@uid bigint,
@roomid int=0 out,
@count int=0 out
AS
begin
set nocount on;
if(charindex(':',@answer_id,1)=0)
return;
set @answer=replace(@answer,',','|');
declare @Activity_No int;
declare @quest_order tinyint;
set @Activity_No = substring(@answer_id,1,charindex(':',@answer_id,1)-1);
set @quest_order=substring(@answer_id,charindex(':',@answer_id,1)+1,len(@answer_id));
--尽量缩短事务占用时间
begin try
begin tran
insert......a
update......b
delete......c
commit tran
select 1 as res,'' as remark
end try
begin catch
if (@@trancount > 0)
rollback;
declare @ErrMESSAGE nvarchar(1024) = error_message(),
@ERRSEVERITY int = error_severity(),
@ERRSTATE int = error_state();
raiserror(@ErrMESSAGE,@ERRSEVERITY,@ERRSTATE);--抛出错误
select 0 as res,'提交失败' as remark
end catch
end
事务注意事项:事务里不能包含事务,也就是说当存储过程调用存储过程的时候,外层存储过程有事务时,被调用的存储过程不能写事务,不然事务回滚不了。