Skip to main content

How to Restore a SQL Server Database from a Backup File

If the restored database has the name '@DatabaseName', you'll need to rename it manually to 'Autocom3_Filial_Movimento_Mensal_2023_03'.


You can do this by right-clicking on the database name in SQL Server Management Studio and selecting Rename, or by using the following SQL command:Restoring a database from a backup file is a common task for database administrators. In this article, we'll walk through the steps to restore a SQL Server 2008 R2 database from a backup file using T-SQL. This is a straightforward process, but it's important to do it correctly to avoid errors and ensure that the data is fully recovered.


Prerequisites

  • SQL Server 2008 R2 installed
  • A backup file of the database (.bkp)

Steps to Restore a Database

1. Define Variables for Backup and Database Names

We need to specify the path to the backup file and the name we want to give to the restored database. 

-- Set the backup file path

DECLARE @BackupFile NVARCHAR(500) = 'C:\autocom3_temp\autocom3_filial_movimento_mensal_2023_03.bkp';

-- Set the database name

DECLARE @DatabaseName NVARCHAR(500) = 'Autocom3_Filial_Movimento_Mensal_2023_03';

2. Restore the Database

Use the RESTORE DATABASE statement to restore the database from the backup file. Here we're assuming that the logical names in the backup match those of the database files on disk. If you're not changing the locations of the database files, you don't need to specify the MOVE option.

-- Restore the database
RESTORE DATABASE @DatabaseName
FROM DISK = @BackupFile
WITH REPLACE, STATS = 10;

or

RESTORE DATABASE @DatabaseName
FROM DISK = @BackupFile

If everything goes well, the database should be successfully restored. However, due to a known issue, the restored database may have the name '@DatabaseName' instead of 'Autocom3_Filial_Movimento_Mensal_2023_03'.

3. Rename the Database (If Necessary)

-- Rename the database
ALTER DATABASE [@DatabaseName] MODIFY NAME = [Autocom3_Filial_Movimento_Mensal_2023_03];

And there you have it! You've successfully restored a SQL Server 2008 R2 database from a backup file.






Comments