If you are using SQL Server, you can set the database to single-user mode to close any open connections and prevent other users from connecting while you are changing the database name. In Object Explorer, expand Databases, right-click the database to rename, and then select Rename.
In some situations, database users and administrators want to change the name of the database for some technical reasons. So, the Rename Database statement in SQL is used to change the name of the existing database.
ALTER keyword is used to add, delete/drop or modify columns in the existing table. It is also used to add and drop various constraints on the existing table. Renaming means we are modifying the data. Hence, we will use ALTER – MODIFY.
That is because there are open transactions. If those transactions can be killed, then this can easily be done with this SQL
ALTER DATABASE ip_ent_site
SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
sp_rename 'ip_ent_site', 'new_db_name' ,'DATABASE';
GO
ALTER DATABASE new_db_name
SET MULTI_USER
GO
Before renaming, set the database to single user mode MSDN
USE master;
GO
ALTER DATABASE ip_ent_site
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;
Then rename it
USE master
GO
ALTER DATABASE ip_ent_site
Modify Name = ip_ent_site1
GO
And then put it back to multi user mode
ALTER DATABASE ip_ent_site1
SET MULTI_USER;
The reason is because the database has to prevent any other connection/transaction to the db while you are renaming it.
A simple script to get a lock on the db:
ALTER DATABASE [ip_ent_site] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO
ALTER DATABASE [ip_ent_site] MODIFY NAME = [ip_ent_site_new]
GO
ALTER DATABASE [ip_ent_site_new] SET MULTI_USER;
GO
To enable the ability to
right click and rename
the DB:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With