39

I have a simple query which loops and I want to see the PRINT messages during the execution. The query is something like this:

WHILE 1 = 1
BEGIN
    WAITFOR DELAY '000:00:10'
    PRINT 'here'
END

The PRINT 'here' does not output until I stop the process. However, I want to see it while it's running. Is this possible?

transport
  • 61
  • 9
Chris Klepeis
  • 9,783
  • 16
  • 83
  • 149

4 Answers4

33

You can use RAISERROR with serverity 0 and the NOWAIT option

WHILE 1 = 1
BEGIN
    WAITFOR DELAY '000:00:10'
    RAISERROR ('here', 0, 1) WITH NOWAIT
END
Mike Forman
  • 4,367
  • 1
  • 21
  • 18
  • 2
    Note that this doesn't work after the first 500 messages; once you print more than that it suddenly starts buffering! – GendoIkari Sep 10 '15 at 20:51
  • 2
    Doesn't work for me in SSMSv 18.1. Maybe things have changed sinze 2015... – Ya. Oct 08 '19 at 21:06
20

I believe that the prints get buffered, releasing "chunks" as the buffer fills up.

try using raiserror:

How do I flush the PRINT buffer in TSQL?

Community
  • 1
  • 1
KM.
  • 101,727
  • 34
  • 178
  • 212
2

Try this..

DECLARE @i INT = 1
WHILE ( @i <= 10)
BEGIN
    --- do something
    SELECT 'Completed  ' + CAST(@i AS VARCHAR(50)) + '  :  ' + CAST(GETDATE() AS VARCHAR(50));
    SET @i = @i + 1
END
codersl
  • 2,222
  • 4
  • 30
  • 33
ambrul
  • 29
  • 1
0

current suggestions don't work for SSMS 18 and above. Print then raiserror(N'', 0, 1) seems to get around this.