SQL Server Row Level Security with Mapping Table

Published
Length
3 min read

Luis Felipe Dias

All posts

You set up sql server row level security with mapping table to filter rows by user identity. The database checks a lookup table on each query and returns matching records. Data teams use this pattern to share one table across many people or tenants. Each person logs in and sees only the rows linked to their account. How sql server row level security with mapping table works in practice A database user represents the identity of a login inside a database. Teams can use a login in master for p

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

You set up sql server row level security with mapping table to filter rows by user identity. The database checks a lookup table on each query and returns matching records.

Data teams use this pattern to share one table across many people or tenants. Each person logs in and sees only the rows linked to their account.

How sql server row level security with mapping table works in practice

A database user represents the identity of a login inside a database. Teams can use a login in master for people who need many databases. They can also use a contained database user with a password for one database. An admin must enable contained databases on the server and database first. SQL Server supports 13 types of database users in total.

Once users exist, engineers build a mapping table to pair users with data keys. This lookup table might pair a user name with a region or tenant ID. A security policy then runs a check function on every query against the main table. The function joins the main table to the mapping table using the active user identity. If the user matches a row in the mapping table, the query returns that data.

This setup helps both human engineers and AI agents query data safely. When an AI agent runs a query, the database filters rows using its user identity. The agent or engineer does not need to write complex filter logic in every query. The database engine handles the row checks before sending results back to the caller.

Security tradeoffs of static roles and mapping tables

Database admins often combine row filters with fixed database roles to manage table rights. The permissions assigned to fixed database roles cannot be changed. For example, members of the db_datareader role can read all data from all user tables. They can also read all views in that database. Members of db_datawriter can add, delete, or change data in all user tables.

Row-Level Security + Server-Sent Events Security: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To block access, admins use deny roles on specific accounts. Members of db_denydatareader cannot read any data from user tables and views. Members of db_denydatawriter cannot add, modify, or delete data in user tables. At the top level, db_owner can run all maintenance tasks and drop the database. All roles, including the public role, have the permission to connect.

Still, static roles and mapping tables leave big gaps during an active session. Many apps, scripts, and AI agents share a single database login to pool connections. When twenty engineers share one login, the mapping table sees only one user identity. You lose per-person tracking in your logs and must update the mapping table often.

Row filters also return whole rows when a user has a match in the table. If an allowed row holds emails, phone numbers, card numbers, or national IDs, everything shows up. Row rules alone do not mask fields inside the allowed rows. Teams also lack a step to approve risky queries before they run in production.

Govern SQL Server queries and responses in flight

Table grants decide which SQL Server objects a caller may touch. They do not check query intent or rewrite sensitive values in results across every client and agent. By design, hoop.dev sits inline in the TDS wire protocol next to SQL Server as a sidecar for your SQL Server. On every production batch from an engineer or AI agent, hoop.dev decides to pass, mask, block, or hold it for approval.

With hoop.dev guardrails and hoop.dev data masking, banned statements are refused or held for review, matched sensitive fields in supported result columns are masked in memory, and each decision is logged. Check out the code on GitHub.

Frequently asked questions

When should I use a mapping table for row filtering?

Use a mapping table when many users need access to different slices of one table. It works well when user assignments change often and live in a central lookup table.

Does the db_datareader role bypass row level security?

Members of db_datareader can read all user tables and views, but row policies still apply. The database checks your mapping table function before returning rows to the reader.

Can row filters hide sensitive columns like card numbers?

No, row filtering only controls which horizontal rows appear in the query result. To protect emails, phone numbers, card numbers, or national IDs in allowed rows, use data masking.

The query runs. The emails stay hidden.