Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to gain exclusive access to SQL Server 2005 database to restore?

Tags:

Whenever I restore a backup of my database in SQL Server I am presented with the following error:

Msg 3101, Level 16, State 1, Line 1
Exclusive access could not be obtained because the database is in use.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

Usually to get around this I just restart the server. This was fine when we were developing on our local instance on our development machines. But we have a few programmers that need to access the database, and the logistics of having everyone script their changes and drop them into Subversion was becoming a nightmare. Regardless our simple solution was to put it on a shared server in the office and backup the server occasionally in case someone screwed up the data.

Well, I screwed up the data and needed to restore. Unfortunately, I have another co-worker in the office who is working on another project and is using the same database server for development. To be nice I'd like to restore without restarting the SQL Server and possibly disrupting his work.

Is there a way to script in T-SQL to be able to take exclusive access or to drop all connections?

like image 539
RedWolves Avatar asked Oct 08 '08 16:10

RedWolves


People also ask

What permission is required to restore SQL Server database?

If the database being restored does not exist, the user must have CREATE DATABASE permissions to be able to successfully restore the database. If the database exists, RESTORE permissions default to members of the sysadmin and dbcreator fixed server roles and the owner ( dbo ) of the database.

Can you restore a SQL 2005 database to SQL 2019?

It's not true. You can backup+restore (safest way) or detach+attach (not a safe way) from SQL Server 2005 (or higher) to any other newer version. You just can't do an in-place upgrade to the latest version from SQL Server 2005.

Can we restore SQL Server 2005 backup to 2017?

Hi @Doria, Can I restore a database backup from SQL Server 2005 (details below) to a SQL Server 2017 Standard? Yes, you can.


2 Answers

I find this vastly faster and generally better than taking offline. Do read about it in MSDN so you understand the caveats. If using aysnc statistics, you have to turn those off, as well.

-- set single user, terminate connections
ALTER DATABASE [target] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE ...
ALTER DATABASE [target] SET MULTI_USER

The "with rollback immediate" is the essential "termination" clause. Leaving it out waits forever. A nicer version of the above gives user transactions a few seconds to terminate.

ALTER DATABASE [target] SET SINGLE_USER WITH ROLLBACK AFTER 5

Offline is a good idea if you want to copy database files around, a scenario that can be handy in desktop editions of SQL. Too heavy for this scenario. If offline, this would be preferred. SQL is moving away from sp_dboption.

ALTER DATABASE [target] SET OFFLINE WITH ROLLBACK AFTER 5
like image 29
Precipitous Avatar answered Sep 17 '22 17:09

Precipitous


You can force the database offline and drop connections with:

EXEC sp_dboption N'yourDatabase', N'offline', N'true'

Or you can

ALTER DATABASE [yourDatabase] SET OFFLINE WITH
ROLLBACK AFTER 60 SECONDS

Rollback specifies if anything is executing. After that period they will be rolled back. So it provides some protection.

Sorry I wasn't thinking/reading right. You could bing back online and backup. There was also a post on Stack Overflow on a T-SQL snippet for dropping all connections rather than binging offline first: Hidden Features of SQL Server

like image 139
mattlant Avatar answered Sep 17 '22 17:09

mattlant