準備:html
create table Nums(X int);post
目的:只向表中插入一行。url
-------------------------------------------------------------------------------------------------------------------------------------spa
begin transaction tran_A -- 最好是爲事務定義一個名字。htm
insert into Nums(X) values(9);blog
save transaction save_tran; --定義一個事務的保存點、當要回滾事務時,能夠回滾到這裏。事務
insert into Nums(X) values(4),(3),(2),(1);ip
rollback transaction save_tran;--回滾事務到保存點get
commit transaction tran_A;-- 提交事務。
goit
USE [warm]
GO
/****** Object: StoredProcedure [dbo].[P_GetYID] Script Date: 06/15/2019 20:19:45 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create PROCEDURE [dbo].[P_GetYID]
@VouID int output
AS
BEGIN
declare @TC int
select @TC = @@TRANCOUNT
if @TC = 0
begin transaction
else
save transaction T_BAK
-------------------------------------------------
update T_VouID_Bank with(tablockx) set I_VouID = I_VouID + 1,@VouID = I_VouID
if @@ROWCOUNT <> 1 or @@ERROR <> 0 goto ERR
-------------------------------------------------
if @TC = 0
commit transaction
return 0
ERR:
if @TC = 0
rollback transaction
else
rollback transaction T_BAK
return 1
END
GO