You'll have to read all the records in before you can generate any HTML. There are at least 2 ways of doing this:
1) Read values into a ColdFusion array, instead of generating HTML directly. When you have read all records, you will know how many there are, and can start generating the table...