5

I use this query in SQL Server 2008 R2 to restore DB1 database:

RESTORE DATABASE [DB1] 
FROM DISK = N'D:\new.bak' 
WITH RESTRICTED_USER, FILE = 1, NOUNLOAD, REPLACE, STATS = 10;

The following error is displayed:

Msg 3102, Level 16, State 1, Line 1
RESTORE cannot process database 'DB1' because it is in use by this session. It is recommended that the master database be used when performing this operation.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

1 Answer 1

9

The message seems pretty clear: you cannot be using the DB1 database when trying to restore it - instead, use the master database!

So in SQL Server Management Studio, first close all windows that are open and using that database! Then run this:

USE master
GO

RESTORE DATABASE [DB1] 
FROM DISK = N'D:\new.bak' 
WITH RESTRICTED_USER, FILE = 1, NOUNLOAD, REPLACE, STATS = 10;
4
  • can not use 'Use master' in SP. error display.
    – Ahmad Kazemi
    Commented Dec 19, 2012 at 6:00
  • @AhmadKazemi: in what database does your stored procedure exist?
    – marc_s
    Commented Dec 19, 2012 at 6:01
  • i want use this query in SP. Can I do it?
    – Ahmad Kazemi
    Commented Dec 19, 2012 at 6:03
  • 1
    It depends on what database that stored procedure is created inside of. Obviously, you cannot have that stored procedure inside the database you're trying to restore ....
    – marc_s
    Commented Dec 19, 2012 at 6:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.