In this article, we will show you, the steps involved to detach Database in SQL.
To demonstration this SQL detach database, we are going to use the existing stored procedure and SQL Server Management Studio.
SQL Detach Database Approach 1
From the below screenshot, you can see that the SQL server have the AdventureWorks2017 database
SQL Server has a sp_detach_db stored procedure to detach database from SQL Server.
Syntax for SQL detach Database is
EXEC sp_detach_db N'Database Name', 'true';
Let me use the above syntax to detach AdventureWorks 2017 database.
EXEC sp_detach_db N'AdventureWorks2017', 'true';
From the below screenshot you can see that the query is executed.
Now you can see the Adventure Works 2017 database is removed from our Server
SQL Detach Database Approach 2
You can also use Management Studio to detach database in Sql Server. To do so, Right-click on the Databases folder, and select the Tasks option and then select Detach.. from the context menu.
Clicking the Detach.. option will open the following window.
- Database Name: This will display the Name of the database that you want to detach.
- Drop Connections: Checkmark this option to drop all the existing connections pointing to this database
- Update Statistics: Checkmark this option to update existing optimization statistics.
Checkmark the Drop Connections, and Click Ok
Now you can see that the Adventure Works DW 2017 database is removed from the Server
The above process will only detach the database from Server, but it will keep the MDF, and LDF files within the physical location (C Drive)