views:

1523

answers:

4

My SQL Server 2005 doesn't restore a backup because of active connections. How to force it?

+4  A: 
George Stocker
Sorry, I do not see any kill connection button
Jader Dias
It's not a button, it's on the dialog that pops up.
George Stocker
Haha, I always use this method, always felt wrong to me, glad others do it too...
JonoW
I think it's a little hacky myself; but it works.
George Stocker
+5  A: 

You want to set your db to single user mode, do the restore, then set it back to multiuser:

ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK AFTER 60 --this will give your current connections 60 seconds to complete

--Do Actual Restore
RESTORE DATABASE YourDB
FROM DISK = 'D:BackUpYourBaackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:DataYourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:DataYourLDFFile.mdf'

/*If there is no error in statement before database will be in multiuser
mode.  If error occurs please execute following command it will convert
database in multi user.*/
ALTER DATABASE YourDB SET MULTI_USER
GO

Reference : Pinal Dave (http://blog.SQLAuthority.com)

brendan
Rather than issuing an IMMEDIATE ROLLBACK , it may be pertinent to only ROLLBACK after a specificed DELAY, thereby giving user queries an opporunity to complete naturally.
John Sansom
Good point, updated to rollback to include the AFTER 60 command to allow current queries to complete
brendan
+1  A: 

This question is a duplicate. Already asked here. If you want to wait for existing connections to finish, disallow new connections, put DB in single user mode and restore - LOGON Triggers might be a very good option.

Raj

Raj
+1  A: 

Restarting SQL server will disconnect users. Easiest way I've found - good also if you want to take the server offline.

But for some very wierd reason the 'Take Offline' option doesn't do this reliably and can hang or confuse the management console. Restarting then taking offline works

Sometimes this is an option - if for instance you've stopped a webserver that is the source of the connections.

Simon_Weaver