Home » Topic » How to rename SQL Server database?

How to rename SQL Server database?

Lincoln Burrows ~ Modified: December 19th, 2015 ~ ~ 1 Minute Reading

Home Forums How to rename SQL Server database?

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1003 Score: 0
    Lincoln Burrows
    Moderator
    16 pts

    I am working on SQL Server 2008 R2, I have a database that I can access with SQL Server Management Studio and has name “MyDatabase”. I want to change the database name to “MyDatabaseNew.

    What is the correct procedure to rename the SQL Server Database

    #1015 Score: 0
    Andrew Jackson
    Moderator
    1 pt

    Execute the following script to rename the database

    USE master;
    GO
    EXEC sp_dboption MyDatabase, 'Single User', True
    GO
    EXEC sp_renamedb ' MyDatabase', ' MyDatabaseNew’
    GO
    EXEC sp_dboption MyDatabase, 'Single User', False
    GO
    #1018 Score: 0
    Lincoln Burrows
    Moderator
    16 pts

    Thanks Andrew for providing this script it works fine, but I also want to know can I rename the physical file. for example
    MyDatabase.mdf
    to
    MyDatabaseNew.mdf
    Is it possible to change the physical file name

    #1032 Score: 0
    Andrew Jackson
    Moderator
    1 pt

    To change the name of the physical file you have to take the SQL Server database Offline

    • Open SQL Server Management Studio
    • Right-click on Database which you want to rename
    • Click on Tasks
    • Then select Take Offline

    Change the name of the physical files at the OS level and then Bring the database Online.

    #1035 Score: 0
    Stephen West
    Moderator
    4 pts

    You can rename sql server database using transact-sql and sql server management studio, however to rename the physical files you need to detach the database. This post shows you step by step method to rename sql server database and physical files:
    http://www.sqlserverlogexplorer.com/how-to-rename-a-database/

    #1038 Score: 0
    Lincoln Burrows
    Moderator
    16 pts

    Thanks Stephen for this informative link this is exactly what I am looking for I just did it on my SQL Server 2008

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.