gpt4 book ai didi

sql-server - 将 MSSQL 数据库备份还原到新数据库

转载 作者:行者123 更新时间:2023-12-02 22:05:12 25 4
gpt4 key购买 nike

我需要做一个干净的数据库模板备份,然后用另一个名字生成一个新的数据库,但有备份的内容。备份脚本是:

sqlcmd -S %DB_HOST% -Q "BACKUP DATABASE %DB_NAME% TO DISK = '%BACKUP_FILE%'"

恢复脚本是:

sqlcmd -S %DB_HOST% -Q "RESTORE DATABASE %DB_NAME% FROM DISK = '%BACKUP_FILE%' WITH REPLACE"

并且它在相同的数据库名称上工作。但是,如果我备份数据库“ORIGINAL_DB_NAME”,然后将其恢复到“NEW_DB_NAME”,则会引发异常:

Msg 1834, Level 16, State 1, Server <HOST>, Line 1
The file '.....\MSSQL\Data\ORIGINAL_DB_NAME.mdf' cannot be overwritten. It is being used by database 'ORIGINAL_DB_NAME'.
....

最佳答案

您需要为数据/日志文件指定一个新位置。

参见 this MSDN article , 例 D

RESTORE DATABASE AdventureWorks2012
FROM AdventureWorksBackups
WITH NORECOVERY,
MOVE 'AdventureWorks2012_Data' TO
'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\NewAdvWorks.mdf',
MOVE 'AdventureWorks2012_Log'
TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Data\NewAdvWorks.ldf';
RESTORE LOG AdventureWorks2012
FROM AdventureWorksBackups
WITH RECOVERY;

关于sql-server - 将 MSSQL 数据库备份还原到新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16236915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com