CREATE PROC GetChildNodes (@ID uniqueidentifier)
AS
BEGIN
WITH PermissionList (PermissionID, PermissionName, Level)
AS
(
SELECT ap.PermissionID, ap.PermissionName, 0 AS Level
FROM Permission AS ap
WHERE PermissionID = @ID
UNION ALL
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
END