在处理日期参数范围和null时,请解释SSRS SQL语句

在处理日期参数范围和null时,请解释SSRS SQL语句,sql,reporting-services,Sql,Reporting Services,有人能帮我理解SQL语句WHERE子句中的每一行是什么意思吗?有两个参数可以为null或有效日期。where子句是如何工作的?提前谢谢你 SELECT * from t1 WHERE ((@StartDate IS NULL AND @EndDate IS NULL) OR (t1.date >= @StartDate AND t1.date <= @EndDate) OR (t1.date >= @StartDate AND @EndDate IS NULL) OR (@

有人能帮我理解SQL语句WHERE子句中的每一行是什么意思吗?有两个参数可以为null或有效日期。where子句是如何工作的?提前谢谢你

SELECT * from t1

WHERE

((@StartDate IS NULL AND @EndDate IS NULL)
OR (t1.date >= @StartDate AND t1.date <= @EndDate)
OR (t1.date >= @StartDate AND @EndDate IS NULL)
OR (@StartDate IS NULL AND t1.date <= @EndDate))
从t1中选择*
哪里
(@StartDate为空,@EndDate为空)
或者(t1.date>=@StartDate和t1.date=@StartDate和@EndDate为空)

或者(@StartDate为NULL,t1.date如果其中根本没有提供范围,则返回所有条目

((@StartDate IS NULL AND @EndDate IS NULL)
或者有一个日期范围,t1.date在该范围内

OR (t1.date >= @StartDate AND t1.date <= @EndDate)
或者日期范围没有开始,而t1.date在范围结束之前

OR (@StartDate IS NULL AND t1.date <= @EndDate))
你的问题是:

如果SomeParameter等于“N”,则“and t1.name IN(name)”将附加到where子句,否则如果SomeParameter等于“Y”,则“and t1.name IN(name)或t1.name为null”)将附加到where子句。对吗

否。
WHERE
子句始终是它的本来面目。不会更改,但会对其进行评估。让我们看看它在这两种情况下是如何工作的:

案例1:SomeParameter='N'

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

=>

(TRUE and t1.name IN (Name)) OR 
(FALSE and t1.name IN (Name) OR t1.name is null)
如果(name)
中的
t1.name也为true,则记录将包括在结果集中,因为

(TRUE and TRUE) OR (FALSE AND TRUE OR FALSE) = 
 TRUE           OR (FALSE          OR FALSE) = 
 TRUE           OR  FALSE                    =
 TRUE
(TRUE and FALSE) OR (FALSE AND FALSE OR FALSE) = 
 FALSE           OR (FALSE           OR FALSE) = 
 FALSE           OR  FALSE                     =
 FALSE
如果(name)
中的
t1.name为false,则记录不包括在结果集中,因为

(TRUE and TRUE) OR (FALSE AND TRUE OR FALSE) = 
 TRUE           OR (FALSE          OR FALSE) = 
 TRUE           OR  FALSE                    =
 TRUE
(TRUE and FALSE) OR (FALSE AND FALSE OR FALSE) = 
 FALSE           OR (FALSE           OR FALSE) = 
 FALSE           OR  FALSE                     =
 FALSE
案例2:SomeParameter='Y'

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

=>

(FALSE and t1.name IN (Name)) OR 
(TRUE and t1.name IN (Name) OR t1.name is null)
如果(Name)
中的
t1.Name也为true,则记录将包括在结果集中,因为

(FALSE and TRUE) OR (TRUE AND TRUE OR FALSE) = 
 FALSE           OR (TRUE AND TRUE)          =
 FALSE           OR  TRUE                    =
 TRUE
(FALSE and FALSE) OR (TRUE AND FALSE OR FALSE) = 
 FALSE            OR (TRUE AND FALSE)          =
 FALSE            OR  FALSE                    =
 FALSE
如果(Name)
中的
t1.Name为false,则记录不包括在结果集中,因为

(FALSE and TRUE) OR (TRUE AND TRUE OR FALSE) = 
 FALSE           OR (TRUE AND TRUE)          =
 FALSE           OR  TRUE                    =
 TRUE
(FALSE and FALSE) OR (TRUE AND FALSE OR FALSE) = 
 FALSE            OR (TRUE AND FALSE)          =
 FALSE            OR  FALSE                    =
 FALSE

如果没有提供范围,则返回所有条目

((@StartDate IS NULL AND @EndDate IS NULL)
或者有一个日期范围,t1.date在该范围内

OR (t1.date >= @StartDate AND t1.date <= @EndDate)
或者日期范围没有开始,而t1.date在范围结束之前

OR (@StartDate IS NULL AND t1.date <= @EndDate))
你的问题是:

如果SomeParameter等于“N”,则“and t1.name IN(name)”将附加到where子句,否则如果SomeParameter等于“Y”,则“and t1.name IN(name)或t1.name为null”)将附加到where子句。对吗

否。
WHERE
子句始终是它的本来面目。不会更改,但会对其进行评估。让我们看看它在这两种情况下是如何工作的:

案例1:SomeParameter='N'

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

=>

(TRUE and t1.name IN (Name)) OR 
(FALSE and t1.name IN (Name) OR t1.name is null)
如果(name)
中的
t1.name也为true,则记录将包括在结果集中,因为

(TRUE and TRUE) OR (FALSE AND TRUE OR FALSE) = 
 TRUE           OR (FALSE          OR FALSE) = 
 TRUE           OR  FALSE                    =
 TRUE
(TRUE and FALSE) OR (FALSE AND FALSE OR FALSE) = 
 FALSE           OR (FALSE           OR FALSE) = 
 FALSE           OR  FALSE                     =
 FALSE
如果(name)
中的
t1.name为false,则记录不包括在结果集中,因为

(TRUE and TRUE) OR (FALSE AND TRUE OR FALSE) = 
 TRUE           OR (FALSE          OR FALSE) = 
 TRUE           OR  FALSE                    =
 TRUE
(TRUE and FALSE) OR (FALSE AND FALSE OR FALSE) = 
 FALSE           OR (FALSE           OR FALSE) = 
 FALSE           OR  FALSE                     =
 FALSE
案例2:SomeParameter='Y'

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

=>

(FALSE and t1.name IN (Name)) OR 
(TRUE and t1.name IN (Name) OR t1.name is null)
如果(Name)
中的
t1.Name也为true,则记录将包括在结果集中,因为

(FALSE and TRUE) OR (TRUE AND TRUE OR FALSE) = 
 FALSE           OR (TRUE AND TRUE)          =
 FALSE           OR  TRUE                    =
 TRUE
(FALSE and FALSE) OR (TRUE AND FALSE OR FALSE) = 
 FALSE            OR (TRUE AND FALSE)          =
 FALSE            OR  FALSE                    =
 FALSE
如果(Name)
中的
t1.Name为false,则记录不包括在结果集中,因为

(FALSE and TRUE) OR (TRUE AND TRUE OR FALSE) = 
 FALSE           OR (TRUE AND TRUE)          =
 FALSE           OR  TRUE                    =
 TRUE
(FALSE and FALSE) OR (TRUE AND FALSE OR FALSE) = 
 FALSE            OR (TRUE AND FALSE)          =
 FALSE            OR  FALSE                    =
 FALSE

如果没有提供范围,则返回所有条目

((@StartDate IS NULL AND @EndDate IS NULL)
或者有一个日期范围,t1.date在该范围内

OR (t1.date >= @StartDate AND t1.date <= @EndDate)
或者日期范围没有开始,而t1.date在范围结束之前

OR (@StartDate IS NULL AND t1.date <= @EndDate))
你的问题是:

如果SomeParameter等于“N”,则“and t1.name IN(name)”将附加到where子句,否则如果SomeParameter等于“Y”,则“and t1.name IN(name)或t1.name为null”)将附加到where子句。对吗

