SQL Server备份报告

时间:2014-03-04 18:07:04

标签: sql sql-server database administration backups

我正在尝试创建一个报告,详细说明SQL Server上的最新备份。截至目前,我写了这篇文章:

select 
    [Server_Name] = a.server_name, 
    [Database_Name] = a.database_name, 
    [Last_Backup] = max(a.backup_finish_date),  
    [Backup_Type] = CASE A.type
                              WHEN 'D' THEN 'FULL'
                              WHEN 'I' THEN 'Differential'
                              WHEN 'L' THEN 'Log'
                              WHEN 'F' THEN 'FileGroup'
                              WHEN 'G' THEN 'FileGroup Differential'
                              WHEN 'P' THEN 'Partial'
                              WHEN 'Q' THEN 'Partial Differential'
                  END,
    [Backup_Set] = b.name,
    [Days_Since_Last_Backup] = DATEDIFF(d,(max(a.backup_finish_Date)),GETDATE())
from msdb.dbo.backupset AS a
INNER JOIN msdb.dbo.backupset as b ON a.backup_set_id = b.backup_set_id
GROUP BY a.database_name, a.server_name, b.name, a.type
ORDER BY database_name

我想更改它,以便列显示以下内容:

Server_Name, Database_Name, Last_Full_Backup, Last_Diff_Backup, Last_Log_Backup, 
Backup_Set, Days_Since_Last_Full

为了显示Last_Full_BackupLast_Diff_BackupLast_Log_Backup的日期,我想我必须做一些转动,但我不确定如何(我很不熟悉旋转)。

非常感谢任何帮助。感谢所有提前。

1 个答案:

答案 0 :(得分:0)

你可以做一个“手动转轴”,比如;

SELECT 
    [Server_Name] = a.server_name, 
    [Database_Name] = a.database_name, 
    [Last_Backup]      = MAX(a.backup_finish_date),  
    [Last_Full_Backup] = MAX(CASE WHEN A.type='D' 
                                    THEN a.backup_finish_date ELSE NULL END),
    [Last_Diff_Backup] = MAX(CASE WHEN A.type='I' 
                                    THEN a.backup_finish_date ELSE NULL END),
    [Last_Log_Backup]  = MAX(CASE WHEN A.type='L' 
                                    THEN a.backup_finish_date ELSE NULL END),
    [Backup_Set] = b.name,
    [Days_Since_Last_Backup] = DATEDIFF(d,(max(a.backup_finish_Date)),GETDATE())
FROM msdb.dbo.backupset AS a
INNER JOIN msdb.dbo.backupset as b ON a.backup_set_id = b.backup_set_id
GROUP BY a.database_name, a.server_name
ORDER BY database_name