Display column data from within a while loop in MS SQL

I want to display the results of a select from within a while loop in SQL.

The code works, but unlike using the same select outside of the while loop, it doesn't display any results.

Is this possible?

This is my code.... nothing fancy at all....

declare @flag INT
set @flag = 0

while (@flag <12)
     begin
          select count(*) from dbo.queue
          waitfor delay '0:5:0'
          set @ flag = (@flag + 1)
     end

The intention -- get a count of records from the table Queue (display them),
wait 5 minutes, go do it again....until the loop expires.

code works -- just displays everything at the end.... I want it to display during each cycle of the loop.
LVL 5
btetlow-expertAsked:
Who is Participating?
 
ralmadaConnect With a Mentor Commented:
you could try

declare @flag INT
declare @cnt int
set @flag = 0

while (@flag <12)
     begin
          select @cnt = count(*) from dbo.queue
          raiseerror(cast(@cnt as varchar), 0, 1) with nowait
          WAITFOR DELAY '0:5:0'
          set @ flag = (@flag + 1)
     end
0
 
btetlow-expertAuthor Commented:
PERFECT.....

I added an initial select prior to the loop, as the first result doesn't appear until the first cycle concludes.... This is ok for my purposes...


Shame something so simple requires more work than needed.... BUT.... solved!

Thanks!
0
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.

All Courses

From novice to tech pro — start learning today.