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.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.