• Did author ever think about purpose of the IDENTITY column in his temporary table? It's perfect tool to replace loops!

    He needed just to add another column to his temporary table: Counts varchar(8000) NULL to store his @Counts, than update this column where ID <= @NumCounts1 or ID > Max(ID) - @NumCounts2, than set Counts = '-----------------' where Counts is NULL, and in the end select max(ID), Counts group by Counts order by max(ID) (ASC, DESC - whatever you say).

    Result will be the same, but no loops inside!

    And that MUST be done in TSQL, because it's really set oriented task for data processing.

    To do it in Crystal you need to supply all data from DB to Client side, then process it and drop it. Of course if you don't care about network traffic and you Client is equipped with flash new P4 + a lot of RAM, you must use it somehow... But does Intel pay you for marketing?

    _____________
    Code for TallyGenerator