Tuesday, September 17, 2013

Forcefully Rename a SQL Server Database






When SQL Server management studio give below error we have forcefully rename  for our database.


Management Studio is telling you that there are connections that it doesn’t want to mess with.
Or when using sp_renamedb from a query window, you might see this similar error:
Msg 5030, Level 16, State 2, Line 1
The database could not be exclusively locked to perform the operation.
If you really really want to rename the database and don’t care at all about in-flight transactions, then use this script/template I recently came up with:
A Forceful rename  Script:

ALTER DATABASE [old_name]
 
 SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [old_name]
 
 MODIFY NAME = [new_name]
GO
ALTER DATABASE [new_name]
 SET MULTI_USER
GO
 
 
 

No comments :