SQL Server 事务处理 回滚事务

创建表:

复制代码
SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATETABLE[dbo].[t1](     [Id][int]NOTNULL,     [c1][nvarchar](50) NULL,     [c2][datetime]NULL, CONSTRAINT[PK_Table1]PRIMARYKEYCLUSTERED (     [Id]ASC )WITH (PAD_INDEX  =OFF, STATISTICS_NORECOMPUTE  =OFF, IGNORE_DUP_KEY =OFF, ALLOW_ROW_LOCKS  =ON, ALLOW_PAGE_LOCKS  =ON) ON[PRIMARY] ) ON[PRIMARY]
复制代码

 

 

 

解决方案(一)

 

复制代码
declare   @iErrorCount   intset@iErrorCount=0begintran Tran_2008_10_07
insertinto t1(Id, c1) values(1,'1') set@iErrorCount=@iErrorCount+@@error
insertinto t1(Id, c1) values(2,'2') set@iErrorCount=@iErrorCount+@@error
insertinto t1(Id, c1) values('xxxx3','3') set@iErrorCount=@iErrorCount+@@error
insertinto t1(Id, c1) values(4,'4') set@iErrorCount=@iErrorCount+@@error
insertinto t1(Id, c1) values(5,'5') set@iErrorCount=@iErrorCount+@@error
if@iErrorCount=0   begin       COMMITTRAN Tran_2008_10_07   endelse     begin       ROLLBACKTRAN Tran_2008_10_07   end
复制代码

 

 

 

解决方案(二)

 

复制代码
begin try     begintran Tran_2008_10_07
       
insertinto t1(Id, c1) values(1,'1')
       
insertinto t1(Id, c1) values(2,'2')
       
insertinto t1(Id, c1) values('xxxx3','3')
       
insertinto t1(Id, c1) values(4,'4')
       
insertinto t1(Id, c1) values(5,'5')
   
COMMITTRAN Tran_2008_10_07 end try begin catch     raiserror 50005N'出错了'     ROLLBACKTRAN Tran_2008_10_07 end catch
复制代码

http://www.cnblogs.com/emanlee/archive/2008/10/07/1305424.html

posted on 2013-11-08 16:00  %幻#影%  阅读(198)  评论(0编辑  收藏  举报

导航