Snippet Name: Failed_Reports_Failure_Reason
Tags:
Created Date: 2025-06-16 13:12:41
Last Modified Date: 2025-05-31 10:32:10
Dependencies:
DBA Notes:
Sample Output:
Title: Failed Reports Failure Reason
Author: user
for SQL Server 2019, i have the following query which finds recent reports that failed, and their status. is the reason for the failure available to add to this SLQ Server Query? SELECT '[Failed Reports]' AS [Failed Reports],
[ex].[Status] + ' Today' AS Focus,
--@Layer AS ServerLayer,
CONVERT(VARCHAR(128),@@SERVERNAME) AS ServerName,
CONVERT(VARCHAR(128),DB_NAME()) AS DatabaseName,
a.name,
DATEDIFF(minute,[ex].TimeStart,[ex].TimeEnd) AS ElapsedTime,
[ex].[Status],
[ex].[RequestType],
[ex].[Format],
[ex].[Parameters],
[ex].[ReportAction],
[ex].[TimeStart],
[ex].[TimeEnd]
FROM [ReportServer].dbo.[ExecutionLog2] ex
INNER JOIN [ReportServer].[dbo].[Catalog] a
ON ex.[ReportPath] = a.[Path]
WHERE [RequestType] ='Subscription'
AND [TimeStart] > DATEADD(dd,-1,CONVERT(DATE,getdate()))
AND [ex].[TimeStart] >=@dt
AND &#91;ex].&#91;Status] <> 'rsSuccess' --'rsProcessingAborted'
ORDER BY &#91;ex].&#91;TimeStart] DESC
Author: ChatGPT
Yes, you can add the failure reason to your SQL Server query by including the `ByteCount` and `RowCount` columns from the `&#91;ReportServer].dbo.&#91;ExecutionLog2]` table. These columns provide additional details about the execution results. However, to get more specific error details, you should look at the `AdditionalInfo` column, which stores XML data containing detailed error messages.
Here's how you can modify your query to include the error details:
```sql
SELECT
'&#91;Failed Reports]' AS &#91;Failed Reports],
&#91;ex].&#91;Status] + ' Today' AS Focus,
CONVERT(VARCHAR(128), @@SERVERNAME) AS ServerName,
CONVERT(VARCHAR(128), DB_NAME()) AS DatabaseName,
a.name,
DATEDIFF(minute, &#91;ex].&#91;TimeStart], &#91;ex].&#91;TimeEnd]) AS ElapsedTime,
&#91;ex].&#91;Status],
&#91;ex].&#91;RequestType],
&#91;ex].&#91;Format],
&#91;ex].&#91;Parameters],
&#91;ex].&#91;ReportAction],
&#91;ex].&#91;TimeStart],
&#91;ex].&#91;TimeEnd],
&#91;ex].&#91;ByteCount],
&#91;ex].&#91;RowCount],
TRY_CONVERT(XML, &#91;ex].&#91;AdditionalInfo]).value('(ReportServerMessage)&#91;1]', 'NVARCHAR(MAX)') AS FailureReason
FROM
&#91;ReportServer].dbo.&#91;ExecutionLog2] ex
INNER JOIN
&#91;ReportServer].&#91;dbo].&#91;Catalog] a
ON ex.&#91;ReportPath] = a.&#91;Path]
WHERE
&#91;RequestType] = 'Subscription'
AND &#91;TimeStart] > DATEADD(dd, -1, CONVERT(DATE, GETDATE()))
AND &#91;ex].&#91;TimeStart] >= @dt
AND &#91;ex].&#91;Status] <> 'rsSuccess' --'rsProcessingAborted'
ORDER BY
&#91;ex].&#91;TimeStart] DESC;
```
### Explanation of the changes:
1. **`ByteCount` and `RowCount`**: These columns provide information about the data processed during the report execution.
2. **`AdditionalInfo` XML Parsing**:
- The `AdditionalInfo` column contains XML details, which include potential error messages.
- Using `TRY_CONVERT(XML, &#91;ex].&#91;AdditionalInfo])` ensures that the query does not fail if the data is not in valid XML format.
- The `.value('(ReportServerMessage)&#91;1]', 'NVARCHAR(MAX)')` extracts the error message from the XML.
This should help you determine the reason for report failures within the last day. Let me know if you need any adjustments!
Author: user
it appears that the output of the xml column doe not contain the tag we were searching for: TRY_CONVERT(XML, &#91;ex].&#91;AdditionalInfo]).value('(ReportServerMessage)&#91;1]', 'NVARCHAR(MAX)') AS FailureReason
Author: ChatGPT
If the `AdditionalInfo` column doesn't contain the expected `<ReportServerMessage>` tag, it's important to inspect its actual structure. You can retrieve a sample of the XML content to analyze its tags and find where error messages might be stored.
### Step 1: Inspect the XML Structure
Run the following query to get a sample of the XML content:
```sql
SELECT TOP 10
CONVERT(VARCHAR(MAX), &#91;AdditionalInfo]) AS AdditionalInfo
FROM &#91;ReportServer].dbo.&#91;ExecutionLog2]
WHERE &#91;AdditionalInfo] IS NOT NULL
AND &#91;Status] <> 'rsSuccess';
```
Once you have the structure, look for common tags such as:
- `<Messages>` or `<Message>`
- `<Error>`
- `<Exception>`
- `<Details>`
### Step 2: Adjust the Query Based on Actual Tags
After identifying the correct tag, adjust the query like this:
```sql
SELECT
'&#91;Failed Reports]' AS &#91;Failed Reports],
&#91;ex].&#91;Status] + ' Today' AS Focus,
CONVERT(VARCHAR(128), @@SERVERNAME) AS ServerName,
CONVERT(VARCHAR(128), DB_NAME()) AS DatabaseName,
a.name,
DATEDIFF(minute, &#91;ex].&#91;TimeStart], &#91;ex].&#91;TimeEnd]) AS ElapsedTime,
&#91;ex].&#91;Status],
&#91;ex].&#91;RequestType],
&#91;ex].&#91;Format],
&#91;ex].&#91;Parameters],
&#91;ex].&#91;ReportAction],
&#91;ex].&#91;TimeStart],
&#91;ex].&#91;TimeEnd],
&#91;ex].&#91;ByteCount],
&#91;ex].&#91;RowCount],
TRY_CONVERT(XML, &#91;ex].&#91;AdditionalInfo]).value('(//ErrorMessage)&#91;1]', 'NVARCHAR(MAX)') AS FailureReason
FROM
&#91;ReportServer].dbo.&#91;ExecutionLog2] ex
INNER JOIN
&#91;ReportServer].&#91;dbo].&#91;Catalog] a
ON ex.&#91;ReportPath] = a.&#91;Path]
WHERE
&#91;RequestType] = 'Subscription'
AND &#91;TimeStart] > DATEADD(dd, -1, CONVERT(DATE, GETDATE()))
AND &#91;ex].&#91;TimeStart] >= @dt
AND &#91;ex].&#91;Status] <> 'rsSuccess'
ORDER BY
&#91;ex].&#91;TimeStart] DESC;
```
**Adjust the XPath expression (`//ErrorMessage`)** to match the correct error tag found in your XML data.
### Step 3: If XML Tags Are Still Unknown
If the XML structure remains unclear, you can extract the raw content and review it manually:
```sql
SELECT
&#91;ex].&#91;Status],
CONVERT(VARCHAR(MAX), &#91;ex].&#91;AdditionalInfo]) AS AdditionalInfo
FROM &#91;ReportServer].dbo.&#91;ExecutionLog2] ex
WHERE &#91;ex].&#91;Status] <> 'rsSuccess'
AND &#91;ex].&#91;AdditionalInfo] IS NOT NULL
ORDER BY &#91;ex].&#91;TimeStart] DESC;
```
Once you identify the relevant tag, refine the query to extract failure reasons accordingly.
Let me know if you need help analyzing the output.