Difference between revisions of "Renaming a SQL Server database"

From Dot2DotCommunications
Jump to: navigation, search
 
Line 1: Line 1:
 
The following T-SQL script will rename a database:
 
The following T-SQL script will rename a database:
  
use master
+
<pre>
go  
+
use master
+
go  
alter database [original]
+
 
modify name = [renamed]
+
alter database [original]
go
+
modify name = [renamed]
 +
go
 +
</pre>
  
 
If someone's using the database, you'll get a message like this:
 
If someone's using the database, you'll get a message like this:
  
Msg 5030, Level 16, State 2, Line 4
+
<pre style="color: red;">
The database could not be exclusively locked to perform the operation.
+
Msg 5030, Level 16, State 2, Line 4
 +
The database could not be exclusively locked to perform the operation.
 +
</pre>
  
 
If the operation is successful, you'll get something like this:
 
If the operation is successful, you'll get something like this:
  
The database name 'renamed' has been set.
+
<pre>
 +
The database name 'renamed' has been set.
 +
</pre>
  
 
It's a good idea to back up the master database after renaming databases.
 
It's a good idea to back up the master database after renaming databases.

Latest revision as of 09:33, 17 January 2018

The following T-SQL script will rename a database:

use master
go 

alter database [original]
modify name = [renamed]
go

If someone's using the database, you'll get a message like this:

Msg 5030, Level 16, State 2, Line 4
The database could not be exclusively locked to perform the operation.

If the operation is successful, you'll get something like this:

The database name 'renamed' has been set.

It's a good idea to back up the master database after renaming databases.