Wednesday, March 27, 2013

Need to repair corrupt SQL server database


If you are working as a SQL server DBA from a long time then you will have encountered the SQL database corruption once in a while of your DBA life. Often corruption occurs due to power failure, sudden shutdown to the SQL server, hardware malfunction, virus attack, and many more. You can't access the database until repaired the database files.

Additionally, there are several methods available to protect your database file from being corrupt. And it is equally related to your job. In the event of database corruption, the first and foremost question, have you maintained the backup of database? If you do not have backup of database then it is like a soldier had gone to the country's border to protect the country without arms. Soldier may die at the border but your database can't die if you have not the backup of your database. You have chance to recover your database by repairing it.

How to repair?
You can repair corrupt SQL server database by following methods:

1. Repair the database using DBCC CHECKDB command
DBCC CHECKDB is the best command to test and fix the consistency errors in the SQL server database. DBCC CHECKDB is a combination of three commands: DBCC CHECKCATALOG, DBCC CHECKTABLE, and DBCC CHECKALLOC. If you run DBCC CHECKDB against the database then no need to run all other three commands separately. This command has three options to repair a corrupt SQL server database and the commands are repair_fast, repair_rebuild, and repair_allow_database. There is no any data loss, when you run DBCC CHECKDB with repair_fast & repair_rebuild but you may loss some amount of data in repair_allow_data_loss option.

Here is the syntax of DBCC CHECKDB
DBCC CHECKDB
[
[ ( your_database_name | database_id | 0
[ , NOINDEX
| , { REPAIR_ALLOW_DATA_LOSS | REPAIR_FAST | REPAIR_REBUILD } ]
) ]
[ WITH
{
[ ALL_ERRORMSGS ]
[ , EXTENDED_LOGICAL_CHECKS ]
[ , NO_INFOMSGS ]
[ , TABLOCK ]
[ , ESTIMATEONLY ]
[ , { PHYSICAL_ONLY | DATA_PURITY } ]
}
]
]

Note: Don't run DBCC CHECKDB with repair_allow_data_loss against the database without knowing the effect of it.

2. Repair the database using 3rd party SQL repair utility
There are so many data recovery companies available out there that offer SQL recovery utility to repair corrupt SQL server database. Let’s try any of the utility and see what they can do for you.

Note: First download the demo version of any software to measure the functionalities of the software.

Wednesday, February 13, 2013

FAQ: SQL Server Disaster Recovery


Q: Can I restore a SQL server database if I have data & log files?
A: Yes, you can restore a SQL server database if your data file & log file is not corrupt. You can do this by attaching data & log files to the SQL server.

Q: Can I restore a SQL server database if the data file is corrupt?
A: No, you cannot restore a SQL server database with a corrupt data file. You need to restore your database from a good known backup. If you don't have a good backup of your database, there is less change that you can do. There are some data recovery companies that claim they can recover data from the corrupt file by repairing the file. So you can go to the Internet and search the best SQL server mdf file recovery software and see what they can do for you.

Q: Can I attach a SQL server database without transaction log file if database was cleanly shutdown?
A: Yes, you can attach a SQL server database without transaction log file (.ldf) of the database. You can attach your database with the help of SQL Server Management Studio (SSMS). MS SQL server will create a transaction log file while attaching the database.

Q: Can I attach a SQL server database without transaction log file if database was not cleanly shutdown?
A: Yes, you can but method will be different. To do this, you have to perform below steps:
  • Create a same size database as your previous database size
  • Shutdown SQL server
  • Change the old mdf file with new created mdf file
  • Start SQL server, database may go in suspect mode
  • Change the database mode from suspect to emergency by ALTER command
  • Run DBCC CHECKDB with REPAIR_ALLOW_DATA_LOSS.
Q: Is there any In-built utility in SQL server to repair corrupt file like Compact & Repair in MS Access?
A: Yes, MS SQL server has DBCC CHECKDB that performs almost similar activities as Compact & Repair does. DBCC CHECKDB has three options; repair_fast, repair_rebuild & repair_allow_data_loss.

Q: Can I loss data after running DBCC CHECKDB with repair_allow_data_loss?
A: Yes, you may. As the name of command suggests you may loss the data from your database. In some cases it has been observed that database administrator has not lost the data after running DBCC CHECKDN with repair_allow_data_loss command against the database.

Q: Can I backup transaction log if my database is using Simple recovery model?
A: No, you can't backup transaction log file when you database is in simple recovery model.

Q: What can I do to avoid data loss from corruption?
A: Backup, Backup & Backup. It is the best solution to avoid any data loss from corruption. Additionally, running DBCC CHECKDB command against the database on regular interval is a good habit. You may know the corruption as soon as it occurs.

Q: What is the best time interval to run DBCC CHECKDB?
A: It depends upon your business need. If you can afford 2-3 days database downtime then run it on monthly basis but I don't think so a company can afford 2-3 days database downtime.

Sunday, January 27, 2013

Methods to Resolve SQL server Backup Error 3007


After upgrading SQL server database from SQL server 2000 to SQL server 2005. When you perform a full database backup of a SQL server 2005 database, you may got following error message on your system screen.

Executing the query "Backup Database [ZZZ] TO [ZZZ]
WITH NOFORMAT, NOINIT, NAME = N'ZZZ_backup_20060819040020', SKIP, REWIND, NOUNLOAD, STATS = 10
"failed with the following error:
"The backup of the file or filegroup "sysft_ZZZ_FT" is not permitted because it is not online.
BACKUP can be performed by using the FILEGROUP or FILE clauses to restrict the selection to include only online data. BACKUP DATABASE is terminating abnormally.".

Cause: SQL server error 3007 may occur when fulltext catalog is offline for the database ZZZ.

Resolution: You can resolve this error message with the help of two methods: SQL server management studio and drop & rebuild catalog .

With SQL server management studio: It is the simplest action to resolve error 3007. Follow below steps to resolve it.
  • Open SQL server management studio (SSMS).
  • Now go-to the storage container.
  • Check the state of fulltext catalog.
  • If there is a problem then resolve it.
  • Now you will able to backup your database.
With Drop & Rebuild Catalog: If you are still unable to backup your database then drop & rebuild the catalog if catalog is needed otherwise drop it.

Note: You can take backup of your database when rebuild start. There is no need to wait for rebuild catalog.