Solved

script restore 100 db automatically

Posted on 2014-09-28
2
93 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 50

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

710 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