declare @aux varchar(100); set @aux = replace(replace(convert(varchar(100), getdate(), 120), '-', ''), ':', ''); select 'BACKUP DATABASE [' + DB + ']' + ' TO DISK = ''C:\_backups\' + @aux + ' - ' + DB + '.bak''' + ' WITH NOFORMAT, NOINIT,' + ' NAME = ''' + DB + '-Full Database Backup'',' + ' SKIP, NOREWIND, NOUNLOAD, STATS = 10;' as BK_DB, 'drop database [' + DB + '];' as DROP_DB from ( select 'dbONE' as DB union select 'dbTWO' union select 'dbTHREE' ) x;
BK_DB DROP_DB ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------ BACKUP DATABASE [dbONE] TO DISK = 'C:\_backups\20171019 180935 - dbONE.bak' WITH NOFORMAT, NOINIT, NAME = 'dbONE-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10; drop database [dbONE]; BACKUP DATABASE [dbTHREE] TO DISK = 'C:\_backups\20171019 180935 - dbTHREE.bak' WITH NOFORMAT, NOINIT, NAME = 'dbTHREE-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10; drop database [dbTHREE]; BACKUP DATABASE [dbTWO] TO DISK = 'C:\_backups\20171019 180935 - dbTWO.bak' WITH NOFORMAT, NOINIT, NAME = 'dbTWO-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10; drop database [dbTWO]; (3 row(s) affected)