sp_renamedb (Transact-SQL)

Applies to: SQL Server Azure SQL Managed Instance

Changes the name of a database.

Important

This feature will be removed in a future version of SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use ALTER DATABASE MODIFY NAME instead. For more information, see ALTER DATABASE (Transact-SQL).

Transact-SQL syntax conventions

Syntax

sp_renamedb [ @dbname = ] 'old_name' , [ @newname = ] 'new_name'  

Arguments

[ @dbname = ] 'old_name' Is the current name of the database. old_name is sysname, with no default.

[ @newname = ] 'new_name' Is the new name of the database. new_name must follow the rules for identifiers. new_name is sysname, with no default.

Return Code Values

0 (success) or a nonzero number (failure)

Remarks

It isn't possible to rename an Azure SQL database configured in an active geo-replication relationship.

Permissions

Requires membership in the sysadmin or dbcreator fixed server roles.

Examples

The following example creates the Accounting database and then changes the name of the database to Financial. The sys.databases catalog view is then queried to verify the new name of the database.

USE master;  
GO  
CREATE DATABASE Accounting;  
GO  
EXEC sp_renamedb N'Accounting', N'Financial';  
GO  
SELECT name, database_id, modified_date  
FROM sys.databases  
WHERE name = N'Financial';  
GO  

Next steps