Attaching a .mdf file when you don’t have the .ldf file available

- 1 minute read

Hi,

ASP.NET 2.0 is promoted quite heavily by Microsoft and other companies or dedicated sites. What most people seem to love are the starter kits which have several dedicated members that like to extend these starter kits and expose these on the internet. If you would be interested in extending some of the starter kits, or just have one deployed as your own personal website, you can download them here.

Most of these starter kits ship with a sample database which normally consists of a .mdf and a .ldf file. Not all people have SQL Server 2005 Express edition installed but rather only SQL Server 2005. It’s quite easy to attach such an Express database to SQL Server 2005 when you have both files available but as I found out in the past sometimes only the .mdf file is provided which prohibits one to attach the database the normal way.

Luckely there’s a special procedure available: sp_attach_single_file_db.

An example on how to use it: EXEC sp_attach_single_file_db @dbname = ‘pubs’, @physname = ‘c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs.mdf’

The @dbname = and the @physname can be omitted if you like.

Grz, Kris.

Leave a Comment