I'm attempting to merge two backup scripts together and can't quite seem to get my while loop in the correct place. The script should create striped backups for each of the databases in the cursor. Each year a new database is added to the cursor. The output for the 1st database is fine, it's when it goes to the next that the loop for the file count isn't correct. The script is below. Any help would be appreciated.[code="sql"]SET QUOTED_IDENTIFIER OFFdeclare @command varchar(max), @dbname varchar(30), @StripeCnt varchar(3), @backuppath varchar(100), @prename varchar(17), @fileext char(4), @StripeCounter varchar(3) SELECT @StripeCounter = @StripeCnt SELECT @backuppath = '\\backuppath\' SELECT @fileext = '.bak' SELECT @StripeCounter = 4 SELECT @prename = substring(convert(char(19), CURRENT_TIMESTAMP, 120),1,10)+ '-' + substring(convert(char(19), CURRENT_TIMESTAMP, 120),12,2)+ substring(convert(char(19), CURRENT_TIMESTAMP, 120),15,2)+ substring(convert(char(19), CURRENT_TIMESTAMP, 120),18,2)SET NOCOUNT ON DECLARE databasecursor CURSOR FOR SELECT [name] FROM master.sys.databases with (nolock) where [name] in ('db1','db2','db3')OPEN databasecursor FETCH NEXT FROM databasecursor INTO @dbname WHILE (@@fetch_status = 0)BEGIN SELECT @command = 'BACKUP DATABASE '+ quotename(@dbname) + ' TO DISK = '+ +@backuppath+@dbname+'_Full_Monthly_'+@prename+'_'+@StripeCounter+@fileext+'' SELECT @StripeCounter = @StripeCounter - 1 WHILE @StripeCounter > 0 BEGIN SELECT @command = @command + CHAR(10) + ', DISK = ' + @backuppath+@dbname+'_Full_Monthly_'+@prename+'_'+@StripeCounter+@fileext +''-- print @command SELECT @StripeCounter = @StripeCounter - 1 END select @command = @command + ' with compression, stats, buffercount = 2200, maxtransfersize = 4194304' print @command --EXEC (@command) FETCH NEXT FROM databasecursor INTO @dbnameEND godeallocate databasecursorSET ANSI_NULLS OFFSET QUOTED_IDENTIFIER ON[/code]
↧