Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I flush the PRINT buffer in TSQL?

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 the messages back from SQL Server at the very end of my sproc - I'd like to be able to flush the message buffer and see these messages immediately during the sproc's runtime, rather than at the very end.

like image 248
Erik Forbes Avatar asked Nov 20 '08 21:11

Erik Forbes


People also ask

How do you clear output in SQL Server?

Right-click in the Results pane, point to Pane, and then click Clear Results. If a query is being executed when you clear the Results pane, the Query and View Designer stops the query.

Do SQL prints slow down?

A handful of PRINT statements will have a negligible effect on performance - PRINT s in loops that are executed many thousands of times, however, may well cause performance issues.

What is print command in SQL?

The SQL PRINT statement serves to display the user-defined message. For example, you are developing a script with T-SQL Loops, and you want to display some specific message on each iteration of a loop. Then you can use the PRINT statement. Or, you can use it when developing a script with conditional statements.


1 Answers

Use the RAISERROR function:

RAISERROR( 'This message will show up right away...',0,1) WITH NOWAIT 

You shouldn't completely replace all your prints with raiserror. If you have a loop or large cursor somewhere just do it once or twice per iteration or even just every several iterations.

Also: I first learned about RAISERROR at this link, which I now consider the definitive source on SQL Server Error handling and definitely worth a read:
http://www.sommarskog.se/error-handling-I.html

like image 66
Joel Coehoorn Avatar answered Sep 22 '22 18:09

Joel Coehoorn