How to rename/delete Azure SQL DB

T West 136 Reputation points
2021-02-15T21:32:24.283+00:00

Hi,

I'm trying to rename/delete our existing dev SQL Azure DB but I seem to be getting the following error:

68397-sqlerror.jpg

Any ideas what I need to do please?

Thanks

Azure SQL Database
{count} votes

2 answers

Sort by: Most helpful
  1. KalyanChanumolu-MSFT 8,356 Reputation points
    2021-02-16T04:34:30.87+00:00

    @T West Thank you for reaching out.

    You can rename user created databases only and not the system databases.

    Renaming a user created database is not supported from the Azure Portal.
    However, you can easily do this using SSMS.

    68455-image.png

    If you have any active connections on the database, you will need to kill them using the below script before renaming

    DECLARE @kill varchar(8000) = '';  
      
    SELECT @kill = @kill + 'KILL ' + CONVERT(varchar(5), c.session_id) + ';'  
      
    FROM sys.dm_exec_connections AS c  
    JOIN sys.dm_exec_sessions AS s  
        ON c.session_id = s.session_id  
    WHERE c.session_id <> @@SPID  
    --AND status = 'sleeping'  
    ORDER BY c.connect_time ASC  
      
    EXEC(@kill)  
    

    ----------

    If an answer is helpful, please "Accept answer" or "Up-Vote" for the same which might be beneficial to other community members reading this thread.

    3 people found this answer helpful.

  2. SUNOJ KUMAR YELURU 17,401 Reputation points MVP Volunteer Moderator
    2021-02-16T01:20:06.327+00:00

    HI @T West
    Renaming Database - Limitations and Restrictions
    System databases cannot be renamed.
    The database name cannot be changed while other users are accessing the database.
    In SQL Server, you can set a database in single user mode to close any open connections. For more information, see set the database to single-user mode.
    In Azure SQL Database, you must make sure no other users have an open connection to the database to be renamed.

    Rename with command-line – TSQL

    1. Connect with SQL Server Management Studio to your Azure database server
    2. Right-click on the master database and select New Query
    3. In the New Query window type ALTER DATABASE [dbname] MODIFY NAME = [newdbname]. (Make sure you include the square brackets around both database names.)

    Delete a Database
    Create, Copy, Rename And Delete SQL Database In Azure

    If the Answer is helpful, please click Accept Answer and up-vote, this can be beneficial to other community members.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.