Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

script restore 100 db automatically

Posted on 2014-09-28
2
Medium Priority
?
96 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 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

715 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