在Child之前添加Parent,我想要Parent然后它的子..它基于以下过程中的MenuID

时间:2018-06-06 14:17:55

标签: mysql sql stored-procedures oracle11g

我正在尝试获取数据库父子关系,但面临我正在尝试的以下代码的问题:

ALTER PROCEDURE [dhl1adm].[CompileUserMenu]
@UserID numeric(18,0)
AS
BEGIN
SET NOCOUNT ON;

DECLARE @RoleID numeric (18,0)


BEGIN

    declare @SWV_cu CURSOR
    declare @SWV_ru CURSOR
    if EXISTS(SELECT * FROM tempdb.dbo.sysobjects where id = object_id(N'tempdb..#SWTmp_usermenu') and xtype = 'U')
    begin
        DROP TABLE #SWTmp_usermenu
    end


    CREATE TABLE #SWTmp_usermenu
    (
    MenuID                  numeric(18,0)
    ,ParentID               numeric(18,0)
    ,Icon                   nvarchar(50)
    ,MenuHeader             nvarchar(50)
    ,[Page]                 nvarchar(255) 
    ,[Enabled]              bit
    ,HideIfNotAuthorized    bit
    ,hasChildren            bit
    ,isFirstChild           bit
    ,isLastChild            bit
    )
    DECLARE @MenuID                 numeric(18,0)
    DECLARE @ParentID               numeric(18,0)
    DECLARE @Icon                   nvarchar(50)
    DECLARE @MenuHeader             nvarchar(50)
    DECLARE @Page                   nvarchar(255) 
    DECLARE @Enabled                bit
    DECLARE @HideIfNotAuthorized    bit
    DECLARE @hasChildren            bit
    DECLARE @isFirstChild           bit
    DECLARE @isLastChild            bit

    SET @SWV_ru = CURSOR FOR SELECT dhl1adm.UserDetailRoles.RoleID FROM dhl1adm.UserDetailRoles WHERE dhl1adm.UserDetailRoles.UserID=@UserID
    OPEN @SWV_ru    
    FETCH NEXT FROM @SWV_ru INTO @MenuID
    while @@FETCH_STATUS = 0
    BEGIN
    SET @SWV_cu = CURSOR FOR SELECT     dhl1adm.Menu.MenuID,   dhl1adm.Menu.ParentID, dhl1adm.Menu.Icon, dhl1adm.Menu.MenuHeader, dhl1adm.Menu.Page, dhl1adm.Menu.Enabled, dhl1adm.Menu.HideIfNotAuthorized
    FROM            dhl1adm.Menu INNER JOIN
                             dhl1adm.MenuRole ON dhl1adm.Menu.MenuID = dhl1adm.MenuRole.MenuID
    WHERE        (dhl1adm.MenuRole.RoleID = @RoleID) 
    ORDER BY dhl1adm.Menu.SortOrder 

    OPEN @SWV_cu
    FETCH NEXT FROM @SWV_cu INTO @MenuID, @ParentID, @Icon, @MenuHeader, @Page, @Enabled, @HideIfNotAuthorized
       while @@FETCH_STATUS = 0
       begin

        DECLARE @RecCount int
          SELECT @RecCount = COUNT(*) FROM dhl1adm.Menu INNER JOIN
                             dhl1adm.MenuRole ON dhl1adm.Menu.MenuID = dhl1adm.MenuRole.MenuID
                WHERE        (dhl1adm.MenuRole.RoleID = @RoleID) AND dhl1adm.Menu.ParentID = @MenuID


        IF @RecCount > 0 BEGIN  SET @hasChildren = -1 END ELSE BEGIN SET @hasChildren = 0 END

        DECLARE @FirstID    numeric(18,0)
        SELECT  TOP 1  @FirstID = dhl1adm.Menu.MenuID from dhl1adm.Menu INNER JOIN
                             dhl1adm.MenuRole ON dhl1adm.Menu.MenuID = dhl1adm.MenuRole.MenuID
                WHERE        (dhl1adm.MenuRole.RoleID = @RoleID) AND dhl1adm.Menu.ParentID = @MenuID
                ORDER BY dhl1adm.Menu.ParentID , dhl1adm.Menu.SortOrder 

        IF @FirstID = @MenuID  BEGIN SET @isFirstChild =  -1 END ELSE BEGIN SET @isFirstChild = 0 END

        DECLARE @LastID     numeric(18,0)
        SELECT  TOP 1  @LastID = dhl1adm.Menu.MenuID from dhl1adm.Menu INNER JOIN
                             dhl1adm.MenuRole ON dhl1adm.Menu.MenuID = dhl1adm.MenuRole.MenuID
                WHERE        (dhl1adm.MenuRole.RoleID = @RoleID) AND dhl1adm.Menu.ParentID = @MenuID
                ORDER BY dhl1adm.Menu.ParentID desc , dhl1adm.Menu.SortOrder desc

         IF @LastID = @MenuID  BEGIN SET @isLastChild = -1 END ELSE BEGIN SET @isLastChild = 0 END

         DECLARE @RecCountUnique int
         SELECT @RecCountUnique = COUNT(*) from #SWTmp_usermenu where MenuHeader = @MenuHeader
         IF @RecCountUnique > 0
         BEGIN
          FETCH NEXT FROM @SWV_cu INTO @MenuID, @ParentID, @Icon, @MenuHeader, @Page, @Enabled, @HideIfNotAuthorized
         END
         ELSE
         BEGIN
         INSERT INTO #SWTmp_usermenu VALUES(@MenuID, @ParentID, @Icon, @MenuHeader, @Page, @Enabled, @HideIfNotAuthorized, @hasChildren, @isFirstChild, @isLastChild)
         FETCH NEXT FROM @SWV_cu INTO @MenuID, @ParentID, @Icon, @MenuHeader, @Page, @Enabled, @HideIfNotAuthorized
         END 
       end

        FETCH NEXT FROM @SWV_ru INTO @RoleID
       END
       CLOSE @SWV_cu
       CLOSE @SWV_ru

       select  MenuID, ParentID, Icon, MenuHeader, [Page],[Enabled],HideIfNotAuthorized,hasChildren,isFirstChild,isLastChild FROM #SWTmp_usermenu  
END
END

我想:

家长ID:     孩子1     孩子2     Child3 ParentID2

现在,如果我添加一个父ID,它将显示格式: ParentID1 ParentID2 然后是ParentID1的孩子 但如果它应该是 ParentID 1 Child1 \的child2 ParentId2

0 个答案:

没有答案