SQL Server Recovery

Facing Microsoft SQL Error 5173? Know How to Solve This Server Error

Jason Clark ~ Modified: March 3rd, 2023 ~ SQL ~ 6 Minutes Reading

SQL Error 5173

If you are an SQL Server user, you may come across any error while performing your everyday task. Errors can occur due to different reasons and separate solutions are recommended for each of the errors. The error in discussion today is SQL error 5173. In this post, we will learn the reasons and solutions to SQL Server error 5173 attach database. But first, check out the user queries regarding this error found on tech forums.

Table of Contents

User Queries

User Query 1: A few days ago, It attempted to transfer my data to another SQL Server. So, I have performed these steps: first I detached the database, then I copied my MDF file (not LDF) to the new Server. At this point, I created a new database with the original DB name in the new Server.

Then I detached the new database and replaced the new MDF file the old (original) MDF file. During the process, I got SQL error number 5173 that says: “cannot associate files with different databases.” please tell me why I am getting this message and what is the solution.

User Query 2: A hard disk containing a large sized database had some corruption issues. As there was no backup available, we had to take the help of a freelance technician to recover the database.

After the MDF and LDF files have been recovered, we tried to attach those and failed. Instead, MS SQL error 5173 is what we received. Why are we getting this error message? Can anyone explain its reasons and solutions?

What Is MS SQL Error 5173?

The Microsoft SQL Server error 5173 occurs due to a problem with both the Primary and Transaction log file. Due to some sort of error, the files don’t match at all. Therefore, the SQL Database thinks that data files belong to different databases & different servers. In case of primary data file corruption, you can use the automated MS SQL Repair Tool.

MS SQL Server Error 5173 – Reasons

If we check carefully, we will find that this error appears because the files being used by the database belong to different databases. This is basically a safety mechanism that prevents accidental overwriting of existing files. That is why during the startup, database checks if all files are from same database and if not, it shows this error message. If the database is corrupt or it is in Recovery Pending state, this error message will get displayed.

This error message appears due to various reasons. The major causes are corrupt MDF file and files used in database are not from the same database. Besides, power failure, insufficient storage space, and defective hardware can be responsible for this error message.

How to Fix MS SQL Error 5173

Depending on the cause, users need to adopt remedial methods to fix this error.

  • Method 1: Use DBCC CHECKPRIMARYFILE Command

To get rid of this problem, users need to apply DBCC CHECKPRIMARYFILE command to know what is in the primary file header. It is because when a database is attached, reading MDF file information is not feasible. Hence, comes the requirement of DBCC command to open and read the primary file header. To apply the command, use these steps & solve Microsoft SQL server error 5173 attaching database issue:

a. To begin with, users have to create a database that contains two data files and a log file.

b. Then detach the database. Use this command for DB detachment.

c. Run DBCC CHECKPRIMARYFILE command to open file header and read the detailed information.

d. Since the first file is the primary database file, we will get output for that file only (the output will be 1).

e. This command will let users read the properties information from the file header.

Option 1. This displays the file and filegroup ID, physical name, logical name of the files that belong to the same database.
Option 2. It shows the original database where the file belongs, along with database version and collation.
Option 3. This is actually the subset of option no. 1.

  • Method 2: Change Recovery Pending State of Database

a. Run this script to set the DB in emergency mode:


b. Make the database available for multiple users:


c. Detach the database and then re-attach:

 EXEC sp_detach_db '[dbname]' EXEC sp_attach_single_file_db @dbname = '[dbname]', @physname = N'[mdf path]'
  • Method 3: Make Corrupt Database Healthy

If the database has gone corrupt, run this script to repair it:

The Best Solution to Solve Microsoft SQL Error 5173

As this command will result in data loss, experts do not recommend this method. Therefore, it is better to use SQL MDF File Repair Tool. This utility can fix all types of corrupt SQL database without any data loss. Using this application, which is compatible with all latest versions of SQL Server, users can also retrieve deleted database files.

Download Now Purchase Now

Step-1. Launch the Tool to solve Microsoft SQL server error 5173 first.

launch tool

Step-2. Select the Quick or Advanced Scan for database migration.

select quick or advanced scan

Step-3. Preview the Database Files & then Go to Export options.

preview files

Step-4. Select the Destination Path & Set Export Settings here.

set destination path

Step-5. Finally, Click on the Export button to get the desired results.

click export to solve sql error 5173

Concluding Words

Here, we discussed all the aspects of SQL error 5173 including its causes and cures. We have provided multiple solutions that can be implemented to get rid of this error for good. We expect that users can follow the guideline described here and get the desired result.

In a nutshell, we can say that it’s quite easy for users to solve this Microsoft SQL server error 5173 with the automated solution. Apart from that, the manual solution works but it is hectic. In order to stay away form hassles, the modern method is the right choice for users. It can be the best choice to solve this critical SQL server error.