Scripts: Switch AxDB databases

Automatically 

Switch-D365ActiveDatabase -SourceDatabaseName "AxDB_new"

Manual

--set main db to single connections and drop the existing connections
ALTER DATABASE AxDB SET SINGLE_USER WITH ROLLBACK IMMEDIATE
--rename datbase to db_old
ALTER DATABASE AxDB MODIFY NAME = AxDB_old
--set the old db to multi user
ALTER DATABASE AxDB_old SET MULTI_USER
--rename the new db to the main db name
ALTER DATABASE AxDB_new MODIFY NAME = AxDB

Issue during change

Changes to the state or options of database 'AxDB_new' cannot be made at this time. The database is in single-user mode, and a user is currently connected to it.

USE master
GO
DECLARE @kill varchar(max) = '';
SELECT @kill = @kill + 'KILL ' + CONVERT(varchar(10), spid) + '; '
FROM master..sysprocesses 
WHERE spid > 50 AND dbid = DB_ID('AxDB_new')
EXEC(@kill);
GO
SET DEADLOCK_PRIORITY HIGH
ALTER DATABASE [AxDB_new] SET MULTI_USER WITH NO_WAIT
ALTER DATABASE [AxDB_new] SET MULTI_USER WITH ROLLBACK IMMEDIATE
GO