Friday, April 30, 2010

Restoring SQL Server databases from .mdf files

USE master;
GO

-- first detach the exisitng databse if already exists

EXEC sp_detach_db @dbname = 'AdventureWorks';

-- attach .mdf file to the database
EXEC sp_attach_single_file_db @dbname = 'AdventureWorks',
@physname = N'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Data\AdventureWorks_Data.mdf';