Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codykonior/d2f40c758e65b1972bd4d8ca0154c2eb to your computer and use it in GitHub Desktop.
Save codykonior/d2f40c758e65b1972bd4d8ca0154c2eb to your computer and use it in GitHub Desktop.
SELECT DISTINCT
CASE
WHEN ss.EngineEdition = 8
THEN NULL
ELSE d.HostName
END
AS ComputerName,
CASE
WHEN ss.EngineEdition = 8
THEN UPPER(esc.ObjectName)
WHEN ss.EngineEdition IS NOT NULL
THEN UPPER(d.HostName + COALESCE('\' + esc.InstanceName, ''))
END
AS ServerInstance,
td.TaskName,
thi.Message,
ts.LastSuccessTimeUtc,
ts.LastUpdatedTimeUtc,
thi.FaultText
FROM Tasks.TaskStatus ts
JOIN Tasks.TaskDefinition td
ON ts.TaskId = td.TaskId
JOIN Tasks.TaskHistoryItem thi
ON ts.TaskId = thi.TaskId
AND ts.OwnerId = thi.OwnerId
JOIN dbo.EventSource es
ON ts.OwnerId = es.ObjectID -- If it matches here it's Windows
OR ts.OwnerId = es.EventSourceConnectionId -- If it matches here it's SQL
JOIN dbo.EventSourceConnection esc
ON es.EventSourceConnectionId = esc.ObjectId
LEFT JOIN dbo.SqlServer ss
ON ts.OwnerId <> es.ObjectId -- Only join if the task is for SQL
AND esc.ObjectId = ss.ObjectId
JOIN dbo.Device d
ON d.Id = esc.DeviceId
WHERE thi.FaultText IS NOT NULL
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment