I blogged DBCC WritePage a year ago here http://www.sqlnotes.info/2011/11/23/dbcc-writepage/. It’s an extremely dangerous command especially with the last parameter “directORbufferpool” of this command turned on. I also showed you how to fix the page checksum here http://www.sqlnotes.info/2013/05/02/fix-page-checksum/. To protect yourself, please ensure you are operating on the database that you want, not system databases. Taking a full backup before playing this command is important. Never run it in your production. Beside that, there is a simple way to rollback changes made by DBCC WritePage…

When use DBCC Page to read the data. Data will come to the buffer pool first. When the page is not valid, wrong page checksum, conceptually, data will bypass the buffer pool and directly returned to the user.
When use DBCC WritePage to write the data. Data changes will be done in the buffer pool. Buffer pool will harden the changes to disk. What will happen if the file is read only logically? Will DBCC WritePage returns error? Let’s take a look

use master
if db_id('TestDB') is not null
	drop database TestDB
go
create database TestDB 
GO
create table TestDB.dbo.t1(Field1 char(10))
go
insert into TestDB.dbo.t1 values('AAAAAAAAAA')
go
select * from TestDB.dbo.t1
-- Field1
-- ----------
-- AAAAAAAAAA
go
---set the database to read only
alter database TestDB set read_only 
go
delete TestDB.dbo.t1
--Msg 3906, Level 16, State 1, Line 1
--Failed to update database "TestDB" because the database is read-only.
go
dbcc traceon(3604)
dbcc ind(TestDB, t1, 1)

--PageFID PagePID     PageType 
--------- ----------- -------- 
--1       150         10       
--1       147         1        
go
dbcc page(TestDB, 1, 147,3)
dbcc writepage(TestDB, 1, 147,105, 3, 0x424344 )
select * from TestDB.dbo.t1 --  data get changed without any error
--Field1
------------
--AAAAABCDAA
go 
-- this change is done in the memory. 
-- Now let's change the database state
alter database TestDB set read_write
go
-- Check it again. all changes done by DBCC WritePage are "rolled back"
select * from TestDB.dbo.t1 
--Field1
------------
--AAAAAAAAAA

Turn your database into read-only mode can protect you from being hurt by DBCC WritePage
John Huang – SQL MCM & MVP, http://www.sqlnotes.info

Protect Yourself from Using DBCC WritePage

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *

C# | HTML | Plain Text | SQL | XHTML | XML | XSLT |

This site uses Akismet to reduce spam. Learn how your comment data is processed.