Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to Roll Back Insert,Update and Delete Query in Sql Server 2005?
Posted

You need to rollback the query whenever there is any issue occurs while performing these operations, otherwise you will commit the transaction.

So, go through the articles below to know more.
1. Maintaining Database Consistency with Transactions[^].
2. Managing Transactions in SQL Server Stored Procedures[^].

Basically, you need to handle Errors, if any, like below, for instance.
SQL
-- Rollback the transaction if there were any errors
IF @@ERROR <> 0
 BEGIN
    -- Rollback the transaction
    ROLLBACK

    -- Raise an error and return
    RAISERROR ('Error in deleting employees in DeleteDepartment.', 16, 1)
    RETURN
 END


Thanks...
 
Share this answer
 
v2
Comments
Priyanka Jain 18-Jan-13 6:20am    
Thanx...
Most welcome. Anytime. My pleasure.
Thanks for Accepting the answer @Priyanka.
SQL
begin transaction

// execute SQL code here

rollback transaction



Below mentioned articles will help you to get better idea on ur problem.

http://www.sqlusa.com/bestpractices2005/rollback/[^]

http://blog.sqlauthority.com/2010/03/04/sql-server-rollback-truncate-command-in-transaction/[^]

Hope these will help you..
 
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