sp_renamedb

Description

Changes the name of a user database.

Syntax

sp_renamedb dbname, newname

Parameters

dbname

is the original name of the database.

newname

is the new name of the database. Database names must conform to the rules for identifiers and must be unique.

Examples

Example 1

Renames the accounting database to financial:

sp_renamedb accounting, financial

Example 2

Renames the database named work, which is a Transact-SQL reserved word, to workdb. This example shows how sp_dboption is used to place the work database in single-user mode before renaming it and restore it to multi-user mode afterward:

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

Usage

Permissions

The permission checks for sp_renamedb differ based on your granular permissions settings.

Granular permissions enabled

With granular permissions enabled, you must be a user with own database privilege on the database.

Granular permissions disabled

With granular permissions disabled, you must be a user with sa_role.

Auditing

Values in event and extrainfo columns from the sysaudits table are:

Event

Audit option

Command or access audited

Information in extrainfo

38

exec_procedure

Execution of a procedure

  • Roles – Current active roles

  • Keywords or options – NULL

  • Previous value – NULL

  • Current value – NULL

  • Other information – All input parameters

  • Proxy information – Original login name, if set proxy in effect

See also

Commands create database

System procedures sp_changedbowner, sp_dboption, sp_depends, sp_helpdb, sp_rename