Teken eens een boom in SQL. Gegeven een tabel tblNavigation met velden id, parent_id, place [volgorde onder parent_id] en nameEN:

create proc dbo.MakeTree (@Root int) as
begin
  set nocount on
  declare @id int, @place int, @pageName varchar(255)
  set @pageName=(select nameEN from tblNavigation where id=@Root)
  print space(@@nestlevel*2)+'> '+@pageName
  set @id=(select min(id) from tblNavigation where parent_id=@Root)
   while @id is not null begin
       exec dbo.MakeTree @id
       set @place=(select min(place) from
                   dbo.tblNavigation where parent_id=@Root
                   and place>(select place from tblNavigation where id=@id))
       set @id=(select min(id) from dbo.tblNavigation
                where parent_id=@Root and place=@place)
   end
end

…en dat dat aanroepen met exec dbo.MakeTree [root] waarbij [root] het id is van de node waarvan de kinderen moeten getoond worden in een boompje.

‘t zal geen schoonheidsprijzen winnen, maar het doet wat het moet doen:

 > Home
   > About the College
     > Introduction
     > History
     > Locations
     > Campus Life
     > Governing Bodies
     > Financial Supporters
   > Study Programmes
     > Introduction
     > Economic Studies
       > Introduction
       > Study Programme
         > Introduction
         > Compulsory Courses
         > Optional Courses
         > Compact Courses

Geschreven al luisterend naar: Henryk Górecki – Symphony No 3, Op. 36 (London Sinfonietta feat. conductor: David Zinman; soprano: Dawn Upshaw) – II Lento e Largo: Tranquillissimo

Geschreven al luisterend naar: Michelle Shocked – Arkansas Traveler – Over the Waterfall



Reacties

Eén reactie op “Van SQL”