Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.databases.ms-sqlserver > #476
| From | Erland Sommarskog <esquel@sommarskog.se> |
|---|---|
| Newsgroups | comp.databases.ms-sqlserver |
| Subject | Re: Generating a tree structure from data |
| Date | 2011-06-22 22:18 +0200 |
| Organization | Erland Sommarskog |
| Message-ID | <Xns9F0CE2DC065AFYazorman@127.0.0.1> (permalink) |
| References | <b8b37caa-5853-4a76-adc0-c5f0cc28a16a@a10g2000vbz.googlegroups.com> |
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 | Next — Previous in thread | Next in thread | Find similar
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