Preventive measures with Delete or Update Command in SQL Server 2005

Though it’s not wise trying to run Delete or Update command in any live database, sometimes situations compel to do so. Truly speaking, this is the most dangerous task. You may end up deleting or updating all of your data. For example


DELETE FROM t_BillMaster
WHERE BillMasterID =1;



You execute this command in your Query Window and delete one row form t_BillMaster Table. It is OK up to this. The real problem starts when you execute this command without where clause and lost all data. Sorry to say that you will find yourself in hale directly.

However, you can minimize the risk of losing data using Begin Transaction command. Have a look at this code snippet.


BEGIN TRANSACTION
DELETE FROM t_BillMaster
WHERE BillMasterID =1;


After executing this snippet, you can retrieve your data if you wish executing ROLLBACK command.


ROLLBACK;


Or if everything is ok, just execute COMMIT command.


COMMIT;


Last but not the least; you can’t get your data in previous state after committing the transaction.

Thanks
A Rahim Khan

Advertisements
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: