Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

terminate all connections using transact-sql 1

Status
Not open for further replies.

password99

Technical User
Jul 19, 2002
122
US
is there a way to terminate all connections using transact-sql (before performing restore opertaion)
 
or a way to get a forced exclusive lock on the DB using Transact-SQL
 
You can kill all the connections in the database by using a cursor. Something like this.
Code:
declare @spid varchar(10)
declare @dbname varchar(100)
set @dbname = 'DatabaseName'
declare cur CURSOR for select spid from master.dbo.sysprocesses where dbid = (select dbid from master.dbo.sysdatabases where name = @dbname)
open cur
fetch next from cur into @spid
while @@fetch_status = 0
begin
    exec ('kill ' + @spid)
    fetch next from cur into @spid
end
close cur
deallocate cur
go
restore database ...
go

Denny

--Anything is possible. All it takes is a little research. (Me)

[noevil]
(My very old site)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top