I have a little problem with this recursive CTE, it works fine, except when I have a user without root access, there is no record for this item. Therefore, if I run this request with a user with rights only on leaves inside the tree, part of the level of this request will not work correctly.
It will show a real level hierarchy, for example 6, but its top first readable element is for it, so it should be 1.
WITH Tree AS ( SELECT id, parent, 0 AS Level, id AS Root, CAST(id AS VARCHAR(MAX)) AS Sort, user_id FROM SourceTable WHERE parent IS NULL UNION ALL SELECT st.id, st.parent, Level + 1 AS Level, st.parent AS Root, uh.sort + '/' + CAST(st.id AS VARCHAR(20)) AS Sort, st.user_id FROM SourceTable AS st JOIN Tree uh ON uh.id = st.parent ) SELECT * FROM Tree AS t JOIN UserTable AS ut ON ut.id = t.user_id AND ut.user_id = '141F-4BC6-8934' ORDER BY Sort
the level is as follows
id level 5 0 2 1 7 2 4 2 1 2 6 1 3 2 8 2 9 3
When the user now has read permissions only for identifiers 8 and 9, the CTE level remains equal to 2 for identifiers 8 and 3 for identifier 9, but I need for level 8 level 1 if it is not up to
source share