Rename Database and Files

By James|12/30/2011|,

Sometimes you roll out a database and later decide that the name you picked isn't what you really want. To rename a SQL database and it's associated files, use the Detach and Attach functions. Let's say the original database was named "MyStupidDatabase" and used files C:\MyStupidDatabase.mdf and C:\MyStupidDatabase.ldf. We want to rename the database and it's associated files to "MyFreakingAwesomeDatabase".

Detach Database

To detach the database, use the sp_detach_db stored procedure:

use master
go
sp_detach_db 'MyStupidDatabase'
go

After detaching the database, rename MyStupidDatabase.mdf to MyFreakingAwesomeDatabase.mdf and MyStupidDatabase.ldf to MyFreakingAwesomeDatabase.ldf.

Attach Database

Once you've renamed your database and log files, it's time to attach the database with the sp_attach_db stored procedure:

use master
go
sp_attach_db 'MyFreakingAwesomeDatabase', 'C:\MyFreakingAwesomeDatabase.mdf', 'C:\MyFreakingAwesomeDatabase.ldf'
go
Copyright 2011 - 2024 The Lazy IT Admin | All Rights Reserved
menu-circlecross-circle linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram