How to Detect and Fix SQL Server FILESTREAM Database Corruption?

The FILESTREAM feature in SQL Server allows you to store Binary Large Objects (BLOBs) with a varbinary (max) column externally onto the file system instead of storing them inside a database. However, this feature is not enabled by default. You need to enable it by using the SQL Server Management Studio (SSMS) and SQL Server Configuration Manager. When this feature is enabled, the database is stored in the FILESTREAM file groups. Once it is stored in a FILESTREAM column, you can access the files by using T-SQL transactions.

Sometimes, modifying or deleting the files in the FILESTREAM folder or saving a file to the SQL Server database can result in inconsistencies or corruption in the SQL Server database. If the FILESTREAM filegroup is corrupted, then your database will go into the ‘recovery pending’ state. In this blog, we’ll discuss the causes that can lead to corruption in SQL Server FILESTREAM database and see how to resolve corruption and recover the database.

Causes of Corruption in FILESTREAM-enabled Database in SQL Server

The FILESTREAM-enabled database can get corrupted due to various reasons. Some common ones are:

How to Detect Corruption in FILESTREAM-enabled Database in SQL Server?

You can use the DBCC CHECKDB command to detect corruption or physical and logical consistency issues in the FILESTREAM-enabled SQL database. It also validates link-level consistency between metadata of the table and the files and directories of the FILESTREAM file system. Here’s how to check the corruption inthefilestream-enabled database using the DBCC CHECKDB command:

DBCC CHECKDB ‘database_name’;

The DBCC CHECKDB command can report errors, like 7903, 7904, 7905, 7907, etc. Such errors indicate corruption, inconsistencies, or integrity issues that occur due to tampering with the SQL Server FILESTREAM folder.

Methods to Fix Corruption in SQL Server FILESTREAM-enabled Database

If the FILESTREAM-enabled SQL database is corrupted, damaged or inaccessible, you can follow the below methods.

Method 1: Restore from Filegroup Backup

Filestream data is stored in FILESTREAM filegroup. It contains the file system directories known as data containers. If you have files and filegroup backup in SQL Server, you can easily restore the database from the backup. Before that, ensure that the backup file contains the FILESTREAM data using the RESTORE FILELISTONLY command. This command displays all the files in the backup.

Here are the steps to restore database from the backup in the SQL Server:

Note: First, read the limitations and restrictions for restoring files and filegroups backup. Also, make sure you have RESTORE permissions.

This will restore the database. You can locate the physical folder of the FILESTREAM group using the below command:

SQL
SELECT name, physical_name, state_desc, type_desc 
FROM sys.database_files
WHERE type_desc = 'FILESTREAM'

Alternatively, you can use Transact-SQL commands to restore the files and filegroups backup.

Method 2: Use DBCC CHECKDB Command

If the backup is obsolete or not available, you can repair database using the DBCC CHECKDB command with REPAIR_ALLOW_DATA_LOSS repair option. Here are the steps:

ALTER DATABASE Dbtesting SET SINGLE_USER
DBCC CHECKDB (N ’Dbtesting’, REPAIR_ALLOW_DATA_LOSS) WITH ALL_ERRORMSGS, NO_INFOMSGS;
GO
ALTER DATABASE Dbtesting SET MULTI_USER

Although the above DBCC CHECKDB command can fix corruption-related issues in the FILESTREAM-enabled database, it has certain limitations. It may leave your database in a logically inconsistent state. While repairing complex errors, you need to use the command multiple times, which is a time-consuming process. Furthermore, it might also deallocate rows or pages while repairing the database, resulting in loss of data.

Method 3: Use a Professional SQL Repair Tool

To repair the database file with precision and integrity, you can use Stellar Repair for MS SQL – a specialized SQL repair tool that can quickly repair MDF files, even with higher levels of corruption. After repairing, the tool saves the recovered data in a new MS SQL database file.

The tool helps in resolving all corruption-related errors in the SQL database reported by the DBCC CHECKDB command. It has an intuitive user interface that allows users to perform the repair process with ease.

Key Features:

To Conclude

Corruption in FILESTREAM-enabled databases can occur due to many reasons. We have discussed above how to repair corrupted FILESTREAM-enabled database in SQL Server. You can easily restore database from the backup. But if your backup is not accessible, then you can use the DBCC CHECKDB command to repair the database. However, this method would require your time and effort and also doesn’t guarantee complete database recovery. So, for a seamless recovery process, use a professional SQL repair tool, like Stellar Repair for MS SQL.

Related Post

Exit mobile version