SQL Server Print Logging in TSQL / Stored Procedures

In tsql when running stored procedures you may wish to output debug statements.

The problem is that in a long running tsql the “Print” statements will not output until the entire proc has completed.

The solution is to use the RAISERROR statement. This will force the output of the message immediately.

You don’t want to actually raise an error so call it with a 0 error code, but it has the ability to specify “WITH NOWAIT” which outputs the message straight away.

Combine this with my previous post and you can do this;

set @msg = dbo.udfGetDurationMsg('Task 1', @jobStart, getDate())
RAISERROR(@msg, 0, 0) WITH NOWAIT

which would produce output like:

Task 1 – 0mins 43sec

Hope this helps
Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.