You are here -> Home > Developer Center > Attach database from single mdf file using T-SQL

Attach database from single mdf file using T-SQL
By: Jason Nowicki This article has been viewed 1188 times.

A lot of times, one needs to move a database to another server.  Although this can be done with performing a backup and restore, there can be conflits when there was already a database with the same name on the new server.  I find it easier to detach the database, and move just the mdf file to the new server and then attach it there.  To simplify things, unless needed, I don't normally move the transaction log as this is not normally needed when doing development work.  The following script will allow you to attach the single mdf file and also will create a new log file for the newly attached database.

   EXEC sp_attach_single_file_db @dbname = 'pubs',
   @physname = 'c:\Program Files\Microsoft SQL Server\MSSQL\Data\pubs_Data.MDF'

Just replace the database name pubs in this example and enter the correct physicalname path.

 

How helpful was this article?