query to restore database in sql server
This topic describes how to restore a backup from a device in SQL Server 2017 by using SQL Server Management Studio or Transact-SQL.
In This Topic
If the database being restored does not exist, the user must have CREATE DATABASE permissions to be able to execute RESTORE. If the database exists, RESTORE permissions default to members of the sysadmin and dbcreator fixed server roles and the owner (dbo) of the database (for the FROM DATABASE_SNAPSHOT option, the database always exists).
RESTORE permissions are given to roles in which membership information is always readily available to the server. Because fixed database role membership can be checked only when the database is accessible and undamaged, which is not always the case when RESTORE is executed, members of the db_owner fixed database role do not have RESTORE permissions.
To restore a backup from a device
- After connecting to the appropriate instance of the Microsoft SQL Server Database Engine, in Object Explorer, click the server name to expand the server tree.
- Expand Databases, and, depending on the database, either select a user database or expand System Databases and select a system database.
- Right-click the database, point to Tasks, and then click Restore.
- Click the type of restore operation you want (Database, Files and Filegroups, or Transaction Log). This opens the corresponding restore dialog box.
- On the General page, in the Restore source section, click From device.
- Click the browse button for the From device text box, which opens the Specify Backup dialog box.
- In the Backup media text box, select Backup Device, and click the Add button to open the Select Backup Device dialog box.
- In the Backup device text box, select the device you want to use for the restore operation.
- Connect to the Database Engine.
- From the Standard bar, click New Query.
- In the RESTORE statement, specify a logical or physical backup device to use for the backup operation. This example restores from a disk file that has the physical name Z:\SQLServerBackups\AdventureWorks2012.bak.