Take a look at Re: Recursive loop and SCAN Thread #683191.
>Dear all > >Following is the code I have written (in FPD25) to generate a SQL Cursor for a given UniqueID. The selection process is working well but the hierarchy is not maintained as it should. > < snip >