Moving datafiles

Just so I can find this script when I need it …

--MoveDataFile.sql

USE master;
GO

-- 1 alter the metadata to the new path

ALTER DATABASE [SomeDatabaseName] MODIFY FILE (NAME = SomeLogicalFileName, FILENAME = 'F:\SQLData\SomeFileName.mdf');

-- 2 take database offline

ALTER DATABASE [SomeDatabaseName] SET OFFLINE;

-- 3 move the datafile via windows explorer

-- 4 bring database online

ALTER DATABASE [SomeDatabaseName] SET ONLINE;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s