AngelEyes on .Net

Tips, tricks and WTFs about Asp .Net, SQL Server and the likes.
posts - 54 , comments - 235 , trackbacks - 6

My Links

News

Tag Cloud

Archives

3rd party

Official ASP .NET

Useful info

Kill connections to resote DB - SQL Server

The following code is from Ralph Whitbeck's blog. This is really a "cut and save" for me :)

When restoring an SQL Server Database, you'll often need to kill existing connections to it, before starting the task.

Change databaseName To the actual database name.

Use Master
Go

Declare @dbname sysname

Set @dbname = 'databaseName'

Declare @spid int
Select @spid = min(spid) from master.dbo.sysprocesses
where dbid = db_id(@dbname)
While @spid Is Not Null
Begin
        Execute ('Kill ' + @spid)
        Select @spid = min(spid) from master.dbo.sysprocesses
        where dbid = db_id(@dbname) and spid > @spid
End

 

Print | posted on Wednesday, February 24, 2010 11:50 AM |

Feedback

Gravatar

# re: Kill connections to resote DB - SQL Server

Thanks, nice tip!
8/22/2010 11:45 AM | Morteza
Gravatar

# re: Kill connections to resote DB - SQL Server

thanks indeed for you help
10/28/2013 8:15 AM | Khaled EL Sheikh
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: