pjpscriv
pjpscriv

Reputation: 1269

Is there a better logging alternative than RAISEERROR?

I have been using RAISEERROR in my database scripts to log informational messages.

DECLARE @totalrows INT = (SELECT COUNT(*) FROM mytable);
DECLARE @fancyrows INT = (SELECT COUNT(*) FROM mytable WHERE fancy = 1);
RAISEERROR ('%i rows out of %i are fancy', 10, 1, @fancyrows, @totalrows) WITH NOWAIT;
-- Do some more things...

Using a severity parameter lower than 11 means it won't stop the execution of the script, but my co-worker pointed out that when reading the script, it does still seem to imply something has gone wrong.

Is there a clearer way of logging informational messages in SQL? Something like LOGMESSAGE for example?

Upvotes: 0

Views: 510

Answers (1)

pjpscriv
pjpscriv

Reputation: 1269

To give this question an answer (based on @Scratte's link & @Martin Smith's comment):

RAISEERROR is the most powerful method of logging in SQL Server. There is a PRINT method that is functionally similar but falls short on a couple of points. The advantages of RAISEERROR are:

  • Natively allows string interpolation.
  • Can be used with WITH NOWAIT causing its output to be logged immediately. This is not true for PRINT which uses a message buffer that can delay its output

Upvotes: 1

Related Questions