|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 8:06 AM
Points: 1,134,
Visits: 818
|
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: 2 days ago @ 8:24 AM
Points: 122,
Visits: 152
|
|
Hi
This is quicker: alter database [db_name] set single_user with rollback immediate go
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, March 12, 2013 3:40 PM
Points: 39,
Visits: 64
|
|
the original script might kill connections that are active, what about new incoming connections, they will still be created.
With the set single_user also it is not possible to achieve the desired results, for e.g. in a farm environment where you have 10 web front ends connecting to a single sql cluster instance.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Tuesday, April 02, 2013 9:29 AM
Points: 134,
Visits: 159
|
|
| Thanks for the script. It's useful for me.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, March 07, 2013 11:07 AM
Points: 266,
Visits: 159
|
|
Here is a variation of the kill script that can be used on any database whether or not it is the current database. To use, set the @dbname variable to the name of the database where the connections are to be killed.
declare @dbname sysname, @spid int, @cnt int, @sql varchar(2048)
set @dbname = 'targetDB'
select @spid = min(spid), @cnt = count(*) from master.dbo.sysprocesses where dbid = db_id(@dbname) and spid != @@spid
print @dbname + ' connection process(es) to kill: ' + rtrim(@cnt)
while @spid is not null begin print 'kill connection process ' + rtrim(@spid) set @sql = 'kill ' + rtrim(@spid) exec(@sql) select @spid = min(spid), @cnt = count(*) from master.dbo.sysprocesses where dbid = db_id(@dbname) and spid != @@spid end
print 'done'
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, April 10, 2013 10:43 PM
Points: 4,
Visits: 165
|
|
This script will save me a lot of time when restoring test databases. It's simply and replaces a process I was doing manually. Thanks.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, May 22, 2013 11:03 AM
Points: 3,
Visits: 126
|
|
I agree, I think the ALTER DATABASE <databaseName> SET SINGLE_USER WITH ROLLBACK IMMEDIATE is more elegant and precludes the risk of active connections occurring while the cursor is killing the ones it knew about at the time the cursor was opened.
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 10:43 AM
Points: 2,945,
Visits: 10,517
|
|
Setting the database to single user do not prevent someone from making a connection. It just kills the current connections, and prevent users from making more than one new connection.
If all you need to do is get everyone out of the database to do a restore, this will do it:
use master alter database [db_name] set offline with rollback immediate
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, April 10, 2013 10:43 PM
Points: 4,
Visits: 165
|
|
use master alter database [db_name] set offline with rollback immediate
I will try this next time I replace a test DB with a copy of production. Makes sense.
Does a restore automatically set the database online? I'll test when I try it.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, March 12, 2013 3:40 PM
Points: 39,
Visits: 64
|
|
After setting a db to offline it wont be available for a restore, however, you put all statements in a single transaction
--Step1 alter database <dbname> set offline with rollback immediate -- this is the part that kills the connections
--Step2 alter database <dbname> set online
restore database <dbname> <restore parameters>
[b]Credit: This was actually given to me by an on call DBA. Nice alternative instead of having to shutdown IIS on all my web servers to do a db restore./b]
|
|
|
|