|
| 1 | +/* |
| 2 | + Get RESTORE statement for databases Keeping pre-existing FILE STRUCTURE |
| 3 | + Run on Target (Destination) |
| 4 | +*/ |
| 5 | +SET NOCOUNT ON; |
| 6 | + |
| 7 | +declare @p_dbName varchar(100); |
| 8 | +declare @sqlRestoreText varchar(max); |
| 9 | +declare @counter int = 1; |
| 10 | +declare @total_counts int; |
| 11 | + |
| 12 | +IF OBJECT_ID('tempdb..#Dbs') IS NOT NULL |
| 13 | + DROP TABLE #Dbs; |
| 14 | +SELECT ROW_NUMBER()OVER(ORDER BY dbName) as ID, dbName |
| 15 | +INTO #Dbs |
| 16 | +FROM (VALUES |
| 17 | + ('AMG_AVG'),('AMG_Extra'),('AMG_Music'),('AMG_MusicMore'),('Babel'),('DSG_EU'),('Facebook'), |
| 18 | + ('Mosaic'),('MuzeUK'),('MuzeUS'),('MuzeVideo'),('Prism'),('RGS'),('RCM_rovicore_20130710_NoMusic1a_en-US'), |
| 19 | + ('Sky'),('Staging'),('Staging2'),('Twitter'),('TVGolConfigs'),('UKVideo') |
| 20 | + ) Databases(dbName); |
| 21 | +set @p_dbName = 'Cosmo'; |
| 22 | + |
| 23 | +select @total_counts = count(*) from #Dbs; |
| 24 | + |
| 25 | +while @counter <= @total_counts |
| 26 | +BEGIN |
| 27 | + SELECT @p_dbName = dbName FROM #Dbs d WHERE d.ID = @counter; |
| 28 | + |
| 29 | + set @sqlRestoreText = ' |
| 30 | + RESTORE DATABASE '+QUOTENAME(@p_dbName)+' FROM DISK = N''Your-Backup-File-Path-in-Here'' |
| 31 | + WITH RECOVERY |
| 32 | + ,STATS = 3 |
| 33 | + ,REPLACE |
| 34 | + '; |
| 35 | + |
| 36 | + select @sqlRestoreText += --name, physical_name, |
| 37 | + ' ,MOVE N'''+name+''' TO N'''+physical_name+''' |
| 38 | + ' |
| 39 | + from sys.master_files as mf |
| 40 | + where DB_NAME(mf.database_id) IN ('AMG_AVG','AMG_Extra','AMG_Music','AMG_MusicMore','Babel','DSG_EU','Facebook','Mosaic', |
| 41 | + 'MuzeUK','MuzeUS','MuzeVideo','Prism','RGS','RCM_rovicore_20130710_NoMusic1a_en-US','Sky','Staging','Staging2','Twitter', |
| 42 | + 'TVGolConfigs','UKVideo'); |
| 43 | + |
| 44 | + SET @sqlRestoreText += ' |
| 45 | + GO' |
| 46 | + |
| 47 | + PRINT @sqlRestoreText; |
| 48 | + |
| 49 | + SET @counter += 1; |
| 50 | +END |
0 commit comments