Problem The development team approached me with an MDF file of a SQL Server database which they wanted to restore on their server. The developer had some very critical and complex stored procedures which were stored in the ProductsDB.MDF file. However, since this database was kept offline; during the disk cleanup unfortunately one of the developers deleted the transaction log file for this database. As this was a development server there were no database backup jobs scheduled. Since, this database was very critical for the development team they wanted to know if there was a way to recover and use this database. In this tip, we will cover how to attach a database that does not have its transaction log files.
Solution In this tip, we will go through the steps which need to be performed to recover a database when you only have the MDF file of a SQL Server database. The below steps work in both SQL Server 2005 and SQL Server 2008.
Using SQL Server Management Studio - Attach a SQL Server Database without a Transactional Log File
1. Connect to SQL Server 2008 Instance using SQL Server Management Studio
2. In Object Explorer, right click Databases node and select "Attach..." option from the drop down list as shown in the snippet below.
3. This will open up an Attach Databases window as shown in the below snippet.
4. In the Attach Databases window click on "Add..." button as highlighted in the above snippet to open the Locate Databases Files window as shown below.
5. In Locate Databases Files window you need to browse and locate the MDF file which is relevant to the database you want to attach and then click OK. In our example I am using the "ProductsDB.mdf" file.
6. In the Attach Databases window; you will see that SQL Server informs us that the log file is not found, in our case the missing file is "ProductsDB_Log.LDF". In order to attach the database without the ProductsDB_Log.LDF transaction log file you need to select the file as highlighted in the below snippet and click the "Remove" button and finally click OK to attach ProductsDB database without the transaction log file.
7. That's it you have successfully attached a database without using the transaction log file. SQL Server will create a new transaction log file for you and will place the file in the same folder as the data file.
Using T-SQL - Attach a SQL Server Database without a Transaction Log File
DBAs can also attach a SQL Server database without a transaction log file using the below T-SQL code.
In this first script we are using the CREATE DATABASE with ATTACH option and specifying the name and location of the mdf file that we have for this database.
CREATE DATABASE [ProductsDB] ON
( FILENAME = N'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\ProductsDB.mdf' )
Once the above T-SQL code has executed successfully you will get the below message which informs you that SQL Server has created a new transaction log file for the database.
File activation failure. The physical file name "C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\ProductsDB_log.ldf" may be incorrect.
New log file 'C:\Program Files\Microsoft SQL Server\MSSQL10.SQL2008\MSSQL\DATA\ProductsDB_log.LDF' was created.
Verify Logical and Physical Integrity of Database
DBAs can check the logical and physical integrity of all the objects within the database by executing a DBCC CHECKDB. In our case we are using the "ProductsDB" database.
DBCC CHECKDB ('ProductsDB')
If your database has multiple data files you can use this approach as well to create a new transaction log file.
If your database had multiple transaction log files look at using the ATTACH_REBUILD_LOG option with the CREATE DATABASE command
If you loose one of your data files for a database you can not use this approach. This will only rebuild a new transaction log file for you it does not recreate data files.
Above solution will work only in case if DB is cleanly shutdowned else DB server will throw error with message "The log cannot be rebuilt because the database was not cleanly shut down". Please let me know if you have any solution for this problem.
File activation failure. The physical file name "E:\ASWEBTECH\SaradaModelDB-2011-2012_log.ldf" may be incorrect.
New log file 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\SaradaModelDB_log.LDF' was created.
Msg 1813, Level 16, State 2, Line 1
Could not open new database 'SaradaModelDB'. CREATE DATABASE is aborted.
Msg 824, Level 24, State 2, Line 1
SQL Server detected a logical consistency-based I/O error: incorrect checksum (expected: 0x66666666; actual: 0x33333333). It occurred during a read of page (1:1202) in database ID 6 at offset 0x00000000964000 in file 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\SaradaModelDB.mdf'. Additional messages in the SQL Server error log or system event log may provide more detail. This is a severe error condition that threatens database integrity and must be corrected immediately. Complete a full database consistency check (DBCC CHECKDB). This error can be caused by many factors; for more information, see SQL Server Books Online.
Thanks for the article; but what about SQL 2000? I have this exact problem and was helping a buddy migrate his small business. They have SQL 2000, and deleted transaction log. I only have the MDF. What can I do in sql 2000 SP3?
While attaching a database which contains 16 mdf and 4ldf files, iam facing the below errors. as per the below error i have tried lot some methods including your article but there is no use. Kindly suggest me to bring back the database.
File activation failure. The physical file name "O:\DBFolder\Testdb_LOG2.ldf" may be incorrect.
The log cannot be rebuilt because the database was not cleanly shut down.
Msg 1813, Level 16, State 2, Line 3
Could not open new database ' Testdb '. CREATE DATABASE is aborted.
Msg 823, Level 24, State 6, Line 3
The operating system returned error 38(Reached the end of the file.) to SQL Server during a read at offset 0000000000000000 in file ' O:\DBFolder\Testdb_LOG2.ldf'. Additional messages in the SQL Server error log and system event log may provide more detail. This is a severe system-level error condition that threatens database integrity and must be corrected immediately. Complete a full database consistency check (DBCC CHECKDB). This error can be caused by many factors; for more information, see SQL Server Books Online.