繁体   English   中英

基于多行安全性的SQL筛选器

[英]SQL Filter based on Multi-Row Security

我们目前正在使用MS SQL Server 2005,但很快将迁移到2012(或2014)。 我正面临以下情况。

数据表(数据)

id   | location | group  |  level
-----+----------+--------+---------
1    | loc1     |   A    |    1
2    | loc1     |   B    |    5
3    | loc1     |   A    |    9
4    | loc1     |   A    |   12
5    | loc2     |   A    |    1
6    | loc2     |   B    |    5

和这个安全表(安全性)

user | location | group | level from | level to
-----+----------+-------+------------+----------
u1   | loc1     |   A   |    1       |    5
u1   | loc1     |   A   |   10       |   15
u1   | loc2     |       |    4       |    9

逻辑是将安全性表中每一行的每个值都与“和”(如果不为空)连接,而每一行则与“或”连接。

目前,我以简单的字符串形式在存储的proc中动态构建where子句。 对于为单个用户定义的少量安全行,这可以很好地工作,但是将需要5000多个行。

例:

select id from data 
where location = 'loc1' and and group='A' and level>=1 and level <=5
location = 'loc1' and group='A' and level>=10 and level <=15
or location = 'loc2' and level>=4 and level <=9

-> ID:1、4、5、6

我看不到在SQL方面以一种聪明的方式执行此操作的任何方法。 有人有建议吗?

Select distinct id from data
Inner join security on data.location = security.location 
And data.group = security.group
And data.level between security.[level from] and [level to]

独特的部分是为了防止重复的结果从查询中发出

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM