| Author |
Topic |
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2009-09-29 : 07:44:25
|
| I have a table that has the following columnsorganizationidorganization_parentorganization_nameI want to loop through all of the items in there and bring back the parent child relationship likegreat grand parent / grand parent / parent / childI dont know how many levels deep it will go, but as long as organization_parent isnt null, then keep looping until you have them all.Is this do able?Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 07:47:08
|
| make use of recursive cte methodhttp://www.mssqltips.com/tip.asp?tip=1520 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 07:50:52
|
for your case, it will be something like:-;With Org_CTE(ID,Name,Path)AS(SELECT organizationid,organization_name,organization_nameFROM TableWHERE organization_parent IS NULLUNION ALLSELECT t.organizationid,t.organization_name,c.Path + '/' + t.organization_nameFROM Table tINNER JOIN Org_CTE cON c.organizationid=t.organization_parent)SELECT * from Org_CTEOPTION (MAXRECURSION 0) |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2009-09-29 : 08:32:33
|
| I tried this and its just not working, it is telling me that the "types dont match between then ancor and the recursive part in the column organization_sname of recursive query org_ctewith org_cteasselect |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2009-09-29 : 08:35:53
|
| I keep getting an errortypes dont atch between the ancor and the recursive part in the column organization_sname of recursive query org cteWith Org_CTEAS(SELECT organization_id , organization_nameFROM organizationsWHERE organization_parent IS NULLUNION ALLSELECT t.organizationid , t.organization_name + '/' + t.organization_nameFROM organizations tINNER JOIN Org_CTE c ON c.organization_id = t.organization_parent)SELECT * from Org_CTEOPTION (MAXRECURSION 0) |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 08:49:39
|
| please try to run query as i gave you...you're missing a column in first select |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2009-09-29 : 09:53:36
|
| this gives me "invalid column name 'organization_id'"With Org_CTE(id, name, path)AS(SELECT organization_id, organization_sname, organization_snameFROM organizationsWHERE organization_parent IS NULLUNION ALLSELECT t.organizationid, t.organization_sname, c.path + '/' + t.organization_nameFROM organizations tINNER JOIN Org_CTE c ON c.organization_id = t.organization_parent)SELECT * from Org_CTEOPTION (MAXRECURSION 0) |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2009-09-29 : 10:20:00
|
| i changed the join toinner join org_cte c on c.id = t.orgaization_parentand now I am back toTypes don't match between the anchor and the recursive part incolumn "path" of the query "org_cte" |
 |
|
|
HalaszJ
Yak Posting Veteran
59 Posts |
Posted - 2009-09-29 : 10:41:50
|
| I fixed it, thanks for the help |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-09-29 : 13:01:55
|
| welcome |
 |
|
|
|