• Great article, Chris!

    I didn't know about SQL Server Agent tokens until now. I plan to use them for an ErrorLog in SQL Server, but I saw a problem that affects your code too: if the error message contains single quotes ('), the string built for the @message parameter ends there, and the remaining words of the error message will be interpreted as another command, usually leading to an error. The solution that I plan to use is to SET QUOTED_IDENTIFIER OFF and use double quotes (") instead of single quotes. There still is a chance of failure, but it's smaller, because there are much fewer error messages that contain double quotes compared to the error messages that contain single quotes.

    Razvan