top of page

Track SQL Errors

One of our customers has a complex T-SQL process that involves several long stored procedures and runs for hours. The process fails sometimes, and the customer doesn't know why it fails or where in the code it happens.


We decided to create an event session (Extended Events) to capture errors with all the information we need, so that we can then easily query and analyze the collected data.


ree

This is actually a common solution for many use cases. It simply collects data about SQL errors in your SQL Server instance. One benefit is that you can easily customize it to filter specific errors (e.g. only high-severity errors). Another benefit is that you get the information in a nice tabular format, and you can then continue to manipulate it and format the way you want it.


Here is the script that creates the event session and the query to extract the collected data: https://github.com/MadeiraData/MadeiraToolbox/blob/master/Monitoring%20Scripts/Track%20SQL%20Errors.sql.


A few comments:

  1. I use the "error_reported" event that triggers on any SQL error that is raised in the instance.

  2. I filter on the severity to capture only real errors with severity above 10.

  3. Notice that event timestamps are always in UTC, so I use a combination of SWITCHOFFSET and DATENAME to return the timestamp in the current time zone.

  4. I use the "tsql_frame" action to extract the SQL handle, as well as the statement start & end offset and the line number. I then use sys.dm_exec_sql_text to retrieve the module name and the statement text. If the SQL handle doesn't exist anymore (the plan is no longer in cache), then all of this information is gone. This is why it's important to query the data soon after the event occurred or persist the results in a table to query it later.

Go ahead and try it. Create the event session. Then generate some errors. Then query the data to see how it looks like. Have fun!

3 Comments


Guest
Sep 15

Choosing a Escorts in Noida means enjoying companionship that’s passionate and personal. She’s glamorous, confident, and engaging, ensuring your desires are met naturally. Whether you’re craving laughter, romance, or fiery intimacy, she makes it happen with ease. Every meeting feels refreshing, leaving you with cherished memories of warmth, passion, and satisfaction that last long after the night ends.

Like

N
Sep 13

QN

Like

takoankosi
Sep 04

Basketball Bros is a refreshing mix of sports and arcade action where your goal is to shoot better, react faster, and outscore your basketball rival.

Like

STAY IN TOUCH

Get New posts delivered straight to your inbox

Thank you for subscribing!

bottom of page