Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3941800
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:35:11+00:00 2026-05-20T00:35:11+00:00

I’m trying to implement the following adjustments to the default ASP.NET RoleProvider so that

  • 0

I’m trying to implement the following adjustments to the default ASP.NET RoleProvider so that it supports hierarchical role definitions. However i cannot create the following function, it keeps Executing the function…

Ref: http://mark.tremaine.net/howto/hierarchical-sql-role-provider/

What is wrong with this function?

    -- ================================================
    -- Template generated from Template Explorer using:
    -- Create Multi-Statement Function (New Menu).SQL
    --
    -- Use the Specify Values for Template Parameters 
    -- command (Ctrl-Shift-M) to fill in the parameter 
    -- values below.
    --
    -- This block of comments will not be included in
    -- the definition of the function.
    -- ================================================
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:      <Author,,Name>
    -- Create date: <Create Date,,>
    -- Description: <Description,,>
    -- =============================================


     CREATE FUNCTION [dbo].[aspnet_Roles_Ancestor_TVF] (
     @RoleId uniqueidentifier
     )
     RETURNS
     @aspnet_Roles TABLE (
     ApplicationId uniqueidentifier
     , RoleId uniqueidentifier
     , RoleName nvarchar(256)
     , LoweredRoleName nvarchar(256)
     , Description nvarchar(256)
     , ParentRoleId uniqueidentifier
     )
     AS
     BEGIN
     ; WITH aspnet_Roles_CTE (
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     , HierarchyLevel
     ) AS (
     SELECT
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     , 1 AS HierarchyLevel
     FROM aspnet_Roles
     WHERE RoleId = @RoleId

     UNION ALL

     SELECT
     aspnet_Roles.ApplicationId
     , aspnet_Roles.RoleId
     , aspnet_Roles.RoleName
     , aspnet_Roles.LoweredRoleName
     , aspnet_Roles.Description
     , aspnet_Roles.ParentRoleId
     , aspnet_Roles_CTE.HierarchyLevel + 1 AS HierarchyLevel
     FROM aspnet_Roles
     INNER JOIN aspnet_Roles_CTE
     ON aspnet_Roles.RoleId = aspnet_Roles_CTE.ParentRoleId
     )

     INSERT INTO @aspnet_Roles (
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     )
     SELECT
     ApplicationId
     , RoleId
     , RoleName
     , LoweredRoleName
     , Description
     , ParentRoleId
     FROM aspnet_Roles_CTE
     ORDER BY HierarchyLevel

     RETURN
     END
    GO
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:35:11+00:00Added an answer on May 20, 2026 at 12:35 am

    I believe the issue is in the structure of your CTE. The first half of the Union query should represent the parent and the second should return the children. I.e, you are walking down the hierarchy not up it. Thus, I would change the On clause in the second half of the Union query to:

    aspnet_Roles_CTE.RoleId = aspnet_Roles.ParentRoleId.

    Edit

    Some sample data would help. Here’s a small test I whipped up:

    Declare @RoleId int;
    Set @RoleId = 1;
    
    With aspnet_Roles As
        (
        Select 1 As ApplicationId, 1 As RoleId, 'Parent Role A' As RoleName, Null As ParentRoleId
        Union All Select 1, 2, 'Parent Role B', Null
        Union All Select 1, 3, 'Parent Role C', Null
        Union All Select 1, 4, 'Child Role A-A', 1
        Union All Select 1, 5, 'Child Role A-B', 1
        Union All Select 1, 6, 'Child Role A-C', 1
        Union All Select 1, 7, 'Child Role A-A-A', 4
        Union All Select 1, 8, 'Child Role A-A-B', 4
        Union All Select 1, 9, 'Child Role A-A-C', 4
        )
        , aspnet_Roles_CTE ( ApplicationId, RoleId, RoleName, ParentRoleId, HierarchyLevel ) As
        (
        Select ApplicationId, RoleId, RoleName, ParentRoleId, 1 AS HierarchyLevel
        From aspnet_Roles
        Where RoleId = @RoleId
        Union All
        Select AR.ApplicationId, AR.RoleId, AR.RoleName, AR.ParentRoleId, HierarchyLevel + 1
        From aspnet_Roles As AR
            Join aspnet_Roles_CTE As CTE
                On CTE.ApplicationId = AR.ApplicationId
                    And CTE.RoleId = AR.ParentRoleId
         )
    Select ApplicationId, RoleId, RoleName, ParentRoleId, HierarchyLevel
    From aspnet_Roles_CTE
    

    Results:

    ApplicationId | RoleId | RoleName         | ParentRoleId | HierarchyLevel
    1             | 1      | Parent Role A    | NULL         | 1
    1             | 4      | Child Role A-A   | 1            | 2
    1             | 5      | Child Role A-B   | 1            | 2
    1             | 6      | Child Role A-C   | 1            | 2
    1             | 7      | Child Role A-A-A | 4            | 3
    1             | 8      | Child Role A-A-B | 4            | 3
    1             | 9      | Child Role A-A-C | 4            | 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.