I am in the process of writing a restore database script. I want to be able to connect as sa and run the restore script.
When I run SET SINGLE_USER does this mean that my login is the only user access to the database? Or how is this determined? I want to use this as I get an error saying other connections are active.
Below is my restore script to date. Are there any improvements I can make to this?
ALTER DATABASE AdventureWorks SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
ALTER DATABASE AdventureWorks SET READ_ONLY;
GO
RESTORE DATABASE AdventureWorks FROM DISK = 'C:\AdventureWorks.BAK'
WITH
MOVE N'AdventureWorks_dat' to N'H:\Data\AdventureWorks.mdf',
MOVE N'AdventureWorks_log' to N'E:\Logs\AdventureWorks.ldf',
STATS = 5;
GO
ALTER DATABASE AdventureWorks SET MULTI_USER;
GO
ALTER DATABASE AdventureWorks SET READ_WRITE WITH NO_WAIT;
GO
Best Answer
SET SINGLE_USER means only one user can connect to the database at a time. If someone or some service (SSIS, SSRS, SQL Agent, etc) connects before you than you will have to wait for them to disconnect