我需要进行干净的数据库模板备份,然后使用另一个名称生成一个新的数据库,但具有备份的内容。备份脚本是:
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"
它适用于相同的数据库名称。但是,如果我备份 DB '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'.
....