home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.databases.ms-sqlserver      Notorious Rube Goldberg contraption      19,505 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 18,369 of 19,505   
   Erland Sommarskog to Steve   
   Re: Generating a tree structure from dat   
   22 Jun 11 22:18:04   
   
   50c7ed8c   
   From: esquel@sommarskog.se   
      
   Steve (steve.simpson@aircelle.com) writes:   
   > parent is either blank, Y (meaning there are child links) and H   
   > (meaning it's the top level of a structure)   
      
   That's an unusual column for that type of structure. I don't see what it   
   adds. Usually this relation is encoded in the ids. And what does blank mean?   
      
   > I want to be able to create a procedure that recusively loops through   
   > the table (intradtl) and outputs an indented structure of the file   
   >   
   > Level    description   
   > ..0         <>   
   > ...1        <>   
   > .....2      <>   
   > ...1        <>   
   > etc   
      
   So ignoring Parent, and assuming that you are on SQL 2005 or later, you   
   can do:   
      
   WITH rekurs AS (   
      SELECT icode, iddesc, lvl = 0   
      FROM   intradt1   
      WHERE  idcode IS NULL   
      UNION  ALL   
      SELECT i. icode, i.iddesc, r.lvl + 1   
      FROM   intradt1 i   
      JOIN   rekurs r ON  i.idcode = i.code   
   )   
   SELECT replicate('.', lvl + 2) + ltrim(str(lvl)), idddesc   
   FROM   rekurs   
      
      
   --   
   Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se   
      
   Links for SQL Server Books Online:   
   SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx   
   SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca