Av rating:
Total votes: 62
Total comments: 14


Pop Rivett
Pop Rivett and the Suspect Database
08 January 2007

Blake Savage gazed in horror at the string of errors in the database activity log of his communications module. It was always at the worst possible moment that SQL Server grenaded itself and produced a corrupt database. Without the database data, all off-planet communications were impossible! How were they to get back to their ship?

The front of his state-of-the-art 'Goldfish' helmet began to steam up, as panic set in.

"Captain", shouted Blake over his futuristic intercom, "it's happened again, damn it."

What could have caused such a travesty in his wonderful chrome-plated rig, he mused, moodily. Maybe the strange acidic sands of Moon Lunix 245B had penetrated the disk of the machine.

"No worries," said Captain 'Pop' Rivett airily, as he swayed into view over the Moon's soft surface. "The database is using the full recovery model so just restore it from the last good backup, followed by the transaction logs, in sequence"

Blake swore under his breath. Somewhere far above them cruised their great intergalactic battleship, the SS Sicromoft. There, in the locker of his cabin, lay the last known good backup, safe within its Formica package. He froze in panic at the implications of this.

"You have got your last-known good backup haven't you?" asked Pop, instinctively flicking the dial on his stun gun to 'maximum pain'.

Observing the beads of sweat that had appeared on Blake's brow, Pop clenched his teeth and tightened his grip on stun gun, before sighing wearily and returning it to its holster.

"Right, so the database is coming up with a string of DBCC errors, showing all the symptoms of a torn page, and ... for whatever reason …" he said, glaring meaningfully at Blake, "there's no backup. The first thing to do is to stay calm. You may still be able to get the data off the database. Let's go through this step by step."

1. Get the database up and running, flagged as 'suspect'

First, you need to get to the stage where the database is at least running, but flagged as 'suspect'. Restart SQL Server. It will attempt to recover the corrupted database. The Recovery process makes the database consistent by redoing or undoing all the transactions that were either started after or uncommitted at the time of the last checkpoint. This involves reading each log record, comparing its timestamp to the timestamp of the corresponding database page, and either undoing the change (in the case of an uncommitted transaction) or redoing the change (in the case of a committed transaction). If SQL Server cannot complete the recovery procedure it sets one of the bits in the status field in the sysdatabases table so that the database is flagged as 'suspect'.

Blake Savage tapped nervously at the keyboard. "It looks like the file is too badly corrupted even to get it to that stage!" he cried forlornly.

"All may not be lost" said Pop "If the database is too damaged to allow this, there is still a chance of rescuing the situation. Try creating a database with an mdf database file of the same size as the old one, then stop the server, copy the corrupt mdf over the newly created one and finally restart the server."

"OK, that seems to have worked. The database is started up and flagged as being 'suspect'. But now what? What good is a 'suspect' database?" Blake remained uninfected by any of Pop Rivett's optimism.

2. Attempt to recover the suspect database

Firstly, note the errorlog message that is causing recovery to fail. You now need to get the server to retry recovery. First, however, you need to reset the 'suspect' flag. And to do that, you'll have to allow updates to system tables:

Sp_configure 'allow updates', 1
Reconfigure with override

Of course, you need to remember to set it back afterwards! Anyway, with the system table updates enabled you can attempt to reset the 'suspect' status by executing:

sp_resetstatus 'mydbname'

Or:

update master..sysdatabases SET status = status ^ 256
  where name = 'mydbname'

OK, now restart the server. If you are in luck, the database will recover the second time.

"No!" wailed Blake, "it's still set in 'suspect' mode! We're doomed!"

3. If recovery fails, set the database to Emergency mode

"Not quite" replied Pop sagely. To be honest I suspected this would be the case. If you really do have a 'torn page' error, then the database will definitely stay in 'suspect' mode. You simply need to move on to the next step, and that's to put the database into 'emergency' mode.

"Emergency mode?" Blake gulped.

Yes. If you can do this, then you will be able to get to as much of the data as possible. To set the database to 'emergency' mode you should use:

update master..sysdatabases set status = status |  32768
  where name = 'mydbname'

Incidentally, for v7 I believe the equivalent command was:

update master..sysdatabases set status = status |  -32768
where name = 'mydbname'

After issuing this command you must stop and restart the SQL Server service. While a given database is in Emergency Mode, SQL Server will not attempt to restore it when starting up. On start-up the database will still be in emergency mode, but it should allow you to access the data.

"It worked!" cried Blake, for the first time allowing a hint of optimism to creep into his voice.

4. Transfer all non-corrupt data to another database

"Excellent" said Pop, encouragingly. If the database is now accessible, you should be able to read data using standard techniques. The data can be transferred to another database via DTS, BCP or SELECT queries – I prefer BCP native format.

Of course, you will get an error when trying to access the corrupt data but once you know the ranges of the data where the errors will occur, you can use WHERE clauses and indexes to access data around the corrupt pages.

5. Get your sorry ass out of there

"So, we have a chance of getting off this godforsaken moon and back to the SS Sicromoft after all!!!"

"We?" said Pop, affecting bemusement, "I shall be going back to the ship right now, by means of my one-man, officer-class, personal escape capsule. As for you, it all depends on whether you can get the data restored to the point where you can radio in your position. Good luck old boy. Toodle Pip!"

"Smoke me a kipper, I'll be back for breakfast…!!!" shouted Blake defiantly, but somewhat pathetically, as he returned his attention to his wounded server.



This article has been viewed 15205 times.
Pop Rivett

Author profile: Pop Rivett

Pop spent his formative years working in assembler on IBM Series/1 but retrained in VB when that went out of fashion. He soon realised how little relational database expertise existed in most companies and so started to spend most of his time working on that. He now sticks to architecture, release control / IT processes, SQL Server, DTS, SSIS, and access methods in VB/ASP/.NET/Crystal Reports/reporting services. He has been involved with SQL Server since the old days of v4.2 to v2005. He tries to stay away from anything presentation oriented (see www.mindsdoor.net). Theoretically he is semi-retired but seems to keep being offered potentially interesting work.

Search for other articles by Pop Rivett

Rate this article:   Avg rating: from a total of 62 votes.


Poor

OK

Good

Great

Must read
 
Have Your Say
Do you have an opinion on this article? Then add your comment below:
You must be logged in to post to this forum

Click here to log in.


Subject: What about DBCC?
Posted by: Anonymous (not signed in)
Posted on: Wednesday, January 10, 2007 at 3:34 AM
Message: Why do you not mention using DBCC to attempt to repair the database, or at least identify the tables with errors?

Subject: SQL 2005
Posted by: Sergey (view profile)
Posted on: Wednesday, January 10, 2007 at 9:54 AM
Message: Do you have an updated articel to SQL 2005?

Subject: re: SQL 2005
Posted by: Tony Davis (view profile)
Posted on: Wednesday, January 10, 2007 at 12:28 PM
Message:

The code in this article was tested on SQL 2000. I believe the equivalent syntax for SQL 2005 can be found here:

http://msdn2.microsoft.com/en-us/library/ms174269.aspx

HTH

Tony (Simple-Talk ed.)


Subject: SQL 2005 Corrupt DB recovery
Posted by: Anonymous (not signed in)
Posted on: Monday, February 12, 2007 at 11:56 PM
Message: Great article! though would have helped had i read the article before my DB was corrupted. non the less a good article.

Subject: Thanks for the fun
Posted by: Anonymous (not signed in)
Posted on: Tuesday, February 13, 2007 at 12:51 PM
Message: Reading a technical article with humor in it makes the experience so much more fun--and the content much more digestible. Thanks!

Subject: DB Recovery
Posted by: Anonymous (not signed in)
Posted on: Wednesday, February 14, 2007 at 5:23 AM
Message: The last thing you'd want to do with a suspect database is restart the server, you'd normally take other steps to copy/secure the data first.

Subject: Suspect Database
Posted by: Anonymous (not signed in)
Posted on: Thursday, March 01, 2007 at 8:01 PM
Message: Great work. can this one work for SQL 2005?

Subject: Suspect DB Recovery
Posted by: Anonymous (not signed in)
Posted on: Friday, April 20, 2007 at 11:53 AM
Message: Can't argue with success. I followed the steps, recoverd the database, and was carried about the office on the shoulders of adoring developers, who thought that their hard work was lost.
A good article, easy to read and understand.

Subject: the same issue
Posted by: Shukria (not signed in)
Posted on: Tuesday, July 31, 2007 at 12:51 AM
Message: Hi,
thanks for ur help

Subject: Shukria
Posted by: Anonymous (not signed in)
Posted on: Tuesday, July 31, 2007 at 1:35 AM
Message: Hi,
your article is useful. but i am useing SQL as CRM DATABASE So?

Subject: suspect database
Posted by: Anonymous (not signed in)
Posted on: Tuesday, June 24, 2008 at 9:04 AM
Message: plaease give me solution

i have a suspect database.bt iam not able to
recover it.please help me..


my E-MAIL-sn.rana87@gmail.com

Subject: Article
Posted by: Alexander (not signed in)
Posted on: Thursday, July 03, 2008 at 11:13 AM
Message: Thank you for the article, written very nicely and helpful.

Best regards.

Subject: Thanks
Posted by: Richard Gadsden (not signed in)
Posted on: Monday, July 07, 2008 at 9:11 AM
Message: Thank you, you were a lifesaver.

Subject: re:Corrupte SQL database
Posted by: martin (view profile)
Posted on: Friday, August 01, 2008 at 2:10 AM
Message: Hi,
very nice posting. Sometime few instances or poor programming causes database in the suspect state and the transaction log file becomes corrupted. The conditions of the database after this command suggest that the database has got corrupted and create a panic situation. In this situation you need Stellar Phoenix MS SQL Repair software which is a good repairing tool for the MS SQL server databases after any instance of database corruption and with any file version of Windows and thus acting as a complete solution for your corrupted SQL databases.

 









Phil Factor
Bunnikins!
 When an IT manager is selected as a victim of office politics of a large corporate, it is time for him to engage in... Read more...



 View the blog
SQL Toolbelt 2008: Predominantly an Engineering Task
 The conversion of the Red-Gate tools to be compatible with SQL Server 2008 might not seem, on first... Read more...

Audit Crosschecks
 In this short article, the second of a 2-part series, William suggests a solution, using SQL Data... Read more...

Discovering Security Uses for SQL Compare
 Much of the security of SQL Server is implemented as part of the database schema. This provides some... Read more...

XML Jumpstart Workbench
 In which Robyn and Phil decide that the best way of starting to learn XML is to jump in and take a ride... Read more...

RSS Newsfeed Workbench
 Robyn and Phil decide to build an RSS newsfeed in TSQL, using the power of SQL Server's XML.  Read more...

Beginning SQL Server 2005 Reporting Services Part 1
 Steve Joubert begins an in-depth tour of SQL Server 2005 Reporting Services with a step-by-step guide... Read more...

Ten Common Database Design Mistakes
 Database design and implementation is the cornerstone of any data centric project (read 99.9% of... Read more...

SQL Server Full Text Search Language Features
 SQL Full-text Search (SQL FTS) is an optional component of SQL Server 7 and later, which allows fast... Read more...

Beginning SQL Server 2005 Reporting Services Part 2
 Continuing his in-depth tour of SQL Server 2005 Reporting Services, Steve Joubert demonstrates the most... Read more...

Executing SSIS Packages
 Nigel Rivett demonstrates how to execute all SSIS packages in a given folder using either an SSIS... Read more...

Over 150,000 Microsoft professionals subscribe to the Simple-Talk technical journal. Join today, it's fast, simple, free and secure.

Join Simple Talk