SQL provides many transaction management features. SQL commands COMMIT, ROLLBACK and SAVEPOINT helps in managing the transaction.
The COMMIT command is the transactional command used to save changes made by a transaction to the database.The COMMIT command will save all changes to the database since the last COMMIT or ROLLBACK command. Frequent commits in the case of transaction involving large amount of data is recommended. But too many commits can affect performance. In many implementations, an implicit commit of all the changes is done, if user logs off from the database.
The ROLLBACK command is the transactional control command to undo the transactions that have not already been committed to the database. The ROLLBACK command can be issued to undo the changes since the last COMMIT or ROLLBACK.
Illustration with an Example
There are two relational tables one for user registration information (tbl_userinfo) and another for user login information(tbl_login). Both tables have primary key foreign key relationship.
(A).User
registration Information(tbl_userinfo)
|
Column Name |
Data Type |
Constraint/Description |
|
user_id |
Int |
Identity(1,1) Primary Key |
|
name |
Varchar(50) |
Name of User |
|
email |
Varchar(50) |
Email of User |
(B).user login
information(tbl_login)
|
Column Name |
Data Type |
Constraint/Description |
|
login_id |
Int |
Identity(1,1) Primary Key |
|
user_id |
Int |
Foreign Key with tbl_userinfo |
|
user_name |
Varchar(50) |
User Name for login |
|
password |
Varchar(50) |
User Password for login |
(A)General Transaction: In this transaction userreg , one table tbl_userinfo is affected and one row is entered and another table tbl_login is not affected. Here first insert query is successful executed but second insert query is not successful executed due to table tbl_login where column login_id is primary key and auto increment so we can't pass value and CATCH block will be executed.
begin try
begin tran userreg
declare @userid int
insert into
tbl_userinfo(name, email)

surender bhyanPosted Jun 3, 2011, 1:34 PM
Thanks alot sir..keep it up :)