Hi @Sara Saleh
I've tried using Rename from Object Explorer to no avail
The database name cannot be changed while other users are accessing the database. In SQL Server, you can set a database in single user mode to close any open connections. For more information, see set the database to single-user mode. Also, it requires ALTER permission on the database.
Use the following steps to rename a SQL Server database using T-SQL in SQL Server Management Studio including the steps to place the database in single-user mode and, after the rename, place the database back in multi-user mode.
- Connect to the
master
database for your instance. - Open a query window.
- Copy and paste the following example into the query window and select Execute.
- Optionally, if the database was your default database, see Reset your default database after rename.
USE master;
GO
ALTER DATABASE "SQL Tutorial" SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE "SQL Tutorial" MODIFY NAME = SqlTutorial;
GO
ALTER DATABASE SqlTutorial SET MULTI_USER;
GO
Note: To quickly obtain exclusive access, the code sample uses the termination option WITH ROLLBACK IMMEDIATE
. This will cause all incomplete transactions to be rolled back and any other connections to the "SQL Tutorial"
database to be immediately disconnected.
Please refer to this tech doc for more details: Rename a Database.
Best regards,
Cosmog Hong
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.