简体   繁体   English

在SQL中的WHERE子句中使用IF / THEN或CASE

[英]Using IF/THEN or CASE within a WHERE clause in SQL

I have been assigned the task of updating the EEO survey and reporting for a mid sized company. 我被分配了更新中型公司的EEO调查和报告的任务。 I am working on a stored procedure to populate a report from. 我正在处理一个存储过程来填充报告。 All is good but for a syntax problem. 一切都很好,但语法问题。 One of the requirements is to dynamically allow the user to filter the results by the EEO Job Group Number. 其中一个要求是动态允许用户按EEO作业组编号过滤结果。 When the report page loads, it populates the table with all Job Groups Combined. 加载报表页面时,它会填充所有作业组合并的表格。 I have placed a DropDownList on the page that allows the user to choose one of the 10 EEO Job Groups or by default, All Job Groups Combined (no filtering). 我在页面上放置了一个DropDownList,允许用户选择10个EEO作业组中的一个,或者默认情况下,选择所有作业组(无过滤)。 The DDL executes postback and populates a parameter; DDL执行回发并填充参数; @intEeoJobGroupID. @intEeoJobGroupID。 There is not actually a 0 ID value in the table, just in the DDL. 表中实际上没有0 ID值,仅在DDL中。 I want the (usp) query to use one set of WHERE statements if the passed parameter @intEeoJobGroupID = 0, and another if @intEeoJobGroupID <> 0. (Effectively adding another AND statement if the parameter <> 0) 我希望(usp)查询在传递参数@intEeoJobGroupID = 0时使用一组WHERE语句,如果@intEeoJobGroupID <> 0则需要另一组WHERE语句。(如果参数<> 0,则有效地添加另一个AND语句)

I want to return the count of how many EEO records meet the requirements of the query. 我想返回有多少EEO记录满足查询要求的计数。 I have tried IF/THEN, and CASE, in many different formats, and can not seem get the syntax right. 我尝试了许多不同格式的IF / THEN和CASE,似乎无法正确使用语法。 In the example below I get the message "Incorrect Syntax near the first = in the THEN statement, as well as the keyword ELSE. 在下面的示例中,我收到消息“THEN语句中第一个=附近的语法错误,以及关键字ELSE。

Any hints? 任何提示?

DECLARE @intEeoJobGroupID INT

SELECT
     COUNT (E.intEeoID)

FROM
     dbo.NewEEO AS E 
WHERE 
    CASE WHEN @intEeoJobGroupID = 0

    THEN
             E.intGenderID = 1
         AND E.intRaceID = 2

    ELSE
             E.intGenderID = 1
         AND E.intRaceID = 2
         AND E.intEeoJobGroupID = @intEeoJobGroupID

You're making it way too complicated: 你做得太复杂了:

WHERE E.intGenderID = 1 
    AND E.intRaceID = 2 
    AND (E.intEeoJobGroupID = @intEeoJobGroupID OR @intEeoJobGroupID = 0)

As someone else already mentioned, your existing syntax was missing an "END", but it still won't work with that added. 正如其他人已经提到的那样,您现有的语法缺少“END”,但它仍然无法使用添加的语法。 To get this right in the future, one thing you can try to do is remember that CASE expressions in SQL are just that: expressions . 为了在将来实现这一目标,您可以尝试做的一件事是记住SQL中的CASE表达式就是: 表达式 They are not statements , as you might be used to with if statements in c# code. 它们不是语句 ,因为您可能习惯使用c#代码中的if语句。 You don't use CASE for flow control, to define blocks as you were trying to do. 您不使用CASE进行流控制,以便按照您的尝试来定义块。

Don't try to return a boolean from a CASE statement. 不要尝试从CASE语句返回一个boolean Instead return some value that is then checked outside the CASE statement (and so then resulting in a boolean). 而是返回一些值,然后在CASE语句之外检查(然后产生一个布尔值)。

CASE WHEN @mode = 1 THEN CASE WHEN <Condition1> THEN 1 ELSE 0 END
     WHEN @mode = 2 THEN CASE WHEN <Condition2> THEN 1 ELSE 0 END
END
=
1

Note: This will create Awful execution/explain plans and totally nerf performance. 注意:这将创建可怕的执行/解释计划和完全的nerf性能。 You are better using real IF blocks and real queries, or possibly unions... 你最好使用真正的 IF块和真实的查询,或者可能使用工会......

IF @mode = 1
  SELECT foo FROM bar WHERE <Condition1>
ELSE IF @mode = 2
  SELECT foo FROM bar WHERE <Condition2>

Or... 要么...

SELECT foo FROM bar WHERE <condition1> AND @mode = 1
UNION ALL
SELECT foo FROM bar WHERE <condition2> AND @mode = 2

In order to prevent massive duplication of code, you may find that encapsulating the bulk of the query in a VIEW is helpful. 为了防止大量重复代码,您可能会发现将大量查询封装在VIEW中会很有帮助。

You can't make a comparison the result of a case condition. 您无法比较案例条件的结果。 If you're using case in a where clause, it needs to be on one side of the operator: 如果您在where子句中使用case ,则它需要位于运算符的一侧:

CASE @case_value
WHEN 0 THEN
     some_column
ELSE
     some_other_column
END = @some_value

However, if you try to make your actual condition fit this rule, you'll end up not using the case statement at all, as @Joel point out. 但是,如果你试图让你的实际条件符合这个规则,你将最终完全不使用case语句,就像@Joel指出的那样。

You have to add 你必须添加

end 结束

in the end of case. 在案件的最后。

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

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