r/SQLServer • u/Green-Cartoonist-566 • Jul 23 '26
Question Unable to set database containment to NONE
Hey all, hoping someone has hit this before.
Context: Migrating a ~200GB SQL Server database to Azure Sql Database (not managed instances) using transactional replication to minimize downtime. During prep, I discovered the source database has partial containment enabled, which I need to turn off before replication will work properly.
Steps taken so far:
- Identified all contained database users (logins that exist only at the DB level) and converted/mapped them to server-level logins instead.
- Stopped the application(s) connecting to the DB to make sure there were no active sessions.
- Ran
ALTER DATABASE [dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATEto force single-user mode and kill any remaining connections. - Ran:
ALTER DATABASE [dbname] SET CONTAINMENT = NONE;The problem: The statement just fails, only 'ALTER DATABASE statement failed', no reason given, nothing in the SQL Server error log pointing to a cause. It's not throwing a permissions error, a "in use" error, or anything I can act on. It just doesn't apply.
Things I've already ruled out / checked:
- No contained users remain in
sys.database_principals(authentication_type_desc = DATABASE) - No active connections (single-user mode confirmed via
sys.dm_exec_sessions)
Questions:
- Has anyone run into containment refusing to toggle off even with no active sessions and no contained users?
- Any way to force verbose output/logging on this specific ALTER DATABASE operation so I can actually see what's blocking it?
SQL Server version: Microsoft SQL Server 2016 - Standard Version
5
u/ihaxr 2 Jul 23 '26
Have you tried not being in the current database context?
3
u/alecc Jul 23 '26
Two things worth trying. The generic 'ALTER DATABASE statement failed' almost always means SQL Server raised a second, real error that never reached your session. Set up a quick Extended Events session on error_reported, run the ALTER again, and read everything that fired in that window - the true blocker usually shows up there with a usable message. The default trace sometimes catches it too.
Second: your containment check may be too narrow. authentication_type_desc = 'DATABASE' only finds contained SQL users with passwords. Windows users created without a matching server login are also contained, and they report 'WINDOWS'. Worth running:
select dp.name, dp.type_desc, dp.authentication_type_desc
from sys.database_principals dp
left join sys.server_principals sp on dp.sid = sp.sid
where dp.type in ('U','G','S') and dp.authentication_type <> 0 and sp.sid is null;
Anything that comes back needs dropping or remapping before containment goes to NONE. Also run the ALTER from a fresh connection with USE master first; if your own session sits inside the database while it's in single-user mode, you can block yourself.
One note for after the move: Azure SQL Database leans on contained users, so you'll likely recreate some of these on the target anyway. They only need to be gone on the source for replication's sake.
1
u/B1zmark 1 Jul 23 '26
What tool are you using to run these commands?
1
u/rhbcub Jul 26 '26
Tried putting it back to multi user?
BTW, I use restricted_user, not single_user. All you're after is the rollback immediate anyway.
1
u/kapilverma123 Aug 05 '26
I would check the actual error being returned using a try catch block. Alter database statement failed is usually just the generic wrapper and the real reason is in the secondary error message. Also verify there aren not any remaining contained principals or modules using EXECUTE AS, as those can prevent switching containment back to NONE.
0
u/[deleted] Jul 23 '26
[deleted] — view deleted comment