第一步:创建演示程序:打开SQL SERVER查询分析器,在SQL SERVER测试数据库中执行下列脚本(脚本执行操作:创建表testtable,并插入一条记录;创建存储过程test):
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Test]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[Test]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[testtable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[testtable]
GO
CREATE TABLE [dbo].[testtable] (
[testid] [int] NULL ,
[counts] [int] NULL
) ON [PRIMARY]
GO
insert into testtable (testid,counts) values (1,0)
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE Procedure dbo.Test
as
declare @count int
begin tran TEST
select @count=counts from testtable where testid=1
update testtable set counts=@count+1
if (@@error >0) begin
rollback tran TEST
end else begin
commit tran TEST
end
GO