Transaction

When we required running many statements as a single unit means either all statements run successfully or if single statement failed then all statement should be discarded in this situation, for this we use transaction.

Suppose we have two tables, first is personal_info and second is password_info. When a user insert the record in both tables, in case record inserted in personal_info and while inserting in second table there is some error then first table insertion should be cancelled. Because record will be entered in both table otherwise not.

COMMIT and Rollback Statement

COMMIT: - commit command is used to save changes made by the transaction. This command execute when all statement runs successfully without error.

ROLLBACK:-rollback command is used to discard the transaction. This command runs when there is an error in the transaction.

Example of Transaction

begin transaction t1
begin try
insert into EmpSalryDetail values(9,'priya',15000,'Developer')
insert into EmpSalryDetail values(10,'priya','sneha','Developer')
commit transaction t1
select 'Transaction Executed'
end try
begin catch
rollback transaction t1
select 'Transaction Rollbacked'
end catch
		

In this transaction while inserting in second command we did mistake. Instead of inserting int data, we are inserting varchar value which causes an error in 2nd statement so both transactions will be rollbacked. But if both statements will be error free then all statement will be committed and saved in the database.

Email Address

For any query you can send mail at info@techaltum.com
Thanks