Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore database SQL server query

Tags:

sql

sql-server

I want a way to write my own query to restore the database. The database to restore needs to have all the settings to delete the current user and re-map the same user. The reason for that is because when the database is restored, the user will not have the right settings to use the database and will have to re assign the user the privileges.

like image 694
MohammedT Avatar asked Aug 15 '13 20:08

MohammedT


People also ask

How do I restore a new SQL Server database?

Connect to the appropriate instance of the SQL Server Database Engine, and then in Object Explorer, select the server name to expand the server tree. Right-click Databases, and then select Restore Database. The Restore Database dialog box opens. Select the database to restore from the drop-down list.

How do I restore a .BAK file in SQL Server?

Right-click the Databases node in Object Explorer and select Restore Database.... Select Device:, and then select the ellipses (...) to locate your backup file. Select Add and navigate to where your . bak file is located.

What is restore command in SQL?

Use the RESTORE command with the SQL Backup Pro -SQL parameter to restore a SQL Backup backup using the command line or extended stored procedure. Syntax provides the grammar for the RESTORE command.


1 Answers

Check this out:-

Step 1: Retrive the Logical file name of the database from backup.

RESTORE FILELISTONLY
FROM DISK = 'D:BackUpYourBaackUpFile.bak'

GO

Step 2: Use the values in the LogicalName Column in following Step. ----Make Database to single user Mode

ALTER DATABASE YourDB
SET SINGLE_USER WITH
ROLLBACK IMMEDIATE

----Restore Database

RESTORE DATABASE YourDB
FROM DISK = 'D:BackUpYourBaackUpFile.bak'
WITH MOVE 'YourMDFLogicalName' TO 'D:DataYourMDFFile.mdf',
MOVE 'YourLDFLogicalName' TO 'D:DataYourLDFFile.ldf'

/If there is no error in statement before database will be in multiuser mode. If error occurs please execute following command it will convert database in multi user./

ALTER DATABASE YourDB SET MULTI_USER
GO
like image 114
Rahul Tripathi Avatar answered Sep 23 '22 12:09

Rahul Tripathi