How do I flush the PRINT buffer in TSQL?

后端 未结 5 1677
伪装坚强ぢ
伪装坚强ぢ 2020-11-29 16:04

I have a very long-running stored procedure in SQL Server 2005 that I\'m trying to debug, and I\'m using the \'print\' command to do it. The problem is, I\'m only getting th

5条回答
  •  醉酒成梦
    2020-11-29 16:41

    Building on the answer by @JoelCoehoorn, my approach is to leave all my PRINT statements in place, and simply follow them with the RAISERROR statement to cause the flush.

    For example:

    PRINT 'MyVariableName: ' + @MyVariableName
    RAISERROR(N'', 0, 1) WITH NOWAIT
    

    The advantage of this approach is that the PRINT statements can concatenate strings, whereas the RAISERROR cannot. (So either way you have the same number of lines of code, as you'd have to declare and set a variable to use in RAISERROR).

    If, like me, you use AutoHotKey or SSMSBoost or an equivalent tool, you can easily set up a shortcut such as "]flush" to enter the RAISERROR line for you. This saves you time if it is the same line of code every time, i.e. does not need to be customised to hold specific text or a variable.

提交回复
热议问题