0

階層的な役割の定義をサポートするように、デフォルトの ASP.NET RoleProvider に次の調整を実装しようとしています。ただし、次の関数を作成できません。保持されExecuting the functionます...

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

この関数の何が問題になっていますか?

    -- ================================================
    -- 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
4

2 に答える 2

1

問題は CTE の構造にあると思います。Union クエリの前半は親を表し、後半は子を返す必要があります。つまり、階層を上るのではなく下に歩いています。したがって、Union クエリの後半の On 句を次のように変更します。

aspnet_Roles_CTE.RoleId = aspnet_Roles.ParentRoleId.

編集

いくつかのサンプルデータが役立ちます。ここに私が作った小さなテストがあります:

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

結果:

アプリケーション ID | 役割 ID | ロール名 | 親ロール ID | 階層レベル
1 | 1 | 親の役割 A | ヌル | 1
1 | 4 | 子の役割 AA | 1 | 2
1 | 5 | 子役 AB | 1 | 2
1 | 6 | 子の役割 AC | 1 | 2
1 | 7 | 子役 AAA | 4 | 3
1 | 8 | 子の役割 AAB | 4 | 3
1 | 9 | 子の役割 AAC | 4 | 3
于 2011-02-11T18:07:09.270 に答える
0

CTEには再帰の枯渇制限があることは知っていますが、子の親の子、つまり循環関係があるのではないでしょうか。

于 2011-02-11T17:16:01.293 に答える