1

我正在制作一个 rdl 报告,并且我有三个复选框,如果选中,则需要更改我的 WHERE 语句。如果未选中,则结果应仅按日期范围匹配。如果选中一个或多个,则需要返回与变量对应的字符串匹配的字段。

WHERE 
(EffectiveDate BETWEEN @StartDate AND @EndDate) 
AND (((@IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR @IncludeSEWPrefix = 0)
AND ((@IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR @IncludePAWPrefix = 0)
AND ((@IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR @IncludeRPLPrefix = 0)) 

到目前为止,我的代码在没有选中和选中一个时有效,但在选中多个复选框时不返回任何内容。所以为了尝试解决这个问题,我将代码更改为

WHERE 
(EffectiveDate BETWEEN @StartDate AND @EndDate) 
AND ((((@IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR @IncludeSEWPrefix = 0)
OR ((@IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR @IncludePAWPrefix = 0)
OR ((@IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR @IncludeRPLPrefix = 0)))

这导致无论选择什么都会返回所有行。有人可以告诉我哪里出错了吗?

4

3 回答 3

1

我相信这是正确的重新排列。比最初出现的更棘手的问题。((@IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR @IncludeSEWPrefix = 0)问题是像with这样的分隔线AND意味着如果两个包含为真,则一行需要同时包含两个PrefixId',这是不可能的。如果你用 分隔它们OR,那么只有一个包含 false,意味着每一行都会通过。因此,请检查一行是否包含任何包含的前缀,否则所有包含都必须关闭。

WHERE EffectiveDate BETWEEN @StartDate AND @EndDate
AND 
(
    (@IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR
    (@IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR 
    (@IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR
    (@IncludeSEWPrefix = 0 AND @IncludePAWPrefix = 0 AND @IncludeRPLPrefix = 0)
)
于 2012-07-17T12:38:52.707 回答
0

试试这个

WHERE  
(EffectiveDate BETWEEN @StartDate AND @EndDate)  
AND 
(
(@IncludeSEWPrefix = 1 AND PrefixId = 'SEW' OR @IncludeSEWPrefix = 0) AND 
(@IncludePAWPrefix = 1 AND PrefixId = 'PAW' OR @IncludePAWPrefix = 0) AND 
(@IncludeRPLPrefix = 1 AND PrefixId = 'RPL' OR @IncludeRPLPrefix = 0)
)  
于 2012-07-17T10:23:34.307 回答
0

你有比需要更多的括号,它不会伤害,只是要注意。也许这可以帮助:

WHERE (EffectiveDate BETWEEN @StartDate AND @EndDate) 
    AND (   ((@IncludeSEWPrefix = 1 AND PrefixId = 'SEW') OR (@IncludeSEWPrefix = 0 AND @PrefixId <> 'SEW'))
        OR ((@IncludePAWPrefix = 1 AND PrefixId = 'PAW') OR (@IncludePAWPrefix = 0 AND @PrefixId <> 'PAW'))
        OR ((@IncludeRPLPrefix = 1 AND PrefixId = 'RPL') OR (@IncludeRPLPrefix = 0 AND @PrefixId <> 'RPL'))
        )
于 2012-07-17T10:35:22.117 回答