Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 August SQL Server 2016: Row Level Security by Example

SQL Server 2016: Row Level Security by Example

Postgre SQL Row Level Security

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

In this post, I am going to publish about The Row Level Security which is now a common feature of all new version of RDBMSs like SQL Server 2016, PostgreSQL 9.5.

With the concept of Row Level Security, we can restrict the user for a particular set of records.
Previously, we are doing this kind of restriction using by creating filtered VIEWS and assign related VIEWS to relevant Users.

SQL Server 2016 has this Row Level Security feature which extends the overall security policy of the SQL Server.

PostgreSQL 9.5: Row Level Security by Example

Please check the below full demonstration on this.

Create a test database:

1
2
3
4
CREATE DATABASE CheckRowSecurity;
GO
USE CheckRowSecurity;
GO

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
CREATE TABLE tbl_employeeSalary
(
empcode VARCHAR(10)
,empname VARCHAR(10)
,empsalary BIGINT
,[month] INT
)
GO
INSERT INTO tbl_employeeSalary
VALUES
('Anv','Anvesh',150000,1)
,('Anv','Anvesh',120000,2)
,('Anv','Anvesh',160000,3)
,('Anv','Anvesh',170000,4)
,('Mar','Martin',90000,1)
,('Mar','Martin',100000,2)
,('Mar','Martin',120000,3)
,('Mar','Martin',130000,4)
GO

Create two test users and assign SELECT permission on the table:

1
2
3
4
5
6
7
CREATE USER Anv WITHOUT LOGIN;
CREATE USER Mar WITHOUT LOGIN;
GO
 
GRANT SELECT ON tbl_employeeSalary TO Anv
GRANT SELECT ON tbl_employeeSalary TO Mar
GO

Create filter predicate function:

1
2
3
4
5
6
7
CREATE FUNCTION rowLevelSecurity(@userName as sysname)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN SELECT 1 AS rowLevelSecurityResult
WHERE @userName = USER_NAME();
GO

Add filter predicate to the table:

1
2
3
4
5
CREATE SECURITY POLICY UserFilter
ADD FILTER PREDICATE dbo.rowLevelSecurity(empcode)
ON dbo.tbl_employeeSalary
WITH (STATE = ON);
GO

Select table without any user:
You will get a blank result set because we alter the security policy of this table.

1
SELECT * FROM tbl_employeeSalary

Check for User ‘Anv’:

1
2
3
4
EXECUTE AS USER = 'Anv'
SELECT * FROM tbl_employeeSalary
REVERT
GO

Result:

1
2
3
4
5
6
empcode empname empsalary month
---------- ---------- -------------------- -----------
Anv Anvesh 150000 1
Anv Anvesh 120000 2
Anv Anvesh 160000 3
Anv Anvesh 170000 4

Check for User ‘Mar’:

1
2
3
4
EXECUTE AS USER = 'Mar'
SELECT * FROM tbl_employeeSalary
REVERT
GO

Result:

1
2
3
4
5
6
empcode empname empsalary month
---------- ---------- -------------------- -----------
Mar Martin 90000 1
Mar Martin 100000 2
Mar Martin 120000 3
Mar Martin 130000 4

Aug 16, 2017Anvesh Patel
SQL Server Interview: Have you ever created a Temporary Stored Procedure or Function?SQL Server 2012: Introduced the new form of TRY CATCH Exception Handling using THROW
Comments: 1
  1. ravendra
    September 11, 2017 at 6:23 pm

    Hi Anvesh, Thanks for sharing the Knowledge to everywhere…these topics are very helpful to everyone.

    ReplyCancel

Leave a Reply to ravendra Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

ImageAugust 16, 2017 1 Comment SQL ServerAnvesh Patel, database, database research and development, dbrnd, FILTER PREDICAT, Row level scrity, security, SQL Query, SQL Server, SQL Server 2016, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, TSQL
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....