Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Hierarchal Query
Message
From
18/09/2007 08:17:41
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
17/09/2007 03:13:15
Prashant Dongare
Micropro Software Solutions
Nagpur, India
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
01254632
Message ID:
01255079
Views:
17
>I am fixed in this perticular situation, where I want to retrive Hierarchal data from the following table.
>
>
>Field Name      Type                Width
>LVLCODE         Numeric                 2
>PRCODE          Numeric                 2
>NAME            Character              20
>
>
>
>In Oracle I use the following Query to traverse the data from root to the last child.
>
>
>
> SELECT LPAD(' ',3*LVLCODE) || Name,
>         LVLCODE, PRCODE
> FROM   LEVELMST
> CONNECT BY PRIOR LVLCODE = PRCODE
> START WITH PRCODE = 0
>
>
>
>To traverse from child to root the query in Oracle is
>
>
>
> SELECT LPAD(' ',3*LVLCODE) || Name,
>         LVLCODE, PRCODE
> FROM   LEVELMST
> CONNECT BY LVLCODE = PRIOR PRCODE
> START WITH LVLCODE = 17
>
>
>
>Please help me with these queries in VFP.
>Thanks in advance.
>
>Regards
>Prashant

Unlike MSSQL2005 and Oracle, VFP doesn't have out-of-the-box for it. You can do something like:
Select first_Name-(' '+last_Name) As Name,;
  INT(Val(emp_id)) As lvlCode, ;
  INT(Val(reports_to)) As prCode ;
  FROM (_samples+'data\employee') ;
  INTO Cursor myTable nofilter
Use In 'employee'
Index On lvlCode Tag lvlCode

Select Padr(Name,250) As Name,lvlCode,prCode ;
  FROM myTable ;
  WHERE .F. ;
  INTO Cursor crsResult ;
  readwrite

Select myTable
Scan For prCode = 0
  Scatter Name oRoot
  Insert Into crsResult From Name oRoot
  GetChildren('lvlCode','prCode',lvlCode,'crsResult','myTable','lvlCode',1)
ENDSCAN
Select crsResult 
locate
BROWSE NAME oResult nowait
oResult.AutoFit()

Procedure GetChildren(tcPKey,tcFKey,tuFkey,tcTarget,tcSource,tcTag,tnPad)
  Local oRecord,ix
  Local Array aChildren[1]
  Select &tcPKey ;
    FROM (m.tcSource) ;
    WHERE &tcFKey == m.tuFkey ;
    INTO Array aChildren
  If _Tally > 0
    For ix = 1 To Alen(aChildren,1)
      Select (m.tcSource)
      =(Seek(aChildren[m.ix,1],m.tcSource,m.tcTag))
      Scatter Name oRecord
      oRecord.Name = Space(m.tnPad*3)+oRecord.Name
      Insert Into (m.tcTarget) From Name oRecord
      GetChildren(m.tcPKey,m.tcFKey,aChildren[m.ix,1],m.tcTarget,m.tcSource,m.tcTag, m.tnPad+1)
    Endfor
  Endif
Endproc
Getting root from child is much easier.
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform