Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

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.
0
btetlow-expert
Asked:
btetlow-expert
1 Solution
 
ralmadaCommented:
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now