Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #476

Re: Generating a tree structure from data

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail
From Erland Sommarskog <esquel@sommarskog.se>
Newsgroups comp.databases.ms-sqlserver
Subject Re: Generating a tree structure from data
Date Wed, 22 Jun 2011 22:18:04 +0200
Organization Erland Sommarskog
Lines 40
Message-ID <Xns9F0CE2DC065AFYazorman@127.0.0.1> (permalink)
References <b8b37caa-5853-4a76-adc0-c5f0cc28a16a@a10g2000vbz.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252
Content-Transfer-Encoding 8bit
Injection-Info mx04.eternal-september.org; posting-host="DD6dU+BfJNjsjSP4/K/V7w"; logging-data="6940"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/bDC/a+IMF+vnUq3zzJ2Kr"
User-Agent Xnews/2006.08.24 Mime-proxy/2.1.c.0 (Win32)
Cancel-Lock sha1:Q/uzVU2PokadgOK+u3e7Zwk/Lwk=
Xref x330-a1.tempe.blueboxinc.net comp.databases.ms-sqlserver:476

Show key headers only | View raw


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         <<iddesc>>
> ...1        <<iddesc>>
> .....2      <<iddesc>>
> ...1        <<iddesc>>
> 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

Back to comp.databases.ms-sqlserver | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Generating a tree structure from data Steve <steve.simpson@aircelle.com> - 2011-06-22 02:40 -0700
  Re: Generating a tree structure from data Erland Sommarskog <esquel@sommarskog.se> - 2011-06-22 22:18 +0200
  Re: Generating a tree structure from data --CELKO-- <jcelko212@earthlink.net> - 2011-06-23 07:36 -0700
  Re: Generating a tree structure from data rembrandt <batesbrandt@gmail.com> - 2011-06-29 04:47 -0700

csiph-web