简体   繁体   English

CASE 表达式在 SQL 服务器中有效,但在 Visual Studio Reporting Services 中无效

[英]CASE expression working in SQL Server but not working in Visual Studio Reporting Services

There is a where statement:有一个where语句:

WHERE (@obsolete=(case  when part.drawissno = 'OBS' then 'OBS' else 'NO' end) or @obsolete is null)
and ((CASE WHEN part.sm = 'MANUFACTURED' THEN mpfmain.mpflang END) = @country or @country IS NULL)

The parameters work correctly in SQL Server, but when added to the Report, all values, except null, don't work.参数在 SQL 服务器中正常工作,但是当添加到报告中时,除 null 之外的所有值都不起作用。 Parameter @country has values: GERMANY, SPAIN, FRANCE, ITALY.参数@country具有值:德国、西班牙、法国、意大利。 The parameter in report works only when the user has to input the country himself.报告中的参数仅在用户必须自己输入国家/地区时才起作用。 If write in parameter properties the available values, they do not work.如果在参数属性中写入可用值,它们将不起作用。 Other parameters, which are not included here, work correctly, but they don't have CASE within themselves.此处未包含的其他参数可以正常工作,但它们本身没有 CASE。 Could it be the problem with the CASE expression?会不会是 CASE 表达式的问题?

case expressions just make where clauses harder to follow and harder to optimize. case表达式只会使where子句更难遵循和更难优化。 They can usually be rewritten without case :它们通常可以在没有case的情况下重写:

where (@obsolete is null or part.drawissno = @obsolete) and
      (@country is null or (part.sm = 'MANUFACTURED' and mpfmain.mpflang = @country))

I see 2 possible problems with the second CASE expression:我看到第二个 CASE 表达式有两个可能的问题:

1) If mpfmain.mpflang is NULL then you compare with NULL with the equal sign. 1) 如果 mpfmain.mpflang 是 NULL 那么你用等号与 NULL 进行比较。 This will not work even if @country is also NULL.即使@country 也是 NULL,这也不起作用。

2) You are missing an ELSE part. 2)您缺少 ELSE 部分。 It is of course not mandatory, but consider what happens if either part.sm is NULL, or part.sm has another value than 'MANUFACTURED'.这当然不是强制性的,但请考虑如果 part.sm 是 NULL,或者 part.sm 的值不是“MANUFACTURED”,会发生什么情况。

You might benefit from the ISNULL method around columns or parameters than can be NULL.您可能会从围绕列或参数的 ISNULL 方法中受益,而不是 NULL。 You can read more about the ISNULL method here:您可以在此处阅读有关 ISNULL 方法的更多信息:

https://docs.microsoft.com/en-us/sql/t-sql/functions/isnull-transact-sql https://docs.microsoft.com/en-us/sql/t-sql/functions/isnull-transact-sql

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

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