One task sometimes needing to be accomplished is that of restoring an older version of an existing database to the same server. It may be necessary for auditing purposes, restoring or recovering older data or even for reporting. In most cases you wouldn't use your production server, but it could easily be the case for test or development servers.
The process for restoring a database works the same as any other restore process, with one exception. Problems are unlikely during the restore, but the values of the files' physical locations are embedded in the backup copy and will need to be changed.
To investigate this we can run the following query in the master database. This will give us the results of all our databases and the location of the physical files.
SELECT sd.name,
saf.name,
saf.filename
FROM master.dbo.sysaltfiles saf
INNER JOIN master.dbo.sysdatabases sd ON sd.dbid = saf.dbid
Listing 1
| Database |
Logical File |
Physical File |
| master |
master |
C:\Program Files\Microsoft SQL Server\MSSQL\data\master.mdf |
| master |
mastlog |
C:\Program Files\Microsoft SQL Server\MSSQL\data\mastlog.ldf |
| tempdb |
tempdev |
C:\Program Files\Microsoft SQL Server\MSSQL\data\tempdb.mdf |
| tempdb |
templog |
C:\Program Files\Microsoft SQL Server\MSSQL\data\templog.ldf |
| model |
modeldev |
C:\Program Files\Microsoft SQL Server\MSSQL\data\model.mdf |
| model |
modellog |
C:\Program Files\Microsoft SQL Server\MSSQL\data\modellog.ldf |
| msdb |
MSDBData |
C:\Program Files\Microsoft SQL Server\MSSQL\data\msdbdata.mdf |
| msdb |
MSDBLog |
C:\Program Files\Microsoft SQL Server\MSSQL\data\msdblog.ldf |
| pubs |
pubs |
C:\Program Files\Microsoft SQL Server\MSSQL\data\pubs.mdf |
| pubs |
pubs_log |
C:\Program Files\Microsoft SQL Server\MSSQL\data\pubs_log.ldf |
| Northwind |
Northwind |
C:\Program Files\Microsoft SQL Server\MSSQL\Data\Northwind.mdf |
| Northwind |
Northwind_log |
C:\Program Files\Microsoft SQL Server\MSSQL\Data\Northwind_log.ldf |
RESTORE DATABASE Northwind2
FROM DISK='C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Northwind.BAK'
We get the following error message:
Server: Msg 1834, Level 16, State 1, Line 1
The file 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Northwind.mdf' cannot be overwritten.
It is being used by database 'Northwind'.
Server: Msg 3156, Level 16, State 1, Line 1 File 'Northwind' cannot be restored to
'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Northwind.mdf'.
Use WITH MOVE to identify a valid location for the file.
Server: Msg 1834, Level 16, State 1, Line 1
The file 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Northwind_log.ldf' cannot be overwritten.
It is being used by database 'Northwind'.
Server: Msg 3156, Level 16, State 1, Line 1
File 'Northwind_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Northwind_log.ldf'.
Use WITH MOVE to identify a valid location for the file.
Server: Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.
To get around this we need to use the WITH MOVE option such as the following.
RESTORE DATABASE Northwind2
FROM DISK='C:\Program Files\Microsoft SQL Server\MSSQL\BACKUP\Northwind.BAK'
WITH MOVE 'Northwind'
TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Northwind2.mdf',
MOVE 'Northwind_log'
TO 'C:\Program Files\Microsoft SQL Server\MSSQL\Data\Northwind2_log.mdf'
This command uses the same RESTORE command as the first attempt, except we are using the WITH MOVE for both Logical files in the backup. The logical file names that are being used are the names that were pulled from the query results in listing 2.
Summary
As you can see there is not much to restoring another copy of an existing database on the same server. The only thing you really need to do is make sure you rename the physical files. With Enterprise Manager the renaming of the files is automatically done when you click on the Options tab, but with T-SQL you need to use the WITH MOVE option to specify the new file names.
About the author: Greg Robidoux is the president and founder of Edgewood Solutions LLC, a technology services company delivering professional services and product solutions for Microsoft SQL Server. He has authored numerous articles and has delivered presentations at regional SQL Server users' groups and national SQL Server events. Robidoux, who also serves as the SearchSQLServer.com Backup and Recovery expert, welcomes your questions.