If you have .MDF file and the file is shutdown properly, but you do NOT have the .LDF file, then you can attach the .MDF files to SQL Server easily, as follows:
- Start SQL Server Management Studio and connect to the server.
- Click New Query.
- Input the following command:
sp_attach_single_file_db @dbname='MyDB', @physname=N'C:MyDB_Data.mdf' or CREATE DATABASE MyDB ON (FILENAME = N'C:MyDB_Data.mdf') FOR ATTACH_REBUILD_LOG or CREATE DATABASE MyDB ON (FILENAME = 'C:MyDB_Data.mdf') FOR ATTACH;
Here, MyDB is the database name, C:MyDB_Data.mdf is the mdf file path.
Microsoft recommends to use the second and third methods as the first method may retire in the new SQL Server versions.
- Click Execute to execute the query.
You can also get more detailed info at :
https://docs.microsoft.com/en-us/sql/relational-databases/databases/attach-a-database?view=sql-server-ver15
However, if your .MDF file and .LDF file is not shutdown properly. Or in other word, they are corrupt, then you need to use another method to attach them.