SQL Server Row Level Security RLS

Published
Length
3 min read

Luis Felipe Dias

All posts

You use sql server row level security rls to control which table rows users read. The database engine filters rows during each query based on the active user context. This approach keeps access rules inside the database tier instead of across separate apps. Both human engineers and AI agents follow the same data rules during every query. How sql server row level security rls works in practice Row filtering starts with how a person or service logs in to the database. A database user is the id

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 use sql server row level security rls to control which table rows users read. The database engine filters rows during each query based on the active user context.

This approach keeps access rules inside the database tier instead of across separate apps. Both human engineers and AI agents follow the same data rules during every query.

How sql server row level security rls works in practice

Row filtering starts with how a person or service logs in to the database. A database user is the identity of a login when it connects to a database. When someone needs many databases on an instance, admins create a SQL user with a login. That main login lives in the master database.

For one or a few databases, a contained database user is an option. This user has a password and does not tie to a login in master. An admin must first turn on contained databases on the server and the database. Across the platform, SQL Server supports thirteen types of database users.

Once a user logs in, the engine checks their role and applies row filters. Every query runs through a security policy before returning any table rows. If an engineer or an AI tool runs a broad read query, the filter steps in. It hides rows that do not match that user identity or session context. The person or agent only receives the rows that match their specific rule. No one needs to rewrite app queries to add extra filter clauses.

Security and operational tradeoffs of native roles and filters

Database admins often pair row rules with built-in fixed database roles. The permissions assigned to fixed database roles cannot be changed. All roles, including the public role, have basic connect rights. For example, members of the data reader role can read all data from all user tables. They can also read all user views across that database. Meanwhile, the data writer role can add, delete, or change data in all user tables.

Admins can also block access with deny roles. The deny data reader role cannot read any data from user tables and views. It cannot read metadata about those objects either. The deny data writer role cannot add, modify, or delete any table data. At the top level, the database owner role can run all setup and maintenance tasks. That owner role can even drop the database in SQL Server.

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

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Relying only on static logins, fixed roles, and row rules creates operational gaps. Teams often share a single service login across many engineers, scripts, and AI agents. When everyone shares one database user, row filters treat all of them the same way. If an allowed row contains emails, phone numbers, card numbers, or national IDs, the query returns them. Row rules only hide full rows and do not mask columns inside allowed rows. Data masking in a proxy layer rewrites matched fields in results before they leave the network. Also, managing hundreds of separate database users and filter rules across many servers takes heavy work.

Where session controls fit alongside database rules

Row filters work best when every person or bot has a clear, separate identity. Yet engineering teams need fast ways to inspect production data and fix bugs. Creating individual database users for every engineer on every server takes too much time. Shared accounts remain common for break-glass fixes and AI agents.

Adding an access layer in front of the database closes these gaps. It maps each human or agent to their real work identity before any query runs. It can also inspect query results in flight and mask sensitive fields like card numbers. You keep your row rules inside the database while adding field masking and clear audit logs outside.

Add runtime guardrails beside SQL Server

Role grants and network rules are needed setup for SQL Server. Still, an allowed user or agent tool can send a bad delete or read raw customer fields over a valid session. In practice, hoop.dev sits inline in the TDS protocol beside the database as a sidecar for your SQL Server. For every query an agent or human runs in production, hoop.dev decides whether to pass, mask, block, or escalate it for review.

With hoop.dev SQL Server controls, rules refuse banned statements before they run, hold risky queries for human review, and mask matched sensitive values in supported result columns in memory. Furthermore, hoop.dev holds SOC 2 Type II and generates evidence for HIPAA, PCI DSS, SOX, and GDPR audits. See the source on GitHub.

Frequently asked questions

What is the difference between a login and a contained database user?

A standard SQL user links to a server login stored in the master database. A contained database user has its own password inside the target database and needs no master login.

Can admins change the permissions of fixed database roles in SQL Server?

No, the permissions assigned to fixed database roles cannot be changed. Admins must combine fixed roles with custom roles or row filters to narrow down access.

Does row filtering hide sensitive columns inside a permitted row?

No, row filtering only decides if a whole row appears in the query result. If a row passes the filter, all columns in that row return unless another tool masks them.

The query runs. The emails stay hidden.