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.

 All Forums
 SQL Server 2005 Forums
 SQL Server Administration (2005)
 Ok, let me rephrase this (restore from backup)

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 IMMEDIATE

And then if you've got users with db_owner still connected:


DECLARE @spid varchar(10)

SELECT @spid = spid
FROM master.sys.sysprocesses
WHERE dbid IN (DB_ID('dbName'))

WHILE @@ROWCOUNT <> 0
BEGIN
EXEC('KILL ' + @spid)

SELECT @spid = spid
FROM master.sys.sysprocesses
WHERE
dbid IN (DB_ID('dbName')) AND
spid > @spid
END



Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

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
Go to Top of Page

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.
Go to Top of Page

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 :)
Go to Top of Page

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 sysname
set @DB_Name = 'MyDatabase'

-- Take DB offline so no users are in database during restore
if databasepropertyex(@DB_Name,'Status') = 'ONLINE'
begin
exec ('
use master
print ''Set Database ' + @DB_Name + ' to OFFLINE''
alter database ' + @DB_Name + ' set offline with rollback immediate
')
end

--Restore command, etc.




CODO ERGO SUM
Go to Top of Page
   

- Advertisement -