DECLARE @ParentMenuId INT, @ChildMenuId INT, @ViewPermissionId INT, @RoleId INT, @EditPermissionId INT, @PortalID INT
SELECT @ParentMenuId =  MenuId from personabarmenu where Identifier = '' /*Specify The Menu Identifier*/ /*RUN SELECT * FROM Personabarmenu to find identifiers*/
SELECT @ChildMenuId =  MenuId from personabarmenu where Identifier = '' /*Specify The Menu Identifier*/
SELECT @ViewPermissionId = PermissionId from PersonaBarPermission where PermissionKey = 'VIEW' aND MenuId IS NULL
SELECT @EditPermissionId = PermissionId from PersonaBarPermission where PermissionKey = 'EDIT' aND MenuId IS NULL
SELECT @RoleID = RoleId FROM Roles WHERE RoleName = '' /* Specify the name of the role */
SET @PortalID = # /*Specify the PortalID */
IF @ViewPermissionId IS NULL
BEGIN
       PRINT 'View Permission not defined'
END
IF @EditPermissionId IS NULL
BEGIN
       PRINT 'Edit Permission not defined'
END
  
--Add View permissions for Parent menu
IF NOT EXISTS (SELECT * FROM personabarmenuPermission WHERE MenuId = @ParentMenuId and RoleID = @RoleID AND PermissionID = @ViewPermissionId AND PortalId = @PortalID)
BEGIN
       INSERT INTO [dbo].[PersonaBarMenuPermission]
                       ([PortalId]
                       ,[MenuId]
                       ,[PermissionId]
                       ,[AllowAccess]
                       ,[RoleId]
                       ,[UserId]
                       ,[CreatedByUserId]
                       ,[CreatedOnDate]
                       ,[LastModifiedByUserId]
                       ,[LastModifiedOnDate])
              VALUES
                       (@PortalID
                       ,@ParentMenuId
                       ,@ViewPermissionId
                       ,1
                       ,@RoleID
                       ,null
                       ,null
                       ,GetDate()
                       ,null
                       ,GetDate())
END

--Add Edit permissions for Parent menu
IF NOT EXISTS (SELECT * FROM personabarmenuPermission WHERE MenuId = @ParentMenuId and RoleID = @RoleID AND PermissionID = @EditPermissionId AND PortalId = @PortalID)
BEGIN
       INSERT INTO [dbo].[PersonaBarMenuPermission]
                       ([PortalId]
                       ,[MenuId]
                       ,[PermissionId]
                       ,[AllowAccess]
                       ,[RoleId]
                       ,[UserId]
                       ,[CreatedByUserId]
                       ,[CreatedOnDate]
                       ,[LastModifiedByUserId]
                       ,[LastModifiedOnDate])
              VALUES
                       (@PortalID
                       ,@ParentMenuId
                       ,@EditPermissionId
                       ,1
                       ,@RoleID
                       ,null
                       ,null
                       ,GetDate()
                       ,null
                       ,GetDate())
END
  
--Add View permissions for Child menu
IF NOT EXISTS (SELECT * FROM personabarmenuPermission WHERE MenuId = @ChildMenuId and RoleID = @RoleID AND PermissionID = @ViewPermissionId AND PortalId = @PortalID)
BEGIN
       INSERT INTO [dbo].[PersonaBarMenuPermission]
                       ([PortalId]
                       ,[MenuId]
                       ,[PermissionId]
                       ,[AllowAccess]
                       ,[RoleId]
                       ,[UserId]
                       ,[CreatedByUserId]
                       ,[CreatedOnDate]
                       ,[LastModifiedByUserId]
                       ,[LastModifiedOnDate])
              VALUES
                       (@PortalID
                       ,@ChildMenuId
                       ,@ViewPermissionId
                       ,1
                       ,@RoleID
                       ,null
                       ,null
                       ,GetDate()
                       ,null
                       ,GetDate())
END

--Add Edit permissions for Child menu
IF NOT EXISTS (SELECT * FROM personabarmenuPermission WHERE MenuId = @ChildMenuId and RoleID = @RoleID AND PermissionID = @EditPermissionId AND PortalId = @PortalID)
BEGIN
       INSERT INTO [dbo].[PersonaBarMenuPermission]
                       ([PortalId]
                       ,[MenuId]
                       ,[PermissionId]
                       ,[AllowAccess]
                       ,[RoleId]
                       ,[UserId]
                       ,[CreatedByUserId]
                       ,[CreatedOnDate]
                       ,[LastModifiedByUserId]
                       ,[LastModifiedOnDate])
              VALUES
                       (@PortalID
                       ,@ChildMenuId
                       ,@EditPermissionId
                       ,1
                       ,@RoleID
                       ,null
                       ,null
                       ,GetDate()
                       ,null
                       ,GetDate())
END