A TSQL print statement is a misunderstood creature, possibly because of its name. It actually sends the message to the error / message processing engine, which then passes it to the calling application. PRINT is pretty dumb. You can send only 8000 characters (4000 Unicode characters). You can send a literal string, a string variable (varchar or char), or a string expression. If you use RAISERROR then you are limited to a string of only 2044 characters. However, itโs much easier to use it to send information to the calling application, since it calls a formatting function similar to the old printf in the C. C standard library. RAISERROR can also indicate the error number, severity and status code in addition to the text message, and can also be used to return custom messages created using the sp_addmessage system stored procedure. You can also force message logging.
Your error handling procedures will not be good for receiving messages, even though the messages and errors are so similar. Of course, this method depends on how you connect to the database (OLBC, OLEDB, etc.). In order to receive and process messages from the SQL Server Database Engine, when you use System.Data.SQLClient, you need to create a SqlInfoMessageEventHandler delegate that identifies the method that processes the event to listen to the InfoMessage event in the SqlConnection class, you will find that the message context information, such as severity and status, are passed as arguments to the callback because, from a system perspective, these messages are like errors.
It is always useful to have a way to receive these messages in your application, even if you simply buffer to a file, because it will always be useful for them when you try to pursue a really incomprehensible problem. However, I cannot think that Id want end users to ever see them if you cannot reserve an information layer displaying the material in the application.
Phil_Factor Nov 06 '08 at 19:40 2008-11-06 19:40
source share