Click here to Skip to main content
15,867,316 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
i know about through rollback we will recover delete record and if commit we cant get delete record but how its work how to implement it thing thing i want to clear i search for it many times but i am not get a proper and clear answer please help me on it..

suppose i have a Employee table and i delete a record like

delete from Employee where Name='Riya'

after delete i want to rollback it please suggest me on it

Thank You

What I have tried:

begin TRAN DELETE from Employee select * from Employee ROLLBACK select * from Employee
Posted
Updated 8-Jun-16 20:21pm
v2

You're close. You just needed to add the WHERE clause to the DELETE statement. If just run the begin tran and then the delete, you'll notice Riya is not there, roll it back and select the table again, Riya should appear.
BEGIN TRAN
DELETE from Employee WHERE [Name] = 'Riya'
ROLLBACK
select * from Employee 
 
Share this answer
 
If anything goes wrong with any of the grouped statements, all changes need to be aborted. The process of reversing changes is called rollback in SQL Server terminology. If everything is in order with all statements within a single transaction, all changes are recorded together in the database. In SQL Server terminology, we say that these changes are committed to the database.
see below simple query snippet
SQL
BEGIN TRANSACTION trans
BEGIN TRY
DELETE from Employee WHERE [Name] = 'Riya'
 BEGIN COMMIT TRANSACTION trans
 END
END TRY
BEGIN CATCH
 print 'Error Occured'
 BEGIN ROLLBACK TRANSACTION trans 
 END
END CATCH 

see above snippet in which we have put try..catch in sql, if any error occurred when executing DELETE statement then transaction get rollbacked
 
Share this answer
 
Hi,

Check this....


Commit and Rollback Commands in SQL Server[^]


Hope this will help you.

Cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900