Error - Exclusive access could not be obtained because the database is in use

  • I am actually trying to make a script (in Sql Server 2008) to restore one database from one backup file. I made the following code and I am getting an error -

    Msg 3101, Level 16, State 1, Line 3

    Exclusive access could not be obtained because

    the database is in use.

    Msg 3013, Level 16, State 1, Line 3

    RESTORE DATABASE is terminating abnormally.

    How do I fix this problem ?

    IF DB_ID('AdventureWorksDW') IS NOT NULL

    BEGIN

    RESTORE DATABASE [AdventureWorksDW]

    FILE = N'AdventureWorksDW_Data'

    FROM

    DISK = N'C:\Program Files\Microsoft SQL Server\

    MSSQL10_50.SS2008\MSSQL\Backup\AdventureWorksDW.bak'

    WITH FILE = 1,

    MOVE N'AdventureWorksDW_Data'

    TO N'C:\Program Files\Microsoft SQL Server\

    MSSQL10_50.SS2008\MSSQL\DATA\AdventureWorksDW.mdf',

    MOVE N'AdventureWorksDW_Log'

    TO N'C:\Program Files\Microsoft SQL Server\

    MSSQL10_50.SS2008\MSSQL\DATA\AdventureWorksDW_0.LDF',

    NOUNLOAD, STATS = 10

    END

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply