Once I was in a situation where there was a disaster faced by my client. They lost the drive which was having transaction log file. Since they did not have effective monitoring, they realized that the backup jobs were failing. So essentially they only have MDF files for user databases and they were left with option to rebuild the log. Let us learn about how file activation failure can create an interesting error.
They tried below command.
CREATE DATABASE UserDB ON (FILENAME = 'E:\DATA\UserDB.mdf') FOR ATTACH_REBUILD_LOG
But, this was the error received while attaching the database.
File activation failure. The physical file name “E:\LOG\UserDB_log.ldf” may be incorrect.
The log cannot be rebuilt because there were open transactions/users when the database was shutdown, no checkpoint occurred to the database, or the database was read-only. This error could occur if the transaction log file was manually deleted or lost due to a hardware or environment failure.
Msg 1813, Level 16, State 2, Line 1
Could not open new database ‘UserDB’. CREATE DATABASE is aborted.
The error is very clear and interesting. So, I thought of reproducing it
CREATE DATABASE SQLAuthority GO USE SQLAuthority GO CREATE TABLE Foo (bar INT) GO BEGIN TRANSACTION INSERT INTO Foo VALUES (1)
Yes, there is no rollback transaction because I wanted to leave open transaction, as mentioned in the error message. Once done, I stopped SQL Server, renamed MDF and LDF file for the database and started the SQL Server service. As expected, the database came to “Recovery Pending” State.
Here was the cause in ERRORLOG
2016-09-29 00:31:55.86 spid21s Starting up database ‘SQLAuthority’.
2016-09-29 00:31:55.87 spid21s Error: 17204, Severity: 16, State: 1.
2016-09-29 00:31:55.87 spid21s FCB::Open failed: Could not open file C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority.mdf for file number 1. OS error: 2(The system cannot find the file specified.).
2016-09-29 00:31:55.87 spid21s Error: 5120, Severity: 16, State: 101.
2016-09-29 00:31:55.87 spid21s Unable to open the physical file “C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority.mdf”. Operating system error 2: “2(The system cannot find the file specified.)”.
2016-09-29 00:31:55.87 spid20s [INFO] HkHostDbCtxt::Initialize(): Database ID: [4] ‘msdb’. XTP Engine version is 0.0.
2016-09-29 00:31:55.87 spid21s Error: 17207, Severity: 16, State: 1.
2016-09-29 00:31:55.87 spid21s FileMgr::StartLogFiles: Operating system error 2(The system cannot find the file specified.) occurred while creating or opening file ‘C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority_log.ldf’. Diagnose and correct the operating system error, and retry the operation.
2016-09-29 00:31:55.87 spid21s File activation failure. The physical file name “C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority_log.ldf” may be incorrect.
At this point, since I wanted to reproduce client situation, I dropped the database so that I should try to attach MDF file. I renamed file to “SQLAuthority_original.mdf’”
CREATE DATABASE SQLAuthority ON (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority_original.mdf') FOR ATTACH CREATE DATABASE SQLAuthority ON (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority_original.mdf') FOR ATTACH_REBUILD_LOG
Both commands failed with the same error
File activation failure. The physical file name “C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority_log.ldf” may be incorrect.
The log cannot be rebuilt because there were open transactions/users when the database was shutdown, no checkpoint occurred to the database, or the database was read-only. This error could occur if the transaction log file was manually deleted or lost due to a hardware or environment failure.
Msg 1813, Level 16, State 2, Line 1
Could not open new database ‘SQLAuthority’. CREATE DATABASE is aborted.
SOLUTION/WORKAROUND
There is an undocumented option called as ATTACH_FORCE_REBUILD_LOG. Here is the command which worked in my lab.
CREATE DATABASE SQLAuthority ON (FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority_original.mdf') FOR ATTACH_FORCE_REBUILD_LOG
Here was the message:
File activation failure. The physical file name “C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority_log.ldf” may be incorrect.
New log file ‘C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority_log.ldf’ was created.
Restoring from backup is always a best solution and above is the last resort. Have you even been in such situation?
Reference: Pinal Dave (https://blog.sqlauthority.com)
40 Comments. Leave new
I have gone through this situation 1 or 2 time in my life. But I generally used last backup to recover the db. At that time I am unaware of ‘ATTACH_FORCE_REBUILD_LOG’. this is very helpful.
this is only worked with me :
CREATE DATABASE SQLAuthority ON (FILENAME = ‘C:\Program Files\Microsoft SQL Server\MSSQL13.MSSQLSERVER\MSSQL\DATA\SQLAuthority_original.mdf’)
FOR ATTACH_FORCE_REBUILD_LOG
You are a superstar!
Everyone is a superstar :)
VERY helpful — thanks very much for the post! Used it to help a client that had no SQL Server backups and a corrupted VM.
Is there any catch for this?
Thanks for the post. It work fine for me.
thanks very lot .. so helpful
I am glad to hear that Puvi. Thanks for sharing your thought.
The ATTACH_FORCE_REBUILD_LOG was helpful on my case, thanks for the post!
The ATTACH_FORCE_REBUILD_LOG worked for me.Thanks a lot!
Hi!! I’m trying to create a new database from a mdf file using the ATTACH_FORCE_REBUILD_LOG but it returns the next message
Could not open new database ‘FGD_res’. CREATE DATABASE is aborted.
Mens. 5125, Nivel 24, Estado 2, Línea 1
File ‘C:\basesdatos\FGD.mdf’ appears to have been truncated by the operating system. Expected size is 72747968 KB but actual size is 72746752 KB.
Is there any idea to help me solve this?
THANKS A LOT Pinal!
Error messages mean file has been modified by someone other than SQL. nothing can be done.
You are a life saver :)
Thanks you for this blog :)
Thank you for saving my day!
very thank you work for me. loss ldf file
Thank you very.
thanks it works
Awesome – thanks so much! I have used your hints many times.
Just to let you know my situation, I have to do a restore of a database to a previous point in time in order to get data from a table at that time. I have to transfer over 75 GB of databases over a fairly slow connection from a ShadowProtect backup that saved the database in the current state. I did not want to have to spend even more time transferring log files, so this is just what I needed!
Thanks again,
SCarter
Pinal, you are a life saver (again!)
Pinalbhai, I was able to use this today.
Glad to here bhai.
You need a donate link. I owe you a beer. Thanks again.
Thank you so much. I am just happy to help.