2
我試圖獲取缺失完整備份的列表,但我需要此列表來顯示哪個用戶執行了最新的備份。問題是,我不知道如何過濾,因爲我查詢的是爲每個用戶返回所有最後的備份。SQL Server缺少備份
例子:
SELECT
@@SERVERNAME AS ServerName,
d.[name] AS DatabaseName,
b.user_name as UserName,
'Backups Not Performed Recently' AS Finding,
'Last backed up: ' + COALESCE(CAST(MAX(b.backup_finish_date) AS VARCHAR(25)),'never') AS Details
FROM
master.sys.databases d
LEFT OUTER JOIN
msdb.dbo.backupset b ON d.name COLLATE SQL_Latin1_General_CP1_CI_AS = b.database_name COLLATE SQL_Latin1_General_CP1_CI_AS
AND b.type = 'D'
AND b.server_name = SERVERPROPERTY('ServerName') /*Backupset ran on current server */
WHERE
d.database_id <> 2 /* Bonus points if you know what that means */
AND d.state NOT IN(1, 6, 10) /* Not currently offline or restoring, like log shipping databases */
AND d.is_in_standby = 0 /* Not a log shipping target database */
AND d.source_database_id IS NULL /* Excludes database snapshots */
GROUP BY
d.name, b.user_name
HAVING
MAX(b.backup_finish_date) <= DATEADD(dd, -7, GETDATE())
OR MAX(b.backup_finish_date) IS NULL
返回:
ServerName DatabaseName UserName Finding Details
ServerA DatabaseA Userxyz Backups Not Performed Recently Last backed up: Nov 30 2016 2:45PM
ServerA DatabaseA Userabc Backups Not Performed Recently Last backed up: Nov 24 2016 12:36PM
ServerA DatabaseA Userdef Backups Not Performed Recently Last backed up: Feb 5 2017 1:26AM
當我只需要它從最新的日期返回ID和結果。理想的結果是這樣的:
ServerName DatabaseName UserName Finding Details
ServerA DatabaseA Userxyz Backups Not Performed Recently Last backed up: Feb 5 2017 1:26AM
您的解決方案很有趣,但我仍然對面臨的問題服務器,查詢應該不會返回任何內容 – carolcastelli
@carolcastelli然後,也許你應該用反映你正面臨的實際問題的數據來更新你的問題。我的查詢符合您向我們顯示的測試數據。 –