Changes the name of a user database.
sp_renamedb dbname, newname
sp_renamedb accounting, financial
sp_dboption work, single, true go use work go checkpoint go sp_renamedb work, workdb go use master go sp_dboption workdb, single, false go use workdb go checkpoint go
sp_renamedb changes the name of a database. You cannot rename system databases or databases with external referential integrity constraints.
The system administrator must place a database in single-user mode with sp_dboption before renaming it and must restore it to multi-user mode afterward.
select object_name(tableid), db_name(frgndbid) from sysreferences where frgndbid is not null
select object_name(reftabid), db_name(pmrydbid) from sysreferences where pmrydbid is not null
Use alter table to drop the cross-database constraints in these tables. Then, rerun sp_renamedb.
Drop all stored procedures, triggers, and views that include the database name
Change the source text of the dropped objects to reflect the new database name
Re-create the dropped objects
Change all applications and SQL source scripts that reference the database, either in a use database_name command or as part of a fully qualified identifier (in the form dbname.[owner].objectname)
If you use scripts to run dbcc commands or dump database and dump transaction commands on your databases, be sure to update those scripts.
See also create database in Reference Manual: Commands.
The permission checks for sp_renamedb differ based on your granular permissions settings.
Setting | Description |
---|---|
Enabled | With granular permissions enabled, you must be a user with own database privilege on the database. |
Disabled | With granular permissions disabled, you must be a user with sa_role. |
Values in event and extrainfo columns from the sysaudits table are:
Information | Values |
---|---|
Event | 38 |
Audit option | exec_procedure |
Command or access audited | Execution of a procedure |
Information in extrainfo |
|