Database

Take a MSSQL database offline and back online with TSQL

By | Code, Database, MSSQL2005, Software Development, SQL Server, TSQL | One Comment

Take a database offline with TSQL:

— Kill All Other Users Processes And Set The Database To Single User Mode
ALTER DATABASE AdventureWorks
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE
— Take Offline
EXEC sp_dboption ‘AdventureWorks’, ‘offline’, ‘TRUE’

And restoring the database back to online with TSQL:

— Allow Users Back In By Setting Multi User Mode On
ALTER DATABASE AdventureWorks SET MULTI_USER
— Bring Back Online
EXEC sp_dboption ‘AdventureWorks’, ‘offline’, ‘FALSE’