backup and restore database sql server 2017 code example

Example: sql server restore database

--Step 1: Check the logical file names with the help of the following command:

  RESTORE FILELISTONLY 
  FROM DISK = 'E:\DBBackups\mydb.bak'

--Step 2: Use the logical names you get from the above query in the below query:

  RESTORE DATABASE [mydb_new]
  FILE = N'<MDFLogicalName>' 
  FROM DISK = N'E:\DBBackups\mydb.bak'
  WITH FILE = 1
	  ,NOUNLOAD
	  ,STATS = 10
	  ,MOVE N'<MDFLogicalname>' TO N'E:\DBBackups\mydb_new.mdf'
	  ,MOVE N'<LDFLogicalName>' TO N'E:\DBBackups\mydb_new_0.ldf'

Tags:

Sql Example