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.

You dont want to raise an error so you 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

Cheers
John

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.