stored procedure sp_renamedb is used to change the user database.
sp_renamedb dbname, new_name
Where
dbname is existing database name.
new_name is the new database name.
Before you rename the database, do the following things.
1. Take full backup of database.
2. Database must be set in single user mode using sp_dboption procedure. After rename, we have to set it back to multi-user mode.
3. sp_renamedb fails if any table in the database references, or is referenced by, a table in another database. Use the following query to determine which tables and external databases have foreign key constraints on primary key tables in the current database:
For foreign key references
select object_name(tableid), db_name(forgndbid) from sysreferences where forgndbid is not null
For primary key references
select object_name(reftabid), db_name(prmrydbid) from sysreferences where prmrydbid is not null
If there are any databases references, drop them using alter table command.