咨询热线:4006-75-4006
售前:9:00-23:30 备案:9:00-18:00 技术:7*24h
SQL Server 批量完整备份 实现方式一:使用游标
执行下面的SQL脚本就可以备份当前数据库实例的所有数据库(除了系统数据库);
-- ============================================= -- Create date: <2015/12/03> -- Description: <批量备份数据库> -- ============================================= DECLARE @FileNameVARchar(200), @CurrentTimeVARchar(50), @DBNameVARchar(100), @SQL VARchar(1000) SET @CurrentTime = CONVERT(char(8),GETDATE(),112) + CAST(DATEPART(hh, GETDATE()) AS VARCHAR) + CAST(DATEPART(mi, GETDATE()) AS VARCHAR) DECLARE CurDBName CURSOR FOR select NAME from Master..SysDatabases where dbid>4 OPEN CurDBName FETCH NEXT FROM CurDBName INTO @DBName WHILE @@FETCH_STATUS = 0 BEGIN --Execute Backup SET @FileName = 'E:DBBackup' + @DBName + '_' + @CurrentTime SET @SQL = 'BACKUP DATABASE ['+ @DBName +'] TO DISK = ''' + @FileName + '.bak' + ''' WITH NOINIT, NOUNLOAD, NAME = N''' + @DBName + '_backup'', NOSKIP, STATS = 10, NOFORMAT' EXEC(@SQL) --Get Next DataBase FETCH NEXT FROM CurDBName INTO @DBName END CLOSE CurDBName DEALLOCATE CurDBName
执行完上面的SQL脚本,会在E:DBBackup的目录下生成似下图的备份文件: