Monday, July 24, 2017

How to Avoid Accidentally Update of Record in Sql

I am writing this  post after i accidentally update all the records in a table. To avoid accidental  update just follow this simple method . Before executing the query just put a "Begin Transaction" above your query and "Rollback Transaction" after your Query.


I am using  Northwnd database and MS Sql Server for this example.

Example Query:

BEGIN TRANSACTION

UPDATE Categories SET CategoryName= 'A'
SELECT CategoryID,CategoryName FROM Categories

ROLLBACK TRANSACTION

If we want to change the categoryName of  a single item by missing where condition if we executed the update query means it will change all the CategoryName of the table. By using "Begin and Rollback Transaction" we see the result of update condition without actually affecting the Original database.







No comments:

Post a Comment