42

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?

4 Answers 4

34

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
Sign up to request clarification or add additional context in comments.

2 Comments

Note that this doesn't work after the first 500 messages; once you print more than that it suddenly starts buffering!
Doesn't work for me in SSMSv 18.1. Maybe things have changed sinze 2015...
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?

Comments

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

1 Comment

Could you format your code according to the markdown rules please?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.