Click here to Skip to main content
11,632,260 members (89,068 online)
Click here to Skip to main content

Tagged as

SQL Server Recursive Fetch From Self Referencing Table

, 19 Nov 2009 CPOL 8.4K 6
Rate this:
Please Sign up or sign in to vote.
CREATE PROC GetChildNodes (@ID uniqueidentifier)ASBEGINWITH PermissionList (PermissionID, PermissionName, Level)AS(SELECT ap.PermissionID, ap.PermissionName, 0 AS LevelFROM Permission AS apWHERE PermissionID = @IDUNION ALLSELECT ap.PermissionID, ap.PermissionName, Level + 1FROM
CREATE PROC GetChildNodes (@ID uniqueidentifier)
WITH PermissionList (PermissionID, PermissionName, Level)
SELECT ap.PermissionID, ap.PermissionName, 0 AS Level
FROM Permission AS ap
WHERE PermissionID = @ID
SELECT ap.PermissionID, ap.PermissionName, Level + 1
FROM Permission AS ap
INNER JOIN PermissionList AS pl
ON ap.ParentPermissionID = pl.PermissionID

SELECT PermissionID, PermissionName, Level
FROM PermissionList 



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Anish M
Web Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150728.1 | Last Updated 19 Nov 2009
Article Copyright 2009 by Anish M
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid