Introduction

Transaction is most important role play in our Development line. In Transaction have two basic rules.

Process Result

Everything is OK= Success
Some error = Role back Everything.

Transaction process system

Real Time Example

Every buddy like a travlining. suppose i want go HP(Himachal Pardesh) for vinnter vocation. I Need a Train seat for comfertable travel. I Book the Train seat ON ONLINE(IRTC WEBSITE). For seat confimation some procedure follow.

Transaction is both side working Front END(c#) and Back End (SQL Server).

How to declare SQL Transaction

  1. Begin tran
  2. --do stuff here
  3. commit tran
With Example:
  1. SET XACT_ABORT ON --this one
  2. set nocount on
  3. BEGIN tran -- * Transaction Start here * -- -- * Product * --
  4. -- do stuff here
  5. if(@@ERROR<>0)
  6. begin
  7. rollback tran
  8. set @succ=0
  9. end
  10. else
  11. begin
  12. set @succ=-1
  13. commit tran
  14. end
Nesting Transaction

SQL Server allows you to nest transactions. Basically, this feature means that a new transaction can start even though the previous one is not complete. Transact-SQL allows you to nest transaction operations by issuing nested BEGIN TRAN commands. The @@TRANCOUNT automatic variable can be queried to determine the level of nesting - 0 indicates no nesting , 1 indicates nesting one level deep, and so fourth.

Some error occur

You can put set xact_abort on before your transaction to make sure sql rolls back automatically in case of error.