创建表:
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
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
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