How can I clone an SQL Server database on the same server in SQL Server 2008 Express?
Install Microsoft SQL Management Studio, which you can download for free from Microsoft's website:
Version 2008
Microsoft SQL Management Studio 2008 is part of SQL Server 2008 Express with Advanced Services
Version 2012
Click download button and check
ENU\x64\SQLManagementStudio_x64_ENU.exe
Version 2014
Click download button and check MgmtStudio
64BIT\SQLManagementStudio_x64_ENU.exe
Open Microsoft SQL Management Studio.
- Backup original database to .BAK file (db -> Task -> Backup).
- Create empty database with new name (clone). Note comments below as this is optional.
- Click to clone database and open restore dialog (see image)
- Select Device and add the backup file from step 3.
- Change destination to test database
- Change location of database files, it must be different from the original. You can type directly into text box, just add postfix. (NOTE: Order is important. Select checkbox, then change the filenames.)
- Check WITH REPLACE and WITH KEEP_REPLICATION
Right-click the database to clone, click Tasks
, click Copy Database...
. Follow the wizard and you're done.
You could try to detach the database, copy the files to new names at a command prompt, then attach both DBs.
In SQL:
USE master;
GO
EXEC sp_detach_db
@dbname = N'OriginalDB';
GO
At Command prompt (I've simplified the file paths for the sake of this example):
copy c:\OriginalDB.mdf c:\NewDB.mdf
copy c:\OriginalDB.ldf c:\NewDB.ldf
In SQL again:
USE master;
GO
CREATE DATABASE OriginalDB
ON (FILENAME = 'C:\OriginalDB.mdf'),
(FILENAME = 'C:\OriginalDB.ldf')
FOR ATTACH;
GO
CREATE DATABASE NewDB
ON (FILENAME = 'C:\NewDB.mdf'),
(FILENAME = 'C:\NewDB.ldf')
FOR ATTACH;
GO