in reply to (OT?) Recursive sql queries?

That would depend quite a bit on which Database you are actually using, and if they allow recursive SQL. DB2 does, for example, you define a temporary view, which can select on itself. (Thus each iteration/recursion grabs the parent of the last node stored).

Eg:

Recursive SQL (DB2): (WITH X defines a temporary view) WITH trips(destination, route, totalcost) AS ((SELECT destination, destination, cost FROM flights WHERE origin = "SFO") UNION ALL (SELECT f.destination, t.route || ',' || f.destination, t.totalcost * f.cost FROM trips t, flights f WHERE t.destination = f.origin)) SELECT route, totalcost FROM trips WHERE destination = "JFK"; NB: There is no stop condition, so this will run indefinitely..
(Which is currently sitting around on my scratchpad, together with an iterative solution, using an SQL procedure, which is probably faster than using perl, let the database do the work.)

The recursion in DB2 works such that each recursion only 'sees' the results of the previous one.

Anyway, this rambling doesnt help if you dont have DB2, or anything that does such temporary views.. So the other possibility is a stroed sql procedure, assuming you can do those.

C.