MCITP

MCITP

Thursday, March 10, 2011

Rename physical file name for mdf and ldf

You can use ALTER DATABASE...MODIFY FILE. The example below shows how you might change the logical and physical file names to reflect the new database name using T-SQL after "MyDatabase" was renamed to "MyNewDatabaseName".
ALTER DATABASE MyNewDatabaseName
MODIFY FILE
( NAME = N'MyDatabase',
NEWNAME = N'MyNewDatabaseName',
FILENAME = N'C:\DataFiles\MyNewDatabaseName.mdf');
ALTER DATABASE MyNewDatabaseName
MODIFY FILE
( NAME = N'MyDatabase_Log',
NEWNAME = N'MyNewDatabaseName_Log',
FILENAME = N'C:\LogFiles\MyNewDatabaseName_Log.ldf');
GO
ALTER DATABASE MyNewDatabaseName SET OFFLINE;
GO
--after physical files are renamed
ALTER DATABASE MyNewDatabaseName SET ONLINE;

No comments:

Post a Comment