在sql server 2012 express中创建数据库副本

时间:2014-03-19 09:46:55

标签: sql-server database sql-server-2012

您好我有哪些用户工作的数据库。 我想创建此数据库的副本以进行测试。

谁能告诉我怎么做? 我尝试了copy a database within SQL Server Express?

的所有内容

但是复制文件并附加它会破坏第一个数据库,并尝试将备份恢复到新数据库时抛出数据库不一样的错误。

有人可以建议我解决方案吗?

@Nadeem_MK建议我创建备份,现在我正在尝试使用此脚本进行恢复:

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

并抛出错误:

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.

正如您所看到的,恢复正在尝试从备份数据库而不是新数据库

更改文件

@Update: 备份脚本:

--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

文件位置:

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 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 个答案:

答案 0 :(得分:2)

您可以使用以下脚本创建备份并将其还原;

--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

然后恢复它;

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

答案 1 :(得分:2)

假设这是原始数据库的路径:

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

和备份的文件名称为

Equipment.bak

确保新数据库的文件名与旧数据库的文件名不同。这就是你得到的错误意味着:

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

说明:不要使用已有文件的文件名!

相反,请给这些新名称。像这样:

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

这基本上是Nadeem_MK的答案,但现在你应该有Equipment.mdfEquipment_log.ldf

,而不是原来的名字Equipment2.mdfEquipment_log.ldf

如果这样做,请同时选择Nadeem_MK的答案,因为这基本上就是他所说的。我还没有足够的声誉评论他的答案。

答案 2 :(得分:0)

简单,执行以下步骤:

  1. 备份主数据库。

  2. 使用您想要进行测试的名称创建新数据库。

  3. 将主数据库的备份还原到新创建的测试数据库。

  4. 完成!!

答案 3 :(得分:0)

注意:以下功能是 在SQL Express 中可用 我已经在这里留下了答案,仅供将来读者使用。

我能想到的最简单的方法是让Management Studio完成所有工作!

右键单击要复制的数据库,然后在任务菜单下查找复制数据库... 选项。

Copy database task

答案 4 :(得分:0)

为什么不编写数据库脚本并运行脚本?