SQL Server: how to print during program execution?

Print output appears in Messages window only after all sprocs execution completes. How to see results during execution, not in the end.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Not sure I understand what you mean but I usually use GO after each proc block to execute a batch of script at a time
Deepak ChauhanSQL Server DBACommented:
You can use the "print" keyword like :

declare @min int, @max int , @cmd varchar(20)
set @min =1
set @max=10000000
set @cmd='Current Value is: '
while @min <=@max
print @cmd+cast(@min as varchar)

set @min=@min+1
you add print statements whenever required
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
>How to see results during execution, not in the end.
You'll need to provide us some more details.

I've seen some VERY long SP's where in order to be able to view progress the developers created a cursor where rows were processed 50,000 at a time, and the end of each cursor loop logged a row to a 'logging' table to show the status.  That way, developers can query the table to see the progress of the SP.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott PletcherSenior DBACommented:

You're quite right: SSMS buffers PRINT results, so you won't see them immediately; when the buffer fills, it flushes and then  you see the results.  I don't know of a really good way around that, although I've tried to find one.

You could try "RAISERROR('...', 1, 1) WITH NOWAIT", but I've seen that get buffered also.

You could also try PRINTing long dummy strings to fill the buffer, but then you have to read around those when you look at the output.
Anthony PerkinsCommented:
Good question.  If it is that important to you, your best bet is to use Jim's solution of inserting into a table and polling it frequently.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.