How to restore a database to an existing database in use

Source: Internet
Author: User

USE master -- (Can't sit in the database whilst its being restored !)
GO

Alter database MyDatabase SET SINGLE_USER WITH ROLLBACK IMMEDIATE
GO

-- Restore Full Backup
Restore database MyDatabase
From disk = 'x: \ MSSQL \ BACKUP \ MyBackupFilename_Full.BAK'
WITH
REPLACE,
NORECOVERY, -- Use if more T/Logs to recover
-- RECOVERY, -- Use if NO more T/Logs to recover
STATS = 10, -- Show progress (every 10%)
MOVE 'mydatabase _ data' TO 'x: \ MSSQL \ Data \ MyDatabase. mdf ',
MOVE 'mydatabase _ log' TO 'x: \ MSSQL \ DATA \ MyDatabase. ldf'
GO

-- Optional restore Differential Backup
Restore database MyDatabase
From disk = 'x: \ MSSQL \ BACKUP \ MyDatabase_Diff.BAK'
WITH
-- RECOVERY -- Use if NO more file to recover
NORECOVERY -- Use if there are T/Logs to recover
GO

-- Optional restore Transaction Log Backup
Restore database MyDatabase
From disk = 'x: \ MSSQL \ BACKUP \ MyDatabase_yyyymmdd_hhmm_Trans.BAK'
WITH
-- RECOVERY -- Use if NO more T/Logs to recover
NORECOVERY -- Use if more T/Logs to recover
GO

-- Set the database ready for use (after all backups have been restored)
Restore database MyDatabase RECOVERY
GO

-- Rename logical names (only needed if restoring from a backup for a Different database ):
Alter database MyDatabase
Modify file (NAME = 'database database _ data', NEWNAME = 'mydatabase _ data ')
GO
Alter database MyDatabase
Modify file (NAME = 'database database _ log', NEWNAME = 'mydatabase _ log ')
GO

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.