in reply to Looping an SQL query

You might also want to give Understanding and Using PerlMonks a try.