否。
WHERE
子句始终是它的本来面目。不会更改,但会对其进行评估。让我们看看它在这两种情况下是如何工作的:

案例1:SomeParameter='N'

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

=>

(TRUE and t1.name IN (Name)) OR 
(FALSE and t1.name IN (Name) OR t1.name is null)
如果(name)
中的
t1.name也为true,则记录将包括在结果集中,因为

(TRUE and TRUE) OR (FALSE AND TRUE OR FALSE) = 
 TRUE           OR (FALSE          OR FALSE) = 
 TRUE           OR  FALSE                    =
 TRUE
(TRUE and FALSE) OR (FALSE AND FALSE OR FALSE) = 
 FALSE           OR (FALSE           OR FALSE) = 
 FALSE           OR  FALSE                     =
 FALSE
如果(name)
中的
t1.name为false,则记录不包括在结果集中,因为

(TRUE and TRUE) OR (FALSE AND TRUE OR FALSE) = 
 TRUE           OR (FALSE          OR FALSE) = 
 TRUE           OR  FALSE                    =
 TRUE
(TRUE and FALSE) OR (FALSE AND FALSE OR FALSE) = 
 FALSE           OR (FALSE           OR FALSE) = 
 FALSE           OR  FALSE                     =
 FALSE
案例2:SomeParameter='Y'

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

=>

(FALSE and t1.name IN (Name)) OR 
(TRUE and t1.name IN (Name) OR t1.name is null)
如果(Name)
中的
t1.Name也为true,则记录将包括在结果集中,因为

(FALSE and TRUE) OR (TRUE AND TRUE OR FALSE) = 
 FALSE           OR (TRUE AND TRUE)          =
 FALSE           OR  TRUE                    =
 TRUE
