Sql server 带大小写的SQL Where子句&;状况不佳
我有一个带有where子句的select查询。现在我必须在where子句中添加基于用户访问的附加条件。若用户并没有访问权,那个么需要在where子句中包含附加条件,否则若用户有访问权,那个么就并没有附加逻辑 例如:Sql server 带大小写的SQL Where子句&;状况不佳,sql-server,Sql Server,我有一个带有where子句的select查询。现在我必须在where子句中添加基于用户访问的附加条件。若用户并没有访问权,那个么需要在where子句中包含附加条件,否则若用户有访问权,那个么就并没有附加逻辑 例如: Select * from TableA where ID > 100 附加逻辑: 若用户无权访问管理页面,则@X=0;若用户无权访问外部页面,则@Y=0 我需要在where子句中包含以下逻辑: if (@X = 0 and @y=0) then pagecode not
Select * from TableA where ID > 100
附加逻辑:
若用户无权访问管理页面,则@X=0;若用户无权访问外部页面,则@Y=0
我需要在where子句中包含以下逻辑:
if (@X = 0 and @y=0) then
pagecode not in ('admin','external')
else if(@x=0 and @y=1) then
pagecode not in ('admin')
else if(@x=1 and @y=0) then
pagecode not in ('external')
else
no additional logic in where clause
如何在where子句中实现这个用例 这里的细节很少。ELSE很模糊,但我假设两个变量都等于1。但我们甚至不知道这些数据的数据类型,所以不能完全确定。我猜是这样的
where
(
@X = 0
and
@y = 0
and pagecode not in ('admin','external')
)
OR
(
@x = 0
and
@y = 1
and
pagecode not in ('admin')
)
OR
(
@x = 1
and
@y = 0
and
pagecode not in ('external')
)
OR
(
@x = 1
and
@y = 1
)
被警告。这种方法可能会有一些严重的性能问题。盖尔·肖(Gail Shaw)已经写了这篇文章和后续文章。您还可以阅读Erland Sommarskog的文章这里的详细信息。ELSE很模糊,但我假设两个变量都等于1。但我们甚至不知道这些数据的数据类型,所以不能完全确定。我猜是这样的
where
(
@X = 0
and
@y = 0
and pagecode not in ('admin','external')
)
OR
(
@x = 0
and
@y = 1
and
pagecode not in ('admin')
)
OR
(
@x = 1
and
@y = 0
and
pagecode not in ('external')
)
OR
(
@x = 1
and
@y = 1
)
被警告。这种方法可能会有一些严重的性能问题。盖尔·肖(Gail Shaw)已经写了这篇文章和后续文章。您还可以阅读Erland Sommarskog的文章,可能是这样的:
WHERE ID > 100
AND ((pagecode != 'admin' and X = 0) or X= 1)
AND ((pagecode != 'external' and Y = 0) or Y= 1)
也许是这样:
WHERE ID > 100
AND ((pagecode != 'admin' and X = 0) or X= 1)
AND ((pagecode != 'external' and Y = 0) or Y= 1)
既然问题问 如何在where子句中实现这个用例 我想我应该做一些实验。在发现了一些有趣的限制之后,我找到了一种方法 你应该能够适应这个成功的实验:
SELECT 'boom'
WHERE 'a' NOT IN (
SELECT CASE WHEN 1=0 THEN 'a' ELSE '' END
UNION ALL
SELECT CASE WHEN 1=1 THEN 'b' ELSE '' END
)
请注意,ELSE'
很重要。如果没有ELSE
,CASE
将为该行提供NULL
,这将使处于()
顺便说一句,这并不是我解决你实际问题的方法,但它确实回答了问题,所以如果你真的因为某种原因需要使用一个大小写表达式,这就是可以做到的方法。因为问题是这样问的
如何在where子句中实现这个用例
我想我应该做一些实验。在发现了一些有趣的限制之后,我找到了一种方法
你应该能够适应这个成功的实验:
SELECT 'boom'
WHERE 'a' NOT IN (
SELECT CASE WHEN 1=0 THEN 'a' ELSE '' END
UNION ALL
SELECT CASE WHEN 1=1 THEN 'b' ELSE '' END
)
请注意,ELSE'
很重要。如果没有ELSE
,CASE
将为该行提供NULL
,这将使处于()
顺便说一句,这不是我解决实际问题的方法,但它确实回答了问题,因此,如果出于某种原因确实需要使用CASE
表达式,这就是如何实现的方法。您可以在如下所示的情况下使用CASE:
WHERE 1=(
CASE WHEN @X = 0 and @y = 0 and pagecode not in ('admin','external') THEN 1
WHEN @x = 0 and @y = 1 and pagecode not in ('admin') THEN 1
WHEN @x = 1 and @y = 0 and pagecode not in ('external') THEN 1
ELSE 0 END
)
如果@x=1和@y=1,则不会返回任何行。
如果要在@x=1和@y=1时返回所有行
WHERE 1=(
CASE WHEN @X = 0 and @y = 0 and pagecode not in ('admin','external') THEN 1
WHEN @x = 0 and @y = 1 and pagecode not in ('admin') THEN 1
WHEN @x = 1 and @y = 0 and pagecode not in ('external') THEN 1
WHEN @x = 1 and @y = 1 THEN 1
ELSE 0 END
)
您可以在如下所示的情况下使用用例:
WHERE 1=(
CASE WHEN @X = 0 and @y = 0 and pagecode not in ('admin','external') THEN 1
WHEN @x = 0 and @y = 1 and pagecode not in ('admin') THEN 1
WHEN @x = 1 and @y = 0 and pagecode not in ('external') THEN 1
ELSE 0 END
)
如果@x=1和@y=1,则不会返回任何行。
如果要在@x=1和@y=1时返回所有行
WHERE 1=(
CASE WHEN @X = 0 and @y = 0 and pagecode not in ('admin','external') THEN 1
WHEN @x = 0 and @y = 1 and pagecode not in ('admin') THEN 1
WHEN @x = 1 and @y = 0 and pagecode not in ('external') THEN 1
WHEN @x = 1 and @y = 1 THEN 1
ELSE 0 END
)
“这可能是一个严重的性能问题。请参阅我上面评论中Gail Shaw的文章。但这绝对是处理此类问题的一种非常常见的方式。”@TimBiegeleisen-touche。我将把这些链接添加到我的答案中。这绝对证明了它的普遍性D“这可能是一个严重的性能问题。请参阅我上面评论中Gail Shaw的文章。但这肯定是处理此类问题的一种非常常见的方法。”@TimBiegeleisen-touche。我将把这些链接添加到我的答案中。这绝对证明了它的普遍性dw为什么需要使用case
执行此操作?为什么需要使用case
执行此操作?