Wednesday, April 30, 2008

Database Creation

Recently I had to take an .MDF file, and create a new database in SQL 2005 with it. So, initially, I went to the SQL Management Studio, and created a new database with the name I wanted, stopped the SQL service, navigated to where the .MDF files are stored on disk, and replaced the newly created with the one that contained all the data. This didn't work, so I took to Google.

I lost this gem once, and I don't want to lose it again. So. To restore a database from ONLY an .MDF file, issue the following query:

EXEC sp_attach_single_file_db @dbname = '[databasename]',
@physname = '[path to your .MDF file]'

No comments: