2014-03-04 146 views
0

我想創建一個報告,詳細介紹我們的SQL Server上的最新備份。截至目前我已經這樣寫出來: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_Backup的日期和Last_Log_Backup我想我必須做一些擺動,但我不太確定(我對擺動非常不熟悉)。

任何幫助將不勝感激。感謝所有提前。

回答

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 
+0

這看起來很棒!我可以添加到「CASE」語句來處理NULLS。你會如何建議我自[F_SEL_LAST_Backup]上次完整顯示天數?我會改變列閱讀[Days_Since_Last_Full] ... – homerj742

+0

我也想嘗試保持它每行一個數據庫。這個查詢返回每行2個數據庫。 – homerj742

+0

@ user3380003「GROUP BY」包含的類型不應該在那裏刪除。不知道是否應該包含備份集名稱,因爲我不確定它的用途。 –