DECLARE @Dir VARCHAR(200) SELECT @Dir = 'F:\AppData_Bak\' DECLARE @Name VARCHAR(100) DECLARE List CURSOR FOR SELECT name FROM master..sysdatabases WHERE STATUS & 512 != 512 OPEN LIST FETCH List INTO @Name WHILE @@FETCH_STATUS = 0 BEGIN PRINT 'EXECUTE master.dbo.xp_create_subdir N'''+@dir+''+ @name + '''' print '' DECLARE @FileName VARCHAR(500) DECLARE @BackName VARCHAR(50) SELECT @BackName = @Name + '_backup_'+ REPLACE(REPLACE(REPLACE( CONVERT(VARCHAR,GETDATE(),120),'-',''),':',''),' ','') SELECT @FileName = @Dir + @Name + '\' + @BackName+ '.bak' DECLARE @backupSql VARCHAR(1000) SELECT @backupSql = 'BACKUP DATABASE ['+@name+'] TO DISK = N'''+@fileName+''' WITH NOFORMAT, NOINIT, NAME = N'''+@BackName+''', SKIP, REWIND, NOUNLOAD, STATS = 10' print @backupSql print '' print '' FETCH List INTO @Name END CLOSE LIST DEALLOCATE LIST