复制数据库的最佳方法是什么?[英] What is the best way to copy a database?

本文是小编为大家收集整理的关于复制数据库的最佳方法是什么?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

当我想制作数据库的副本时,我总是创建一个新的空数据库,然后将现有数据库的备份还原到其中.但是,我想知道这是否真的是最小的错误,最不复杂的,也是最有效的方法?

推荐答案

备份和还原是我所知道的最直接的方式.您必须在服务器之间谨慎,因为安全凭据不带有还原的数据库.

其他推荐答案

可以跳过创建空数据库的步骤.您可以创建新数据库作为还原过程的一部分.

这实际上是我知道克隆数据库的最简单和最佳方法.您可以通过拼写备份和还原过程来消除错误,而不是通过SQL Server Management Studio

运行错误.

您还可以探索其他两个选项:

  1. 分离数据库,复制.mdf文件并重新签约.
  2. 使用SQL Server Integration Services(SSIS)将所有对象复制到

我建议坚持备份,并在必要时恢复和自动化.

其他推荐答案

这是我过去使用过的动态SQL脚本.它可以进一步修改,但它将为您提供基础知识.我更喜欢脚本脚本以避免使用管理工作室可以犯的错误:

Declare @OldDB varchar(100)
Declare @NewDB varchar(100)
Declare @vchBackupPath varchar(255)
Declare @query varchar(8000)


/*Test code to implement 
Select @OldDB = 'Pubs'
Select @NewDB = 'Pubs2'
Select @vchBackupPath = '\\dbserver\C$\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\pubs.bak'
*/

SET NOCOUNT ON;

Select @query = 'Create Database ' + @NewDB
exec(@query)

Select @query = '
Declare @vBAKPath varchar(256)
declare @oldMDFName varchar(100)
declare @oldLDFName varchar(100)
declare @newMDFPath varchar(100)
declare @newLDFPath varchar(100)
declare @restQuery varchar(800)

select @vBAKPath = ''' + @vchBackupPath + '''
select @oldLDFName = name from ' + @OldDB +'.dbo.sysfiles where filename like ''%.ldf%''
select @oldMDFName = name from  ' + @OldDB +'.dbo.sysfiles where filename like ''%.mdf%''
select @newMDFPath = physical_name from ' + @NewDB +'.sys.database_files where type_desc = ''ROWS''
select @newLDFPath = physical_name from ' + @NewDB +'.sys.database_files where type_desc = ''LOG''

select @restQuery = ''RESTORE DATABASE ' + @NewDB + 
' FROM DISK = N'' + '''''''' + @vBAKpath + '''''''' + 
'' WITH MOVE N'' + '''''''' + @oldMDFName + '''''''' +  
'' TO N'' + '''''''' + @newMDFPath + '''''''' +  
'', MOVE N'' + '''''''' + @oldLDFName + '''''''' +  
'' TO N'' + '''''''' + @newLDFPath + '''''''' +  
'', NOUNLOAD, REPLACE, STATS = 10''

exec(@restQuery)
--print @restQuery'


exec(@query)





本文地址:https://www.itbaoku.cn/post/2792399.html

问题描述

When I want to make a copy of a database, I always create a new empty database, and then restore a backup of the existing database into it. However, I'm wondering if this is really the least error-prone, least complicated, and most efficient way to do this?

推荐答案

Backup and Restore is the most straight-forward way I know. You have to be careful between servers as security credentials don't come with the restored database.

其他推荐答案

It is possible to skip the step of creating the empty database. You can create the new database as part of the restore process.

This is actually the easiest and best way I know of to clone a database. You can eliminate errors by scripting the backup and restore process rather than running it through the SQL Server Management Studio

There are two other options you could explore:

  1. Detach the database, copy the .mdf file and re-attach.
  2. Use SQL Server Integration Services (SSIS) to copy all the objects over

I suggest sticking with backup and restore and automating if necessary.

其他推荐答案

Here's a dynamic sql script I've used in the past. It can be further modified but it will give you the basics. I prefer scripting it to avoid the mistakes you can make using the Management Studio:

Declare @OldDB varchar(100)
Declare @NewDB varchar(100)
Declare @vchBackupPath varchar(255)
Declare @query varchar(8000)


/*Test code to implement 
Select @OldDB = 'Pubs'
Select @NewDB = 'Pubs2'
Select @vchBackupPath = '\\dbserver\C$\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\pubs.bak'
*/

SET NOCOUNT ON;

Select @query = 'Create Database ' + @NewDB
exec(@query)

Select @query = '
Declare @vBAKPath varchar(256)
declare @oldMDFName varchar(100)
declare @oldLDFName varchar(100)
declare @newMDFPath varchar(100)
declare @newLDFPath varchar(100)
declare @restQuery varchar(800)

select @vBAKPath = ''' + @vchBackupPath + '''
select @oldLDFName = name from ' + @OldDB +'.dbo.sysfiles where filename like ''%.ldf%''
select @oldMDFName = name from  ' + @OldDB +'.dbo.sysfiles where filename like ''%.mdf%''
select @newMDFPath = physical_name from ' + @NewDB +'.sys.database_files where type_desc = ''ROWS''
select @newLDFPath = physical_name from ' + @NewDB +'.sys.database_files where type_desc = ''LOG''

select @restQuery = ''RESTORE DATABASE ' + @NewDB + 
' FROM DISK = N'' + '''''''' + @vBAKpath + '''''''' + 
'' WITH MOVE N'' + '''''''' + @oldMDFName + '''''''' +  
'' TO N'' + '''''''' + @newMDFPath + '''''''' +  
'', MOVE N'' + '''''''' + @oldLDFName + '''''''' +  
'' TO N'' + '''''''' + @newLDFPath + '''''''' +  
'', NOUNLOAD, REPLACE, STATS = 10''

exec(@restQuery)
--print @restQuery'


exec(@query)