存储过程事务

BEGIN TRY --开始捕捉异常
BEGIN TRANSACTION --开始 事务
insert into DictInfoes values(' 1111',0) --插入数据
select 1/0
--除0
insert into DictInfoes values(' 1111',0) --插入数据
COMOKIT -- 提交事务
ENDTRY-结束捕捉异常
BEGIN CATCH
--开始处理异常

IF @@TRANCOUNT > 0 -- 判断是否发生事务
R0LLBACK -- 回滚
DECLARE @ErrMsg nvarchar (4000),@ErrSeverity int --定义变里
SELECT @ErrMsg = ERROR_ _MESSAGE(),
@ErrSeverity = ERROR_ SEVERITY() -- 查询错误信息
RAISERROR (@ErrMsg,@ErrSeverity, 1) -- 抛出错误信息
END CATCH
--结束处理异常

 

 

 

-------------------------

勿悔 20:29:26
现在方便就发来吧

黄晨旭15093312145 20:29:38
create proc DelStudent(@Sid int,@msg int out)
as
begin
begin try
begin tran       --开始事务
declare @ClassId int
        select @ClassId = ClassId from Student where Sid = @Sid
        delete from Student where Sid = @Sid
        update Class set StuCount = StuCount - 1 where Cid = @ClassId
        set @msg = 1
commit             --提交事务
end try
begin catch
rollback tran      --回滚事务
set @msg = 0
end catch
end

 

posted @ 2020-07-08 20:40  G蠢蠢  阅读(162)  评论(0编辑  收藏  举报