Wednesday, August 8, 2007

Restore MSSQL DB from mdf,ldf and ndf files

MS Sql DB comprises mdf, ndf and ldf data files. If you happen to have taken a backup of the database (.bak), then restoring it on to a different instance is quite straight forward (using SQL Server Management Studio).
But in case you just have the database files with you, then one needs to attach it to the instance. Attach is a better technical term for this than restore.

Following is the way we can attach the Database to an instance:

use master
GO

EXEC sp_attach_db @dbname = N'NewDB',
@filename1 = N'C:\Program Files\MasterDB.mdf',
@filename2 = N'C:\Program Files\MasterDB_log.ldf',
@filename3 = N'C:\Program Files\DATA2.ndf',
@filename4 = N'C:\Program Files\DATA3.ndf'

No comments: