본문 바로가기
RDB/MSSQL

[MSSQL] 이미 존재하는 DB 복원 실패

by kigo23 2024. 2. 5.
반응형

The backup set holds a backup of a database other than the existing {DATABASE NAME} database.
RESTORE DATABASE is terminating abnormally. (Microsoft SQL Server, Error: 3154)

 

또는

 

Msg 1834, Level 16, State 1, Line 1
The file {FILE_PATH} cannot be overwritten.  It is being used by database {DATABASE NAME}.
Msg 3156, Level 16, State 4, Line 1
File {NEW_DATABASE NAME} cannot be restored to {mdf_file_path}. Use WITH MOVE to identify a valid location for the file.

 

기존 DATABASE가 있는 상태에서 같은 DATABASE를 다른 이름으로 복원시도 할 때 위의 에러코드를 볼 수 있습니다. 해당 오류는 기존 사용중인 mdf, ldf 파일을 덮어쓰려고 하기 때문에 발생하는 오류입니다. 다음 쿼리를 사용하여 복원을 실행하여 해결합니다.

RESTORE DATABASE {NEW DATABASE NAME}
FROM DISK = {FILE_PATH}
WITH REPLACE,
MOVE {data} TO 'D:\DB\data\{NEW NAME}.mdf',
MOVE {log} TO 'D:\DB\log\{NEW NAME}.ldf';