This article was reproduced from: http://www.cnblogs.com/sky_Great/archive/2013/01/09/2852417.html
SQL Normal Transaction
begin TransactionTRDeclare @error int; Set @error=0; Select * fromCar_brandSet @error=@error+@ @ERROR Select 1/0 Set @error=@error+@ @ERROR Select * from AreaSet @error=@error+@ @ERROR Print 'I have executed!' Set @error=@error+@ @ERROR Print '@ @error:'+cast(@error as varchar( -)) if(@error<>0) begin Print 'executed failed'; rollback TransactionTREnd Else begin Print 'executed Success'; Commit TransactionTREnd
Transactions that combine try Catch
beginTrybegin TransactionTRSelect * fromCar_brandUpdateCar_logSet [reason]='Ceshiceshiceshi' where [ModelID]=557; Select 1/0 Print 'I have executed!' Select * from AreaPrint 'executed Success'; Commit TransactionTREndTrybeginCatchPrint 'executed failed'; rollback TransactionTREndCatch
(go) SQL Server transaction with try Catch