views:

91

answers:

4

I'm trying to restore backups of our production databases to our development server. When I run the following script which has previously worked:

RESTORE DATABASE M2MDATA01 FROM DISK = 'C:\Install\SQLBackup\M2MDATA01.SQLBackup' WITH REPLACE,
   MOVE 'M2MDATA01' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\M2MData01.mdf',
   MOVE 'M2MDATA01_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\M2MData01.ldf'

I get the following error:

    Error   12/21/2009 9:06:09 AM 0:00:00.000 SQL Server Database Error: Exclusive access could not be obtained because the database is in use. 5 0

However, I have no idea how what could possibly be using it. How can I tell?

A: 

Profiler is one option.

From Sql Server Management Studio, select Tools|Sql Server Profiler.

  1. Connect to the server instance that the database you are working with is on.
  2. Switch to the Event Selection tab
  3. check the checkbox below the grid labeled "Show all columns"
  4. In the grid find the DatabaseName column and check the entire column.
  5. (optional) press the Column Filters button and filter to the database name you are working with.

This should at least tell you if something is using the database in question.

doug_w
The only activity in Profiler is from the report server. The database in question is not listed.
DavidStein
+3  A: 

Check what's connected (the easy way)

SELECT * FROM sys.sysprocesses S WHERE S.dbid = DB_ID('M2MDATA01')

Note: sysprocesses can not be emulated using dmvs...

Edit, check for locks too

SELECT * FROM sys.dm_tran_locks L WHERE L.resource_type = 'DATABASE' AND L.resource_database_id = DB_ID('M2MDATA01')
gbn
When I run this, no records return.
DavidStein
And in a different query windows whilst running restore?
gbn
+2  A: 

In SQL Server Management Studio, go to Management => Activity Monitor. This will show you all processes that are connected to all databases, and allow you to kill these processes (only recommended as a last resort).

Phil Sandler
When I do this, the only login is mine. There are 5 processes, one of which is running on Tempdb. The rest of the records have nothing listed for Database name. Any ideas?
DavidStein
Hmm, is this 2008? There was a bug at one point with this in 2008, see this link: http://blogs.msdn.com/billramo/archive/2009/01/02/case-of-the-missing-database-name-in-activity-monitor.aspx. Once you have installed the fix, the activity monitor should help you track down the "rogue" process.
Phil Sandler
A: 

Easiest solution here is to delete the database and select the "close existing connections" checkbox before hitting okay. Then the restore will work just fine. It's just DEV right? :}

Jason Cumberland