SQL Server 2012 Database Restore

Jason-
Jason- used Ask the Experts™
on
I have a backup of a database called FOXVISUAL from a certain point in time. I want to restore this database on the server but I am changing the name of it to TESTVISUAL. I just want to look into this database and see some particular records and how they were at this point in time. I was always able to do this in SQL Server 2005 by doing a database restore pointing to the .bak file and changing the name of the destination to the new name and renaming the MDF and LDF. This way I would have my test database and my production database running on the same SQL Server. When I do this in 2012, I get a "Exclusive access could not be obtained because the target database is in use." To which the target database is called TESTVISUAL and does not exist. I am not sure how to acheive this.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Nakul VachhrajaniTechnical Architect, Capgemini India

Commented:
After changing the destination  database name, can you check if the restore wizard is trying to restore the DB over the same files as used by the primary database? If yes, you should change the target folder and file names that need to be used by your required destination  TESTVISUAL database.

Author

Commented:
I have the file names set to TESTVISUAL.MDF and TESTVISUAL_1.LDF in the Files section under the restore as field. It should not be trying to write over top the primary database.
Senior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
Do you need to just restore a single backup, or do you need to apply a differential/logs?

For just a restore, don't use the GUI for that, use a RESTORE command.  It's much better control, and easier to repeat later.  You will need the logical file names, which you can get from this command:

RESTORE FILELISTONLY
FROM DISK = '<backup_file_path_and_name>'

If you need to apply logs, you'd have to generate the restore command, you wouldn't want to write that by hand.

Then you're ready for this command:

IF EXISTS(SELECT 1 FROM sys.databases WHERE name = 'TESTVISUAL')
    DROP DATABASE TESTVISUAL;
GO

RESTORE DATABASE TESTVISUAL
FROM DISK = '<backup_file_path_and_name>'
WITH MOVE 'FOXVISUAL' TO '<path_to_file>\TESTVISUAL.MDF',
    MOVE 'FOXVISUAL_LOG' TO '<path_to_file>\TESTVISUAL_1.LDF',
    NORECOVERY
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
Once that runs, apply differential, if needed, and log backup(s) if needed, then do a final recovery on the db:

RESTORE DATABASE TESTVISUAL WITH RECOVERY;

And your db should be good to go!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial