Display column data from within a while loop in MS SQL

Posted on 2012-08-30
Last Modified: 2012-08-30
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)
          select count(*) from dbo.queue
          waitfor delay '0:5:0'
          set @ flag = (@flag + 1)

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.
Question by:btetlow-expert
    LVL 41

    Accepted Solution

    you could try

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

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

    Author Comment


    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!


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
    Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now