Database reckless modifications. ROLLBACK and COMMIT cure.

One day you might be forced to do modifications on live database and accidentially delete/update all records in table instead of specified in where statement. It happened to me in a past it might happen to you too. Usually you think that world is over. Then you remember about backup, but still there is a possibility that some data would be lost forever. Always make sure to email your records to yourself with a trusted email service provider for further back up. The best solution is as usual prevention of disaster.

There is a reason why SSMS Tools pack by default creates for new queries:

BEGIN TRAN

...

ROLLBACK

Use transaction for all your modifications. Inside transaction you can test if your update/delete does exactly what you want it to do.
Here is a little illustration:

CREATE TABLE TestTable
(
    Id int identity primary key ,
    [Description] varchar(max)
)
GO
 
BEGIN TRAN
 
insert into TestTable
values
('Test1'),
('Test2'),
('Test3')
 
select * from TestTable -- First query, returns all records
 
delete TestTable
where Id=2
 
select * from TestTable -- Second query, returns two records
 
ROLLBACK -- COMMIT
 
select * from TestTable -- Third query, no records because of ROLLBACK

Results:

Query 1:

1 Test1
2 Test2
3 Test3

 

Query 2:

1 Test1
3 Test3

 

Query 3:

After you are done with testing, just change your ROLLBACK with COMMIT statement.

 


Posted on Wednesday, February 17, 2010 by | Add Comment

New Comment

Your Name:
Email (for internal use only):
Comment:
 
Code above:

Categories

Recent Tweets

Valid HTML5