Friday, 15 July 2011

sql - Find all chlidren for more parents -


I am using the hierarchy in my table (SQL Server 2008 R2) for all the nodes for more than one node Have to find. My result of parents looks like this:

  | Parents |: ----------- | | 0x6CB0AC | | 0x6CB180 | 0x6CB181 | | 0x6CB141 | | |  

How should I write that question, which will return all the offspring to all those parents?

Design of my table:

  ID: HierarchyId name: varchar (100) Alias: varchar (100)  
< P>

Below is a table name tree, here are the questions:

 ; Select as P (ParentId as hierarchyid) as ParentId (VALUES (0x6CB0AC), (0x6CB180)) as parent form (ParentId) * Where does Tree T exist (P Choose from * T.Id.IsDescendantOf (P.ParentId) = 1)  

Edit: The last two binary strings I removed because they are not valid hierarchyid

Edit :. Here is an example that shows you descendants of 1.1 and 1.2, with hierarchyid 0x5AC0 and 0x6AC0

  create table trees (id hierarchyid primary key, name varchar (100)); INSERT INTO tree (number, name) values ​​('/ 1 /', '1'), ('/ 1/1 /', '1.1'), ('1/1/1 /', '1.1.1 '', ('/ 1/1/2 /', '1.1.2'), ('/ 2 /', '2'), ('/ 2/1 /', '2.1'), ('/ 2/1/1 / ',' 2.1.1 '), (' / 2/1/2 / ',' 2.1.2 '); as ID (select cast (form of hierarchyid as ID (Where's the selected T * Tree T with the help of (0x5AC0), (0x6ac0) T (ID) from values ​​(select from id * T.Id.IsDescendantOf (Ids.Id) = 1) / Code>  

Result:

  + -------- + ------- + | ID | name | +---- ---- + ------- + | 0x5AC0 | 1.1 | | 0x5AD6 | 1.1.1 | | 0x5ADA | 1.1.2 | | 0x6AC0 | 2.1 | | 0x6Ad6 | 2.1.1 | | 0x6ADA | 2.1 .2 | + -------- + ------- +  

Keep in mind that parents consider themselves as their descendants.


No comments:

Post a Comment