An Azure relational database service.
Row-Level Security (RLS) in Azure SQL Database enables you to restrict access to specific rows in a table based on the executing user's identity, role membership, or application context. RLS is enforced by the database engine, providing centralized security without requiring changes to application queries.
Implementation Steps
1. Create a predicate function
The predicate function determines which rows a user can access.
CREATE SCHEMA Security;
GO
CREATE FUNCTION Security.fn_salesrep_access(@SalesRep sysname)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN
(
SELECT 1 AS access_granted
WHERE @SalesRep = USER_NAME()
OR IS_ROLEMEMBER('ManagerRole') = 1
);
GO
2. Create a security policy
Attach the predicate function to the target table.
CREATE SECURITY POLICY SalesAccessPolicy
ADD FILTER PREDICATE Security.fn_salesrep_access(SalesRep)
ON dbo.Sales
WITH (STATE = ON);
GO
Once enabled, queries against dbo.Sales automatically return only rows permitted by the predicate.
3. Test the policy
EXECUTE AS USER = 'SalesRep1';
SELECT * FROM dbo.Sales;
REVERT;
A sales representative sees only their own rows, while members of ManagerRole can view all rows.
FILTER vs BLOCK Predicates
FILTER predicates silently filter rows from query results.
BLOCK predicates prevent unauthorized INSERT, UPDATE, or DELETE operations.
Multi-Tenant Applications
For SaaS applications, Microsoft commonly recommends using SESSION_CONTEXT() to store tenant information and enforce tenant-level isolation through RLS.
References
https://learn.microsoft.com/sql/relational-databases/security/row-level-security
https://learn.microsoft.com/sql/t-sql/statements/create-security-policy-transact-sql
https://learn.microsoft.com/azure/azure-sql/database/security-overview
Please let us know if you have any questions.