Rename LDF and MDF

Here’s a script for renaming your database and the LDF and MDF of it.
– Change path and physical name
ALTER DATABASE AdventureWorks2012 modify FILE (NAME = ‘AdventureWorks2012 ‘, filename =
‘D:\AdventureWorks2012_NEW.mdf’);

ALTER DATABASE AdventureWorks2012 modify FILE (NAME = ‘AdventureWorks2012_log’, filename =
‘D:\AdventureWorks2012_old.ldf’);

– Change logical names
ALTER DATABASE AdventureWorks2012 modify FILE (NAME = AdventureWorks2012 , newname =
AdventureWorks2012 _NEW);

ALTER DATABASE AdventureWorks2012 modify FILE (NAME = AdventureWorks2012 , newname =
AdventureWorks2012 _NEW_log);

– take database offline
ALTER DATABASE AdventureWorks2012 SET offline WITH ROLLBACK immediate

– Rename physical files
DECLARE @command1 VARCHAR (250)

SET @command1 = ‘RENAME D:\AdventureWorks2012 .mdf AdventureWorks2012_NEW.mdf’

EXEC master..Xp_cmdshell @command1

SET @command1 = ‘RENAME D:\AdventureWorks2012 .ldf AdventureWorks2012_NEW.ldf’

EXEC master..Xp_cmdshell @command1

– bring database online
ALTER DATABASE AdventureWorks2012 SET online

– rename database
ALTER DATABASE AdventureWorks2012 modify NAME = AdventureWorks2012_NEW

 

 

Lasă un răspuns