日期:2014-05-18  浏览次数:20549 次

T-SQL备份数据库报错
SQL code

USE test
DECLARE  @DBName      VARCHAR(100) , @DBName2     VARCHAR(100)
DECLARE  @ym          VARCHAR(6) ,   @ym2         VARCHAR(6)
DECLARE  @msg         VARCHAR(100) , @msg2        VARCHAR(100)
DECLARE  @BackupFile  VARCHAR(100) , @BackupFile2 VARCHAR(100)
DECLARE  @BackName    VARCHAR(100) , @BackName2   VARCHAR(100)
DECLARE  @Stats       VARCHAR(2)

SET      @DBName2     = N'AdventureWorks'
SET      @ym2         = Convert(char(6),getdate() ,112)
SET      @ymd2        = Convert(char(8),getdate() ,112)
SET      @msg2        = N'''验证失败。找不到数据库' + @DBName2 + '的备份信息。'''
SET      @BackupFile2 = N'''E:\FULL\' + @DBName2 + '_' + 'full' + '_' + @ym2 + '.bak'''
SET      @BackName2   = N'''' + @DBName2 + '_' + 'full' + '_' + ''''

SET @DBName      = @DBName2 
SET @ym          = @ym2 
SET @ymd         = @ymd2 
SET @msg         = @msg2 
SET @BackupFile  = @BackupFile2 
SET @BackName    = @BackName2 
SET @Stats       = 10

EXECUTE ('BACKUP DATABASE' + ' ' +
         '['               +
    @DBName           +
         ']' + ' '         +
    'TO DISK ='       +
    @BackupFile       + ' ' +
    'WITH'            + ' ' +
    'NOFORMAT'        +
    ',NOINIT'         +
    ',NAME ='         +
    @BackName         +
    ',SKIP'           +
    ',NOREWIND'       +
    ',NOUNLOAD'       +
    ',STATS ='        + ' ' +
    @Stats            +
    ',CHECKSUM')

DECLARE @backupSetId AS INT
SELECT
    @backupSetId    = position
    FROM  msdb..backupset
    WHERE database_name   = @DBName
    AND   backup_set_id   = (SELECT MAX(backup_set_id)
                             FROM msdb..backupset
                             WHERE database_name = @DBName )

IF @backupSetId IS NULL
BEGIN
    raiserror(@msg, 16, 1)
END

RESTORE VERIFYONLY                  --------------这行开始报错
FROM    DISK = @BackupFile
WITH
    CHECKSUM ,
    STOP_ON_ERROR ,
    FILE = @backupSetId, 
    NOUNLOAD ,
    NOREWIND



RESTORE VERIFYONLY这里开始报错:
无法打开备份设备 'C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup\'E:\FULL\Beijixiong_Data_full_201205.bak''。出现操作系统错误 123(文件名、目录名或卷标语法不正确。)。
备份过程没有建立备份设备,而是直接指定备份为一个磁盘上的BAK文件,此文件定义在@BackupFile2中,然后传递给@BackupFile。一路监视下来,无论@BackupFile还是@BackupFile2,始终是'E:\FULL\Beijixiong_Data_full_201205.bak',没有任何变化,那这是正确的啊,成功备份也印证了这个字串没有错误。我以为是两个变量传递过程中出错了,试过只用一个变量@BackupFile,结果也还是报错,求解释,求指点!谢谢!

------解决方案--------------------
查看变量@BackupFile的值是怎么来的,逐个排查.