Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to attach a MS SQL database (.mdf) file with a missing log file (.ldf)

Tonight I ran into a dilemma where I was only supplied with a Microsoft SQL .mdf file. I thought that I would have no problems and just have to Attach the Database file and Microsoft SQL would rebuild the log file for me.

I was wrong and it didn't take any longer than five minutes for me to figure that out. I thought to myself that quite a few people would have had the same problem as me and a solution would be available the World Wide Web. Thanks to Google, I eventually came across a post where a guy had available the SQL command that will attach the database file and build the log from scratch if it detects any errors.

Here is the MSSQL command needed to attach an .mdf Sql Database file with no .ldf file, enjoy!

sp_attach_single_file_db @dbname= 'databasename', @physname= 'C:\Users\Blake\Documents\Databases\databasefile.mdf'

You may get something like the following output to the Messages window

File activation failure. The physical file name "C:\Program Files\Microsoft SQL Server\MSSQL\Data\databasefile_log.ldf" may be incorrect.

New log file 'C:\Users\Blake\Documents\Databases\databasefile_log.LDF' was created.

Converting database 'databasename' from version 100 to the current version 200.



This post first appeared on Blogs - Windowscoding.com;, please read the originial post: here

Share the post

How to attach a MS SQL database (.mdf) file with a missing log file (.ldf)

×

Subscribe to Blogs - Windowscoding.com;

Get updates delivered right to your inbox!

Thank you for your subscription

×