Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

script restore 100 db automatically

Posted on 2014-09-28
2
Medium Priority
?
98 Views
Last Modified: 2014-09-29
Hello,

I search how to automaticaly restore 100 db with the same structure from db_001 to db_100

RESTORE DATABASE [db_001] FROM  DISK = N'\\share\backupsql$\db_001_20140928_031842.BAK' WITH  FILE = 1,  MOVE N'db_001_Data' TO N'd:\MSSQL\db_001_Data.mdf',  MOVE N'db_001_Log' TO N'd:\MSSQL\db_001_Log.ldf',  NOUNLOAD,  STATS = 5

GO

Thanks
0
Comment
Question by:bibi92
2 Comments
 
LVL 25

Expert Comment

by:Mohammed Khawaja
ID: 40348927
Best way to do is to create a database first and then restore the backup.  Below is a sample script:

USE master
GO
CREATE DATABASE Sales
ON
( NAME = Sales_dat,
   FILENAME = 'd:\MSSQL\db_100.mdf',
   SIZE = 20,
   FILEGROWTH = 10 )
LOG ON
( NAME = 'Sales_log',
   FILENAME = 'd:\MSSQL\db_100.ldf',
   SIZE = 20MB,
   FILEGROWTH = 10MB )
GO

RESTORE DATABASE [db_001] FROM  DISK = N'\\share\backupsql$\db_001_20140928_031842.BAK'
Go
0
 
LVL 52

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 40349549
I didn't test it but I think this script it's maybe what you are looking for:
DECLARE @NumOfRestores INT = 100
DECLARE @CurRestore INT = 1
DECLARE @RestoreCmd VARCHAR(4000)
DECLARE @NumFormat VARCHAR(3)

WHILE @CurRestore <= @NumOfRestores
BEGIN
	SET @NumFormat=RIGHT('000'+ CONVERT(VARCHAR,@CurRestore),3)
	SET @RestoreCmd = 'RESTORE DATABASE db_' + @NumFormat +
						' FROM  DISK = N''\\share\backupsql$\db_001_20140928_031842.BAK'' WITH  FILE = 1, MOVE N''db_' + @NumFormat +
						'_Data'' TO N''d:\MSSQL\db_' + @NumFormat + '_Data.mdf'', MOVE N''db_' + @NumFormat +
						'_Log'' TO N''d:\MSSQL\db_' + @NumFormat + '_Log.ldf'',  NOUNLOAD,  STATS = 5'
	EXEC @RestoreCmd
	SET @CurRestore=@CurRestore+1
END

Open in new window

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question