0
votes

Hello I have database on which user work. I want to create copy of this database for testing purpose.

Can anyone tell how can this be done? I tried everything from copy a database within SQL Server Express?

But Copying files and attaching it destroys first database and trying to restore backup into new database throws error that database is not the same.

Can anyone suggest me solution?

as @Nadeem_MK suggested I created backup and now I'm trying to restore with this script:

RESTORE DATABASE [Equipment Test]  -- use your new db name
FROM  DISK = N'C:\Backup\ExistingDb.bak'  --Path of backup location
WITH  REPLACE,RECOVERY,  
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\ExistingDb.mdf',  --logical name and physical name 
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\ExistingDb.ldf' --logical name and physical name

and this throws error:

Msg 1834, Level 16, State 1, Line 1
The file 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf' cannot be overwritten.  It is being used by database 'Equipment'.
Msg 3156, Level 16, State 4, Line 1
File 'Equipment' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 1834, Level 16, State 1, Line 1

The file 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf' cannot be overwritten.  It is being used by database 'Equipment'.
Msg 3156, Level 16, State 4, Line 1
File 'Equipment_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf'. Use WITH MOVE to identify a valid location for the file.
Msg 3119, Level 16, State 1, Line 1
Problems were identified while planning for the RESTORE statement. Previous messages provide details.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

As you can see restores is trying to alter files from backup-ed database instead of new database

@Update: BackUp Script:

--To backup Database (Delete the previous backup first)
BACKUP DATABASE Equipment --Database Name
TO DISK = 'C:\Backup\Equipment.bak' WITH INIT --Path of backup location

file locations:

C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment Test.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment Test_log.mdf

Restore function:

RESTORE DATABASE [Equipment Test]  -- use your new db name
FROM  DISK = N'C:\Backup\Equipment.bak'  --Path of backup location
WITH  REPLACE,RECOVERY,  
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment2.mdf',  --logical name and physical name 
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log2.ldf' --logical name and physical name
5
Easiest is properly just to get a backup and then restore under different name.Allan S. Hansen
yeah, sorry, the reporting file name should bear the new db name.. answer amended! should work now!Nadeem_MK

5 Answers

2
votes

You can create a backup and restore it using the following script;

--To backup Database (Delete the previous backup first)
BACKUP DATABASE ExistingDb --Database Name
TO DISK = 'C:\DBBackup\ExistingDb.bak' WITH INIT --Path of backup location

Then restore it;

RESTORE DATABASE NewDbName  -- use your new db name
FROM  DISK = N'C:\DBBackup\ExistingDb.bak'  --Path of backup location
WITH  REPLACE,RECOVERY,  
MOVE N'Reporting' TO N'C:\Databases\MDF\NewDbName.mdf',  --logical name and physical name 
MOVE N'Reporting_log' TO N'C:\Databases\LDF\NewDbName.ldf' --logical name and physical name
2
votes

Assuming this are the paths of the original database:

C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment.mdf
C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log.ldf

and the filename of your backup is called

Equipment.bak

Make sure that the filenames for the new database aren't the same as the old one. That is what the errors you are getting means:

The file 'C:\...' cannot be overwritten.  It is being used by database 'Equipment'.

Explanation: Don't use filenames of files you already have!

Instead, give these a new name. Like this:

RESTORE DATABASE [Equipment Test]  -- use your new db name
FROM  DISK = N'C:\Backup\Equipment.bak'  --Path of backup location
WITH  REPLACE,RECOVERY,  
MOVE N'Equipment Test' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment2.mdf',  --logical name and physical name 
MOVE N'Equipment Test_log' TO N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA\Equipment_log2.ldf' --logical name and physical name

This is basically Nadeem_MK's answer, but instead of your original names Equipment.mdf and Equipment_log.ldf, now you should have Equipment2.mdf and Equipment_log.ldf

If this works, please also opvote Nadeem_MK's answer, as this is basically what he was saying. I just don't have enough reputation yet to comment on his answer.

0
votes

Simple, Do these steps:

  1. Take backup of your primary database.

  2. Create new database with whatever name you want to give it for testing.

  3. Restore backup of your primary database to your newly created testing database.

Done!!

0
votes

Note: The functionality below is not available in SQL Express I have left the answer here for completeness for future readers only.

The simplest method I can think of is to let Management Studio do all the work!

Right-click the database you wish to copy and look for the Copy Database... option under the Tasks menu.

Copy database task

0
votes

Why don't you script the database and run the script?