SQL Server - How to send warnings and error messages to the bank through CLR (C #)

Views: 1.363
Reading Time: 3 minutes

Hello people,
Good Morning!

In this post I would like to demonstrate to you as I am (and intend to continue) posting several cool things about CLR, such as sending warnings (PRINT) and error messages (RAISEERROR) to SQL Server when your CLR compiled Stored Procedures are executed.

Although the post is small, I decided to create a post just with that, because I use this class a lot in the SP's that I will publish here in the future, so I understand that it is easier to create this reference than to repost this class several times.

Server.cs file source code (prerequisite)

Return class source code:

Once this class is created in your CLR project, just import it into your Stored Procedure and start sending warnings and error messages, as I will demonstrate below:
Simulation of an error in a CLR method:

Example of use:
SQL Server - sql server clr c # csharp send warnings error messages warnings send text print error messages 2

Querying error history:
As you may have noticed, in the error method I put an SQL statement to record the history of when this method is called, making it have a CLR error log, making it easy to find possible problems in your CLR procedures.

Sql server - sql server clr c # csharp error messages print error messages

That's it folks!
Any questions, leave it here in the comments.

Hug.

sql server clr c # csharp send warnings error messages warnings send text print error messages

sql server clr c # csharp send warnings error messages warnings send text print error messages