73

How can i rename the database in sql Azure?

I have tried Alter database old_name {MODIFY NAME = new_name} but not worked.

Is this feature available in SQL Azure or not?

Joseph Idziorek
  • 4,853
  • 6
  • 23
  • 37
Arun Rana
  • 8,426
  • 14
  • 67
  • 107

7 Answers7

133

Just so people don't have to search through the comments to find this... Use:

ALTER DATABASE [dbname] MODIFY NAME = [newdbname]

(Make sure you include the square brackets around both database names.)

Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
Brett
  • 2,101
  • 2
  • 16
  • 20
  • 11
    You do have to be logged into the master database for this to work... And it isn't super obvious how to do that. From the management portal, I went into one of my databases and clicked manage, which opened up the SQL manager. From there, I couldn't change which database to log into... I had to actually log in and click log out, and only then was I able to type master as the database I wanted. Yikes. But the script works fine. :) – Brian MacKay Sep 20 '13 at 01:25
  • 2
    Instead of logging in/out - you can just use the URL https://.database.windows.net/?langid=en-us and type 'master'. If you click the link in the portal it adds an URL parameter of #$database= which pre-loads the field and makes it read-only. – Joe May 07 '14 at 21:04
  • 2
    You don't need to connect to master database in SQl DB V12. You can rename it by connecting to the database. SQL DB V12 supports more alter statements, you can check here: http://www.sqlindepth.com/alter-database-in-sql-v12/ – Sirisha Chamarthi May 28 '15 at 05:50
  • 1
    I ran this command and it worked, however I still got the following error: `Msg 42019, Level 16, State 2, Line 1 ALTER DATABASE operation failed. Internal service error. Msg 0, Level 20, State 0, Line 0 A severe error occurred on the current command. The results if any should be discarded` – Ogglas Oct 11 '16 at 12:55
  • 1
    If you get the "Internal service error" references above by Ogglas, make sure to run the query on the master database. – Cpt. Monac Feb 06 '17 at 21:43
  • I got the internal service error when running this in the Azure Portal SQL tool. But it renamed the database anyway – Nick.Mc Oct 25 '17 at 01:44
31

Please check that you've connected to master database and you not trying to rename system database.

Please find more info here: https://msdn.microsoft.com/en-US/library/ms345378.aspx

Dene
  • 578
  • 5
  • 9
dimko1
  • 872
  • 7
  • 15
  • @ArunRana what db you are connecting to? – dimko1 Oct 11 '11 at 06:41
  • I am connected with SQL Azure database in R2 client of SQL server – Arun Rana Oct 11 '11 at 09:35
  • when you trying to execute sql script what database is used? Please ensure that you are using master db. – dimko1 Oct 11 '11 at 09:56
  • Alter database [APMV2.4] { MODIFY NAME [APMV2.2_Test] } – Arun Rana Oct 11 '11 at 12:12
  • Please try this: USE master; ALTER DATABASE APMV2.4 Modify Name = APMV2,2_TEST; – dimko1 Oct 11 '11 at 12:22
  • Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '.4'. – Arun Rana Oct 11 '11 at 13:36
  • Msg 40510, Level 15, State 2, Line 1 Statement 'ALTER DATABASE' is not supported in this version of SQL Server. – Arun Rana Oct 11 '11 at 14:20
  • This is just incorrect. Are you sure that you are connecting to right database? – dimko1 Oct 11 '11 at 18:18
  • Yes i was wrong, i have change name from sql azure portal with login to master database and use above script, but it was unsuccessful when i was trying with R2 client. thanks – Arun Rana Oct 14 '11 at 08:37
  • 2
    To get this to work recap: 1) Login via web based management tool 2) Run query like: ALTER DATABASE [Blah] MODIFY NAME = [Blah-test] – James Reategui Feb 08 '12 at 21:33
  • that doesn't work. I get: "Msg 40508, Level 16, State 1, Line 1 USE statement is not supported to switch between databases. Use a new connection to connect to a different Database.". if I don't have the "use master" then I get "Msg 5001, Level 16, State 1, Line 1 User must be in the master database." – ekkis Aug 08 '14 at 14:55
  • ah. Brian MacKay points to how to log into the master database in the comment below. +1 for him – ekkis Aug 08 '14 at 14:57
28

You can also connect with SQL Server Management Studio and rename it in Object Explorer. I just did so and the Azure Portal reflected the change immediately.

Do this by clicking on the database name (as the rename option from the dropdown will be greyed out)

d219
  • 2,707
  • 5
  • 31
  • 36
zacharydl
  • 4,278
  • 1
  • 29
  • 23
7

Connect with SQL Server Management Studio to your Azure database server, right-click on the master database and select 'New Query'. In the New Query window that will open type ALTER DATABASE [dbname] MODIFY NAME = [newdbname].

Orlin
  • 308
  • 2
  • 4
5

It's Very simple for now - Connect to DB via SQL Management Studio and Just rename as you generally doing for DB [Press F2 on DB name]. It will allow you to do this and it will immediately reflect the same.

Abhishek Gupta
  • 583
  • 5
  • 6
0

I can confirm the

ALTER DATABASE [oldname] MODIFY NAME = [newname];

works without connecting to master first BUT if you are renaming a restored Azure database; don't miss the space before the final hyphen

ALTER DATABASE [oldname_2017-04-23T09 -17Z] MODIFY NAME = [newname];

And be prepared for a confusing error message in the Visual Studio 2017 Message window when executing the ALTER command

Msg 0, Level 20, State 0, Line 0
A severe error occurred on the current command.  The results, if any, should be discarded.
Robert
  • 31
  • 3
0

You can easily do it from SQL Server Management Studio, Even from the community edition.

Ali Sufyan
  • 94
  • 1
  • 3