EXEC sp_dboption N'mydb', N'offline', N'true'
OR
ALTER DATABASE [mydb] SET OFFLINE WITH
ROLLBACK AFTER 30 SECONDS
OR
ALTER DATABASE [mydb] SET OFFLINE WITH
ROLLBACK IMMEDIATE
Using the alter database statement (SQL Server 2k and beyond) is the preferred method. The rollback after statement will force currently executing statements to rollback after N seconds. The default is to wait for all currently running transactions to complete and for the sessions to be terminated. Use the rollback immediate clause to rollback transactions immediately.
Reference : Pinal Dave (http://blog.SQLAuthority.com) , BOL





Nice post, with some useful samples.
[...] Reference : Pinal Dave (http://www.SQLAuthority.com), SQL SERVER – 2005 Take Off Line or Detach Database [...]
And SQL sERVER 6.5? wATH
Hi Dave!
ALTER DATABASE [mydb] SET OFFLINE WITH ROLLBACK IMMEDIATE
Works just fine but if I want to take many DBs (354) offline with a script at the same time. Can that be done. I also what to exclude some DBs from beeing taken offline.
/Magnus
Magnus, sure it can. A quick way to do this is simply to use a cursor and a query similar to -
select name from sysdatabases where name not in (‘master’, ‘msdb’, ‘model’, ‘tempdb’, ‘donttakeoffline’)
*note ‘donttakeoffline’ is the name of a db you might want to exclude, the others you shot NOT take offline)
Have the cursor dynamically build the command and exec such as this –
set @cmd = ‘Alter database ‘ + @dbname + ‘ set offline with rollback immediate’
exec (@cmd)
If you want to exclude them you can exclude from the original query for the cursor or use an IF statement example
If @dbname != ‘dontoffline’
begin
exec @cmd
else
end
Mind you, doing this can be dangerous and is not 100% certain. Most things like this should be done one at a time and/or monitored very closely.
If I were going to do this, I would have it record into a table somewhere of the status of taking it offline and when it was taken offline for each DB.
*not responsible for people breaking their DB using this information*
Hi dave
i want to lock the record and send the message to user who try to access the same record for certain time
can u help me out .
how to shrink transaction loh in sql 2005.
Hi Pinal,
1.Can we shrink the log file after taking database into offline?
Hello,
does a offline database uses less memory ?
hi i want to know how i can create an offline server to play for example Lineage-i own SQL and navicat
i want to know how i can create an offline server to play for example Lineage-i own sql and navicat
Carlos,
for SQL Server 6.5, you can use sp_dboption dbname, offline, true
I tried to Drop database from web page, by executing SQL Command, then show the result, done or error. And I got this message : Cannot drop database “detach” because it is currently in use.
Thanks for your help.
I assume using the command -
ALTER DATABASE [mydb] SET OFFLINE WITH
ROLLBACK AFTER 30 SECONDS
will close the existing connections as I want to restore my DB and restore command fails with
“Exclusive access could not be obtained because the database is in use.”
Any help will be appreciated.
Thanks