对许多数据库进行verifyonly还原

时间:2017-05-31 18:00:42

标签: sql-server database tsql backup database-backups

DECLARE @backupSetId INT
DECLARE @URL NVARCHAR(max)
SET @URL = ''

SELECT @backupSetId = position 
FROM msdb..backupset 
WHERE database_name = N'database name' 
  AND backup_set_id = (SELECT MAX(backup_set_id) 
                       FROM msdb..backupset 
                       WHERE database_name = N'database name')

IF @backupSetId IS NULL
BEGIN 
    RAISERROR(N'Verify failed. Backup information for database '
'myActions'' not      found.', 16, 1) 

END

RESTORE VERIFYONLY 
FROM @URL'n'
WITH FILE = @backupSetId,  checksum
GO

1 个答案:

答案 0 :(得分:0)

我正在回答,但我并不是100%有信心这是否会成功,因为我没有在虚拟存储池中备份进行测试。我也使用数据库ID作为行号(@Loop)作弊。与数据库计数相同(因为我也不知道这一点)。这可以更优雅地完成。希望这至少可以引导您朝着正确的方向前进:

DECLARE @backupSetId INT
DECLARE @URL NVARCHAR(max)
DECLARE @DatabaseName NVARCHAR(100)
DECLARE @Loop int = 1
DECLARE @NumberOfDBs int = 0

SET @URL = ''

SET @NumberOfDBs = (SELECT COUNT(*) FROM sys.databases)

WHILE @Loop <= @NumberOfDBs
BEGIN

    SET @DatabaseName = (SELECT name FROM sys.databases WHERE database_id = @Loop)

    EXEC sp_executesql 
    N'SELECT @backupSetId = position 
    FROM msdb..backupset 
    WHERE database_name = N''@DatabaseName'' 
    AND backup_set_id = (SELECT MAX(backup_set_id) 
                   FROM msdb..backupset 
                   WHERE database_name = N''@DatabaseName'')'

    IF @backupSetId IS NULL
        BEGIN 
             RAISERROR(N'Verify failed. Backup information for database ''myActions'' not      found.', 16, 1) 
        END




RESTORE VERIFYONLY 
FROM @URL
WITH FILE = @backupSetId,  checksum

SET @Loop = @Loop +1
END