(FALSE and FALSE) OR (TRUE AND FALSE OR FALSE) = 
 FALSE            OR (TRUE AND FALSE)          =
 FALSE            OR  FALSE                    =
 FALSE
如果(Name)
中的
t1.Name为false,则记录不包括在结果集中,因为

(FALSE and TRUE) OR (TRUE AND TRUE OR FALSE) = 
 FALSE           OR (TRUE AND TRUE)          =
 FALSE           OR  TRUE                    =
 TRUE
(FALSE and FALSE) OR (TRUE AND FALSE OR FALSE) = 
 FALSE            OR (TRUE AND FALSE)          =
 FALSE            OR  FALSE                    =
 FALSE

如果没有提供范围,则返回所有条目

((@StartDate IS NULL AND @EndDate IS NULL)
或者有一个日期范围,t1.date在该范围内

OR (t1.date >= @StartDate AND t1.date <= @EndDate)
或者日期范围没有开始,而t1.date在范围结束之前

OR (@StartDate IS NULL AND t1.date <= @EndDate))
你的问题是:

如果SomeParameter等于“N”,则“and t1.name IN(name)”将附加到where子句,否则如果SomeParameter等于“Y”,则“and t1.name IN(name)或t1.name为null”)将附加到where子句。对吗

否。
WHERE
子句始终是它的本来面目。不会更改,但会对其进行评估。让我们看看它在这两种情况下是如何工作的:

案例1:SomeParameter='N'

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

=>

(TRUE and t1.name IN (Name)) OR 
(FALSE and t1.name IN (Name) OR t1.name is null)
如果(name)
中的
t1.name也为true,则记录将包括在结果集中,因为

(TRUE and TRUE) OR (FALSE AND TRUE OR FALSE) = 
 TRUE           OR (FALSE          OR FALSE) = 
 TRUE           OR  FALSE                    =
 TRUE
(TRUE and FALSE) OR (FALSE AND FALSE OR FALSE) = 
 FALSE           OR (FALSE           OR FALSE) = 
 FALSE           OR  FALSE                     =
 FALSE
如果(name)
中的
t1.name为false,则记录不包括在结果集中,因为

(TRUE and TRUE) OR (FALSE AND TRUE OR FALSE) = 
 TRUE           OR (FALSE          OR FALSE) = 
 TRUE           OR  FALSE                    =
 TRUE
(TRUE and FALSE) OR (FALSE AND FALSE OR FALSE) = 
 FALSE           OR (FALSE           OR FALSE) = 
 FALSE           OR  FALSE                     =
 FALSE
案例2:SomeParameter='Y'

(SomeParameter = 'N' and t1.name IN (Name)) OR 
(SomeParameter = 'Y' and t1.name IN (Name) OR t1.name is null)

=>

(FALSE and t1.name IN (Name)) OR 
(TRUE and t1.name IN (Name) OR t1.name is null)
如果(Name)
中的
t1.Name也为true,则记录将包括在结果集中,因为

(FALSE and TRUE) OR (TRUE AND TRUE OR FALSE) = 
 FALSE           OR (TRUE AND TRUE)          =
 FALSE           OR  TRUE                    =
 TRUE
(FALSE and FALSE) OR (TRUE AND FALSE OR FALSE) = 
 FALSE            OR (TRUE AND FALSE)          =
 FALSE            OR  FALSE                    =
 FALSE
如果(Name)
中的
t1.Name为false,则记录不包括在结果集中,因为

(FALSE and TRUE) OR (TRUE AND TRUE OR FALSE) = 
 FALSE           OR (TRUE AND TRUE)          =
 FALSE           OR  TRUE                    =
 TRUE
(FALSE and FALSE) OR (TRUE AND FALSE OR FALSE) = 
 FALSE            OR (TRUE AND FALSE)          =
 FALSE            OR  FALSE                    =
 FALSE

我假设您的意思是对调用应用程序“无限制”,即它将验证开始和/或结束日期(如果提供),否则将被忽略。我重新编写了一点-我的意思是对日期范围=>的“任意一方”没有限制,即返回所有值。谢谢,但我认为所有以AND和on开头的代码都会附加到where子句中。例如,如果我有一个声明:(SomeParameter='N'和t1.name IN(name))或SomeParameter='Y'(并且t1.name IN(name)或t1.name为null))--如果SomeParameter等于'N',那么“and t1.name IN(name)”将被追加到where子句中,否则如果SomeParameter等于'Y',then”和t1.name IN(name)或t1.name为null)将被追加到where子句。是这样吗?事情不是这样的。我将用你的第二个例子编辑我的答案。@Thorsten Dittmar极好的答案!非常感谢您的详细解释!我为此挣扎了很长一段时间。:)我是阿苏