SQL Server - Get all children of a row in many-to-many relationship? -
I SQL Server I am trying to write a recursive query which is basically a given parent to parent-child hierarchy Lists. A parent can have many children and a child can have many parents, so it is stored in many relationships.
I have revised the following question with any other related question, although it does not increase with all kinds of trees and only the first level children ...
Selects @ObjectId @ObjectId = '1A213431-F83D-49E3-B5E2-42AA6EB419F1' set uniqueidentifier; As trees (choose A * to Objects_In_Objects choose where A.ParentObjectId = @ObjectId UNION ALL B * Join Tree Select with A.ParentObjectId = B.ObjectId) on Join Objects_In_Objects B. An internal tree from * Objects arrest on trees. ObjectId = ar.ObjectId Do anyone know how to modify the query to go down from 'tree'? Or is it not possible to use the above construction?
Object column: ObjectId | Name Object_in_object column: ObjectId | ParentObjectId Sample data: Objects Object ID | Name 1A 213431-F 83 D-49E3-B5A2-42AA 6EB 419F1. Main container 63BD908B-54B7-4D62-BE13-B888277B7365 | Sub Container 71526 E15-F713-4F2-B707-3F5529D6B25E Sub Container 2 ADA 9A487-7256-46AD-8574-0CE9475315 E4. Objects in objects ObjectId parent object ID ADA9A487-7256-46AD-8574-0CE9475315E4 | 71526E15-F713-4F03-B707-3F5529D6B25EAdA9A487-7256-46AD-8574-0CE9475315E4 | 63 BD 908 B-54B 7-4D 62-BE 13-B888277 B 7365 63 BD 908 B-54B 7-4D 62-BE 13-B 888277 B 7365 | 1A 213431-F 83 D-49E3-B5A2-42AA 6EB 419F1 71526E15-F713-4FB-707-3F5529D6B25E. 1A213431-F83D-49E3-B5E2-42AA6EB419F1
Such a recursive CTE (Common Table expression) will goo all the way . Try this:
; Where as tree (SELECT A.ObjectID, A.ObjectName, o.ParentObjectID, 1 dbo.Objects included within a tourist A.ObjectID = on dbo.Objects_In_Objects o o.ParentObjectID A.ObjectId = @ObjectId 'level' as - here using A.ObjectId UNION aLL A2.ObjectID, A2.ObjectName, B.ParentObjectID, t.Level + tree you choose 1 T insider Join dbo.Objects_In_Objects B 'level B.ParentObjectID = t on' .ObjectID insider dbo.Objects Join Select B.ObjectId) as A2.ObjectId = on A2 Tree * insider tourist arrest dbo.Objects tree.ObjectId = ar.ObjectId < P> Do you change it Is - now works for you? (I've added a layer column - normally helps to understand "depth" in the hierarchy for each line) I get the proper output on my SQL server instance It's been, at least ...
Comments
Post a Comment