Solved

script restore 100 db automatically

Posted on 2014-09-28
2
88 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 24

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 45

Accepted Solution

by:
Vitor Montalvão earned 500 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

746 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now