Monday, May 3, 2010

SQL | Detecting and Fixing Torn Pages

Detecting and Fixing Torn Pages

--Find the torn page. If no results returned then you are fine
SELECT db_name(database_id) DatabaseName, file_id, page_id, last_update_date
FROM msdb..suspect_pages
WHERE event_type = 3

--Backup your transaction log
USE master
BACKUP LOG DBName
TO DISK = 'C:\DBName.trn'
WITH NORECOVERY

--Restore Torn Page. 1 is the file_id, and 123 is the page_id from the first query
RESTORE DATABASE DBName PAGE='1:123'
FROM DISK='C:\DBName.bak'
WITH NORECOVERY


--Restore your log
RESTORE LOG DBName FROM
DISK='C:\DBName.trn'
WITH RECOVERY