DECLARE @PayState int
BEGIN
-- 购买成为会员
BEGIN TRY---------------------开始捕捉异常
BEGIN TRAN------------------开始事务
-- 金币加入用户的账户
insert into test(id,[time]) values(2,getdate())
-- 付费成功
SET @PayState = 0
COMMIT TRAN -------提交事务
END TRY-----------结束捕捉异常
BEGIN CATCH------------有异常被捕获
-- 失败等待再次付费
SET @PayState = -4
insert into test(id,[time]) values(22,getdate())
IF @@TRANCOUNT > 0---------------判断有没有事务
BEGIN
ROLLBACK TRAN----------回滚事务
END
insert into test(id,[time]) values(33,getdate())
END CATCH--------结束异常处理
END
SELECT @PayState