Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
amodi
Yak Posting Veteran
83 Posts |
Posted - 2009-08-04 : 07:58:00
|
Hello Friends,I have a table that maintains heirarchy. When input is passed,its first child should be the output. Table data is as follows:pageId-------parentId46------------185-------------466-------------57-------------68-------------69-------------610------------611------------612------------613------------614------------6For example: when the input is 46, the output should be 7. ( 46 has childern, its 1st child is 6. 6 has children, its first child is 7 ) I need something like recursive function.Thanks a million. |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-04 : 08:10:14
|
See Books Online for RECURSIVE CTE (Common Table Expression). N 56°04'39.26"E 12°55'05.63" |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-08-04 : 08:14:31
|
[code]DECLARE @Sample TABLE ( PageID INT, ParentID INT )INSERT @SampleSELECT 46, 18 UNION ALLSELECT 5, 46 UNION ALLSELECT 6, 5 UNION ALLSELECT 7, 6 UNION ALLSELECT 8, 6 UNION ALLSELECT 9, 6 UNION ALLSELECT 10, 6 UNION ALLSELECT 11, 6 UNION ALLSELECT 12, 6 UNION ALLSELECT 13, 6 UNION ALLSELECT 14, 6DECLARE @NodeID INTSET @NodeID = 18;WITH Yak(PageID)AS ( SELECT PageID FROM @Sample WHERE ParentID = @NodeID UNION ALL SELECT s.PageID FROM Yak AS y INNER JOIN @Sample AS s ON s.ParentID = y.PageID)SELECT PageIDFROM Yak[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
amodi
Yak Posting Veteran
83 Posts |
Posted - 2009-08-04 : 08:25:47
|
Thanks peso, i will check that and get back to YOU . |
 |
|
|
|
|
|
|