Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
SiliconSeed
Starting Member
3 Posts |
Posted - 2008-07-29 : 17:30:36
|
| Since I can't figure out how to kick users....How do you restore over a database from a backup file using a script?I have a script that restores over a database but because SQL does not allow this while (it thinks) others are connected to the database it fails.How can I tell it to restore anyways? |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-07-29 : 17:43:52
|
Just kick them out before the restore:ALTER DATABASE dbName SET RESTRICTED_USER WITH ROLLBACK IMMEDIATEAnd then if you've got users with db_owner still connected:DECLARE @spid varchar(10)SELECT @spid = spidFROM master.sys.sysprocessesWHERE dbid IN (DB_ID('dbName'))WHILE @@ROWCOUNT <> 0BEGIN EXEC('KILL ' + @spid) SELECT @spid = spid FROM master.sys.sysprocesses WHERE dbid IN (DB_ID('dbName')) AND spid > @spid ENDTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-07-29 : 18:15:52
|
This is even easier, because once the database is offline, it is impossible for anyone to connect to it.alter database MyDatabase set offline with rollback immediate CODO ERGO SUM |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-07-29 : 19:01:27
|
quote: Originally posted by Michael Valentine Jones This is even easier, because once the database is offline, it is impossible for anyone to connect to it.alter database MyDatabase set offline with rollback immediate CODO ERGO SUM
I think you can't restore DB in offline mode. You can put in Single User mode but transactions has to be completed. |
 |
|
|
SiliconSeed
Starting Member
3 Posts |
Posted - 2008-07-30 : 12:29:22
|
| tkizer's code seems to be working! I can't believe how many steps it takes but all is well if it does the job. Thank you. Mabey in 2008 microsoft will come out with an ALTER DATABASE KICK_USERS option :) |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-07-30 : 13:29:33
|
quote: Originally posted by sodeep
quote: Originally posted by Michael Valentine Jones This is even easier, because once the database is offline, it is impossible for anyone to connect to it.alter database MyDatabase set offline with rollback immediate CODO ERGO SUM
I think you can't restore DB in offline mode. You can put in Single User mode but transactions has to be completed.
You can restore in offline mode.This is normally how I do restores to existing databases, expecially if they are automated restores in jobs.Sample code to use in a job. Uses dynamic SQL, because it otherwise it would fail if the DB does not exit, or is not online.declare @DB_Name sysnameset @DB_Name = 'MyDatabase'-- Take DB offline so no users are in database during restoreif databasepropertyex(@DB_Name,'Status') = 'ONLINE'beginexec ('use masterprint ''Set Database ' + @DB_Name + ' to OFFLINE''alter database ' + @DB_Name + ' set offline with rollback immediate')end--Restore command, etc.CODO ERGO SUM |
 |
|
|
|
|
|
|
|