Undo Human Errors in SQL Server – SQL in Sixty Seconds #109 – Point in Time Restore

Undo Human Errors in SQL Server - SQL in Sixty Seconds #109 - Point in Time Restore 109-UndoHumanErrors-800x450

In this blog post, we are going to talk about how to undo human errors in SQL Server with the help of Point in Time Recovery. Let me start asking some questions –

  • Have you ever dropped your table by mistake?
  • Have you ever wrote an UPDATE statement without the WHERE condition?
  • Have you ever deleted the data which are critical to the business?

If the answer to this question is yes. I am sure you understand the importance of rolling back the transactions. In SQL Server you can always Undo Human Errors with the help of Point in Time Recovery. Let us learn how you can do that in this video.

Here are a few things to remember. You can rollback your transaction if your recovery model is Full Recovery Model and also if you have taken one full backup before you made the error. If either of the conditions is not satisfied you may not be able to recover your data again.

One of the most common questions I have received for this video is what happens if the database has many other important transactions going on other tables and you want to keep them rolling forward. If that is the case, you should restore your database on another system or with a different name on the same system and restore your database to the point right before you made the error.

Once you do that, you can retrieve your data from the table where you have accidentally faced the disaster and insert into your original live database.

In any case, it is always a good idea to perform all of your experiments on the development environment first. Once you are satisfied with the results and output, you should try them in the production environment. It is always a good idea to test your work before deploying it.

Here is the sample script which I have used in this blog post.

CREATE DATABASE SQLAuthority
GO
ALTER DATABASE SQLAuthority
SET RECOVERY FULL
GO
USE SQLAuthority
GO
CREATE TABLE TestTable (ID INT)
GO
BACKUP DATABASE [SQLAuthority] 
TO DISK = N'D:SQLAuthority.bak'
GO
INSERT INTO TestTable (ID)
VALUES (1), (2), (3)
GO
SELECT GETDATE() BeforeTruncateTime;
-- 2020-08-18 19:45:38.717
SELECT *
FROM TestTable
GO
-- Disaster
TRUNCATE TABLE TestTable
GO
SELECT *
FROM TestTable
GO
BACKUP LOG [SQLAuthority] TO
DISK = N'D:SQLAuthorityLog.trn'
GO
-- ------------------
-- Restore operation
USE master
GO
RESTORE DATABASE [SQLAuthority] 
FROM DISK = N'D:SQLAuthority.bak'
WITH NORECOVERY, REPLACE
GO
RESTORE LOG [SQLAuthority] 
FROM DISK = N'D:SQLAuthorityLog.trn'
WITH STOPAT = '2020-08-18 19:45:38.717', RECOVERY
GO
USE SQLAuthority
GO
SELECT *
FROM TestTable
GO
USE master
GO
DROP DATABASE SQLAuthority
GO

Please run the script above with the video which is included in this blog post. If you accidentally run the script without understanding what each command do, you may damage your system.

Remember final thing: When something goes wrong, do not worry or panic. Just note down the time when it happened. You can always rollback if you have a full back and full recovery model using the technique explained in this video.

If you still need help you can reach out to me here:  Comprehensive Database Performance Health Check.

Reference: Pinal Dave (https://blog.sqlauthority.com)

Author: admin

Leave a Reply

Your email address will not be published.