Problem:
Previously, we had been using the following script to rename the DB after importing the bacpac file into Tier-1 cloud-hosted environment.
USE master; go ALTER DATABASE AxDB MODIFY NAME = AxDB_OLD; ALTER DATABASE AxDB_20250306 MODIFY NAME = AxDB; go
But this script now gives the following error message.
The database could not be exclusively locked to perform the operation.

Application:
Dynamics 365 for Finance and Operations
Solution:
Please use the following script to successfully rename the DB.
Code
--set main db to single connections and drop the existing connections ALTER DATABASE AxDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE --rename database to DB_OLD ALTER DATABASE AxDB MODIFY NAME = AxDB_OLD --set the DB_OLD to multi user ALTER DATABASE AxDB_OLD SET MULTI_USER --rename the new database to the main DB name ALTER DATABASE AxDB_20250306 MODIFY NAME = AxDB
