force DB to go offline

How do I force my Database to go Offline, in no regards to what or who is is already using it?

 

Go offline

USE master GO ALTER DATABASE YourDatabaseName SET OFFLINE WITH ROLLBACK IMMEDIATE GO

Go online

USE master GO ALTER DATABASE YourDatabaseName SET ONLINE GO


Detaching removes the registration of the database within SQL server.  You will need to tell SQL server where the database file(s) are located when you go to make it operational again.

Taking a database offline leaves the registration intact, so all you need to do is set it online to make it operational again.

This is not correct. You should be fine with using OFFLINE/ONLINE to copy a database between instances. It is particularly useful when you have replication or other cross database ties in your database that you don’t want to lose.

Detach and offline both attempt to shutdown the database cleanly and will close the file handles. The only real difference is that detach can change the file ACLs so that the detacher has control of them.


#USE MASTER
ALTER DATABASE %%% SET OFFLINE WITH ROLLBACK IMMEDIATE
ALTER DATABASE %%% SET ONLINE WITH ROLLBACK IMMEDIATE
In case DB's name contains '-', it should be in brackets:
ALTER DATABASE "test-db"
force DB to go offline