USE MASTER ALTER DATABASE [MyDB01] SET SINGLE_USER WITH ROLLBACK IMMEDIATE RESTORE DATABASE [MyDB01] FROM DISK = 'C:\DatabaseBackup\MyDB01_20200101.bak' WITH REPLACE ALTER DATABASE [MyDB01] SET MULTI_USER ALTER DATABASE [MyDB01] SET RECOVERY SIMPLE WITH NO_WAIT
USE MASTER -- Database Backup File Info RESTORE FILELISTONLY FROM DISK = 'G:\SQLBackup\MyDB01_FULL_20200707.bak' ALTER DATABASE [MyDB01] SET SINGLE_USER WITH ROLLBACK IMMEDIATE RESTORE DATABASE [MyDB01] FROM DISK = 'G:\SQLBackup\MyDB01_FULL_20200707.bak' WITH FILE = 1, REPLACE, maxtransfersize = 4194302, buffercount = 96, stats = 5, MOVE ' + @db +' TO 'G:\MSSQL\MyDB01\Data\MyDB01.mdf', MOVE 'MyDB01_DATA' TO 'G:\MSSQL\MyDB01\Data\MyDB01_DATA.ndf', MOVE 'MyDB01_INDEXES' TO 'G:\MSSQL\MyDB01\Data\MyDB01_INDEXES.ndf', MOVE 'MyDB01_LOG' TO 'G:\MSSQL\MyDB01\Log\MyDB01_LOG.ldf' ALTER DATABASE [MyDB01] SET MULTI_USER ALTER DATABASE [MyDB01] SET RECOVERY SIMPLE WITH NO_WAIT
REPLACE Option Impact REPLACE should be used rarely and only after careful consideration. Restore normally prevents accidentally overwriting a database with a different database. If the database specified in a RESTORE statement already exists on the current server and the specified database family GUID differs from the database family GUID recorded in the backup set, the database is not restored. This is an important safeguard.
The REPLACE option overrides several important safety checks that restore normally performs. The overridden checks are as follows:
Restoring over an existing database with a backup taken of another database.
With the REPLACE option, restore allows you to overwrite an existing database with whatever database is in the backup set, even if the specified database name differs from the database name recorded in the backup set. This can result in accidentally overwriting a database by a different database.
Restoring over a database using the full or bulk-logged recovery model where a tail-log backup has not been taken and the STOPAT option is not used.
With the REPLACE option, you can lose committed work, because the log written most recently has not been backed up.