Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 August SQL Server: Corrupt a table using DBCC WRITEPAGE

SQL Server: Corrupt a table using DBCC WRITEPAGE

data-corrupt

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am sharing a demonstration on how to damage a table of SQL Server.
Why I am sharing this because practice on data corruption is required for SQL DBA before actual data corruption happens.

You can find N number of different solutions for restoring corrupted database or table, but what about like if you want to test any of this solution.
In most of the cases, the table gets corrupt because of invalid data or junk data of data page. Using DBCC WRITEPAGE, you can modify the table page.

Please check the below demonstration, and share your commands/options via comments to restore this table.

Create a sample database:

1
2
3
4
CREATE DATABASE KillMe
GO
USE KillMe
GO

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CREATE TABLE tbl_bad
(
ID INT IDENTITY(1,1)
,Name VARCHAR(10) DEFAULT 'dbrnd'
)
GO
 
CREATE CLUSTERED INDEX ix_ID ON tbl_bad(ID)
GO
INSERT INTO tbl_bad DEFAULT VALUES
INSERT INTO tbl_bad DEFAULT VALUES
INSERT INTO tbl_bad DEFAULT VALUES
INSERT INTO tbl_bad DEFAULT VALUES
INSERT INTO tbl_bad DEFAULT VALUES
GO

Find the page information of table:

1
2
DBCC IND (KillMe, 'tbl_bad', 1)
GO

Result:

1
2
3
4
PageFID PagePID IAMFID IAMPID ObjectID PageType
------- ----------- ------ ----------- ----------- --------
1 320 NULL NULL 565577053 10
1 312 1 320 565577053 1

Change database to single user mode:

1
ALTER DATABASE KillMe SET SINGLE_USER WITH ROLLBACK IMMEDIATE

Modify page number which page type is 1:

1
DBCC WRITEPAGE('KillMe', 1, 312, 60, 1, 0x00, 1)

Now try to select your table data:

1
SELECT *FROM tbl_bad

Error in the result:

1
2
3
4
5
Msg 824, Level 24, State 2, Line 1
SQL Server detected a logical consistency-based I/O error: incorrect checksum (expected: 0xe777b300; actual: 0xe777b303). It occurred during a read of page (1:312) in database ID 10 at offset 0x00000000270000
in file 'C:\Program Files\Microsoft SQL Server\MSSQL13.SQLSERVER\MSSQL\DATA\KillMe.mdf'. Additional messages in the SQL Server error log or system event log may provide more detail.
This is a severe error condition that threatens database integrity and must be corrected immediately. Complete a full database consistency check (DBCC CHECKDB).
This error can be caused by many factors; for more information, see SQL Server Books Online.

Check the suspect_pages:

1
SELECT * FROM [msdb].[dbo].[suspect_pages]

Result:

1
2
3
database_id file_id page_id
----------- ----------- --------------------
10 1 312

Aug 9, 2017Anvesh Patel
SQL Server: Script to find SOS_SCHEDULER_YIELD wait type usageSQL Server: How to find Table name from Page ID?

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

ImageAugust 9, 2017 SQL ServerAnvesh Patel, database, database research and development, DBCC IND, DBCC WRITEPAGE, dbrnd, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, suspect_pages, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....