首先,我对这个主要内容了解不多,我很久以前从网上的某个地方获取此脚本,并通过Windows中的任务计划程序将其用于自动备份,但由于某些原因在最近实现它只备份一些DBase,我注意到任何以数字开头的数据库名称都不会备份,所有其他数据库都会备份。
有人可以看看这个并给我一个帮助,在此先感谢您的帮助。
这是我正在使用的脚本:
SET QUOTED_IDENTIFIER OFF
USE master
GO
SET NOCOUNT ON
DECLARE @dayofweek varchar(20)
SELECT
@dayofweek = CASE datepart(dw, getdate())
WHEN 1 THEN 'Domingo'
WHEN 2 THEN 'Segunda'
WHEN 3 THEN 'Terca'
WHEN 4 THEN 'Quarta'
WHEN 5 THEN 'Quinta'
WHEN 6 THEN 'Sexta'
WHEN 7 THEN 'Sabado'
END
DECLARE @OutputPath varchar(500)
DECLARE @DatabaseBackupFile varchar(500)
DECLARE @FolderName varchar(25)
DECLARE @DatabaseName varchar(25)
DECLARE @strSQL varchar(2000)
DECLARE @hostname varchar(255)
SET @hostname = (select replace(convert(varchar(255),serverproperty('SERVERNAME')), '\','_'))
SET @OutputPath = 'B:\BACKUPS_SQL'
DECLARE cur_Backup CURSOR FOR
select name from sysdatabases where name != 'master' and name != 'model' and name != 'msdb' and name != 'temdb'
OPEN cur_Backup
-- Fetch the db names from Cursor
FETCH NEXT FROM cur_Backup INTO @DatabaseName
WHILE @@FETCH_STATUS = 0
BEGIN
SET @DatabaseBackupFile = @OutputPath + '\' + @hostname + ' - ' + @DatabaseName + ' - ' + @dayofweek + '.bak'
print @DatabaseBackupFile
SET @strSQL = 'BACKUP DATABASE '+@DatabaseName+' TO DISK = "'+ @DatabaseBackupFile+ '" WITH RETAINDAYS = 7, NOFORMAT, INIT, NAME = N''Full Database Backup'', SKIP, NOREWIND, NOUNLOAD, STATS = 10'
PRINT @strSQL
EXEC (@strSQL)
FETCH NEXT FROM cur_Backup INTO @DatabaseName
END
-- Distroy Cursor
CLOSE cur_Backup
DEALLOCATE cur_Backup
SET NOCOUNT OFF
我在日志文件中得到这个错误,因为每个DBase都以一个数字开头:
B:\BACKUPS_SQL\WINRT02_SAGE - 234RH - Quarta.bak
BACKUP DATABASE 234RH TO DISK = "B:\BACKUPS_SQL\WINRT02_SAGE - 234RH - Quarta.bak" WITH RETAINDAYS = 7, NOFORMAT, INIT, NAME = N'Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
Msg 102, Level 15, State 1, Server WINRT02\SAGE, Line 1
Incorrect syntax near '234'.