Skill 2.1.3

Design and implement Row-Level Security (RLS)

Key Concepts

RLS uses a predicate function, a schema-bound inline table-valued function that returns a row when access is allowed, for example when @SalesRepId equals SESSION_CONTEXT(N'UserId') or the user name. CREATE SECURITY POLICY applies it to tables as a filter predicate, which silently removes rows from SELECT, UPDATE and DELETE, or as a block predicate, which rejects writes that violate the rule: AFTER INSERT, AFTER UPDATE, BEFORE UPDATE and BEFORE DELETE.

Applications that connect with one shared identity, such as a web app or Data API builder, set the end user's identity with sp_set_session_context (DAB can pass user claims this way), and the key should be set read-only so later code cannot change it. Predicate functions should be simple and supported by indexes because they run for every query. Users with permissions to alter the policy or the function can bypass RLS, and side-channel attacks such as divide-by-zero errors can reveal values, so RLS works alongside least-privilege permissions.