Untitled UI logotext
Solutions
WebsitesEcommerceMobile AppsWeb AppsProduction Support & Maintenance
Our work
Company
About usBlogPodcastContact us
Book a free consultation

How do I drop connections and detach a database in SQL Server

Olivia Rhye

Frequently when moving or modifying databases, they need to be detached. The problem with this is a database can't be detached if there are active connections to it.

The following code snippet disconnects active sessions and detaches the database myDbName

USE master;
GO
ALTER DATABASE myDbName
SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
EXEC sp_detach_db @dbname = 'myDbName', @skipchecks = 'true';
GO

Ready to start a project?

Book a free consultation
Untitled UI logotext
Our work
About us
Blog
Careers
Submit a ticket
Agency Partnerships
Contact
© 2024 fjorge. All rights reserved.
Privacy