通往t - sql的阶梯:超越基本级别6:使用CASE表达式和IIF函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了通往t - sql的阶梯:超越基本级别6:使用CASE表达式和IIF函数相关的知识,希望对你有一定的参考价值。
摘要:此文章转自:
http://www.sqlservercentral.com/articles/Stairway+Series/108723/
Stairway to T-SQL: Beyond The Basics Level 6: Using the CASE Expression and IIF Function
通往t - sql的阶梯:超越基本级别6:使用CASE表达式和IIF函数
By Gregory Larsen, 2016/04/20 (first published: 2014/04/09)
格雷戈里·拉森(Gregory Larsen),2016/04/20(第一次出版:2014 /04/09)
The Series
该系列
This article is part of the Stairway Series: Stairway to T-SQL: Beyond The Basics
Following on from his Stairway to T-SQL DML, Gregory Larsen covers more advanced aspects of the T-SQL language such as subqueries.
这篇文章是楼梯系列的一部分:通往t - sql的楼梯:超越基础
从他的阶梯到t - sql DML,Gregory Larsen涵盖了t - sql语言的更高级的方面,如子查询。
There are times where you need to write a single TSQL statement that is able to return different TSQL expressions based on the evaluation of another expression. When you need this kind of functionality you can use the CASE expression or IIF function to meet this requirement. In this article I will be reviewing the CASE and IIF syntax and showing you examples of how the CASE expression and IIF function.
有时您需要编写一个TSQL语句,该语句可以根据另一个表达式的评估返回不同的TSQL表达式。当您需要这种功能时,您可以使用CASE表达式或IIF函数来满足这个需求。在本文中,我将回顾这个案例和IIF语法,并向您展示示例如何使用CASE表达式和IIF函数。
Understanding the CASE Expression
了解CASE的表达
The Transact-SQL CASE expression allows you to place conditional logic in your TSQL code. This conditional logic provides you with a way to place different code blocks in your TSQL statements which can be executed depending on a TRUE or FALSE evaluation of the conditional logic. You can place multiple conditional expressions in a single CASE expression. When you have multiple conditional expressions in your CASE clause, the first expression that evaluates to TRUE will be the code block that is evaluated by your TSQL statement. To better understand how the CASE expression works I will review the syntax of the CASE expression and then go through a number of different examples.
transact - sql CASE表达式允许您在TSQL代码中放置条件逻辑。这个条件逻辑为您提供了一种方法,可以将不同的代码块放置在您的TSQL语句中,这取决于对条件逻辑的真实或错误的评估。您可以在一个CASE表达式中放置多个条件表达式。当您在CASE子句中有多个条件表达式时,计算为TRUE的第一个表达式将是由TSQL语句计算的代码块。为了更好地理解CASE表达式的工作方式,我将回顾一下CASE表达式的语法,然后再看一些不同的例子。
CASE Expression Syntax
CASE语法的表达
CASE表达式有两种不同的格式:简单和搜索。每种类型都有略微不同的格式,如图1所示。
图1:CASE语法的表达
By reviewing the two different formats for the CASE expression in Figure 1 you can see how each format provides a different way to identify one of multiple expressions that determine the results of the CASE expression. With both types of CASE, a Boolean test is performed for each WHEN clause. With the Simple CASE expression the left hand side of the Boolean test appears right after the CASE word and is called the “input_expression”, and right hand sided of the Boolean test is right after word WHEN and is called the “when expression”. With the Simple CASE expression the operator between the “input_expression” and the “when_expression” is always the equality operator. Whereas with the searched CASE expression each WHEN clause will contain a “Boolean_expression”. This “Boolean_expression” can be a simple Boolean expression with a single operator, or a complex Boolean expression with many different conditions. In addition, the searched CASE expression can use the full set of Boolean operators.
通过查看图1中的CASE表达式的两种不同的格式,您可以看到每种格式提供了一种不同的方式来确定多个表达式中的一个,这些表达式决定了CASE表达式的结果。在这两种情况下,每个WHEN子句都执行布尔测试。在简单的CASE表达式中,布尔测试的左手边出现在CASE word之后,被称为“input_expression”,而布尔测试的右手边则是“WHEN”,被称为“WHEN表达式”。使用简单的CASE表达式,在“input_expression”和“何时表达式”之间的操作符始终是相等的运算符。而在搜索的CASE表达式中,每个WHEN子句将包含一个“Boolean_expression”。这个“Boolean_expression”可以是一个简单的布尔表达式,一个操作符,或者一个复杂的布尔表达式,有许多不同的条件。此外,搜索的CASE表达式可以使用完整的布尔运算符集。
Regardless of which CASE format is used, each WHEN clause is compared in the order in which it appears. The result of the CASE expression will be based on the first WHEN clause that evaluates to TRUE. If no WHEN clause evaluates to TRUE then the ELSE expression is returned. When the ELSE clause is omitted and no WHEN clause evaluates to TRUE, then a NULL value is returned.
不管使用哪种情况格式,每个WHEN子句都按其出现的顺序进行比较。CASE表达式的结果将基于第一个WHEN子句计算为TRUE。如果不将子句计算为TRUE,则返回ELSE表达式。当ELSE子句被省略时,当子句计算为TRUE时,则返回空值。
Sample Data for Examples
样本数据的样子
In order to have a table to demonstrate using the CASE expression I will be using the script in Listing 1 to create a sample table named MyOrder. If you would like to follow along with my examples and run them on your SQL Server instance you can create this table in a database of your choice.
为了使用CASE表达式演示一个表,我将使用清单1中的脚本创建一个名为MyOrder的示例表。如果您想要跟随我的示例并在您的SQL服务器实例上运行它们,您可以在您选择的数据库中创建此表。
清单1:创建样例表MyOrder
Using a Simple CASE expression with WHEN and ELSE expressions
使用一个简单的CASE表达式和其他表达式
To demonstrate how the simple CASE expression format works let me run the code in Listing 2.
为了演示简单的CASE表达式格式如何工作,我来运行清单2中的代码。
清单2:用ELSE表达式进行简单的CASE表达式
Listing 2: Simple CASE expression with ELSE expression
Let me first talk about why this is a Simple CASE expression. If you review the code in Listing 2 you can see that right after the word CASE I specified the expression “YEAR(OrderDT)”, and then I followed that by three different WHEN expressions each one having a different year specified, starting with 2014. Because I specified that expression between the CASE and the first WHEN keyword this tell SQL Server that this is a simple CASE expression.
When my simple CASE expression is evaluated it uses the equality operator (“=”) between the “YEAR(OrderDate)” value and the different WHEN expressions. Therefore the code in Listing 1 is going to display “Year 1” for the YearType column for rows with an OrderDT year value of “2014”, or it will display “Year 2” for rows with an OrderDT year of “2013” or it will display “Year 3” for rows with an OrderDT year of “2012”. If the year of the OrderDT doesn’t match any of the WHEN expressions then the ELSE condition will display “Year 4 and beyond”.
When I run the code in Listing 2 I get the output shown in Result 1.
让我先说说为什么这是一个简单的案例表达。如果您回顾清单2中的代码,您可以看到在单词CASE之后,我指定了表达式“YEAR(OrderDT)”,然后我遵循了三种不同的表达式,每一个都有不同的年份,从2014开始。因为我在CASE和第一个关键字之间指定了这个表达式,它告诉SQL Server这是一个简单的CASE表达式。
当我的简单的CASE表达式被评估时,它使用“YEAR(OrderDate)”值和不同的表达式之间的等号运算符(“=”)。因此,清单1中的代码将显示为YearType列“1年”行OrderDT年值“2014”,或将显示“2年”行OrderDT一年的将显示“2013”或者“三年级”行OrderDT年的“2012”。如果OrderDT的年份不匹配任何表达式,那么ELSE条件将显示“year 4及beyond”。
当我运行清单2中的代码时,我将得到结果1所示的输出。
结果1:运行清单2的结果
Using a Simple CASE expression without an ELSE expression
使用一个没有其他表达式的简单的CASE表达式
Let me run the code in Listing 3 which will show what happens when a Simple CASE expression doesn’t have an ELSE clause.
让我运行清单3中的代码,它将显示当一个简单的CASE表达式没有其他子句时发生的情况。
清单3:使用一个没有其他表达式的简单的CASE表达式
The code in Listing 3 is just like code in Listing 2 but without an ELSE clause. When I run the code in Listing 3 it produces the results shown in Result 2.
清单3中的代码就像清单2中的代码,但没有其他子句。当我运行清单3中的代码时,它会产生结果2所示的结果。
结果2:运行名单3的结果
By reviewing the output in Result 2 you can see that when the year of the OrderDT in the MyOrder table doesn’t meet any of the WHEN clause conditions SQL Server displays “NULL” for the YearType value for that row.
通过检查结果2中的输出,您可以看到,当MyOrder表中的OrderDT年不满足任何when子句条件时,SQL Server显示该行的YearType值为“NULL”。
Using a Searched CASE expression
使用一个搜索的CASE表达式
In the simple CASE expression the WHEN expressions were evaluated based on equality operator. With the searched CASE expressions you can use other operators, and the CASE expression syntax is a little different. To demonstrate this let’s look at the code in Listing 4.
在简单的情况下,表达式是基于等式运算符来计算的。通过搜索的CASE表达式,您可以使用其他操作符,而CASE表达式语法略有不同。为了演示这一点,我们来看看清单4中的代码。
清单4:搜索CASE的表达式
If you look at the code in Listing 4 you can see that the WHEN clause follows directly after the CASE clause with no text between the two clauses. This tells SQL Server this a searched CASE expression. Also note the Boolean expression following each WHEN clause. As you can see not all those Boolean expressions are using the equality operator, the last WHEN expression uses the less than (“<”) operator. The CASE expression in Listing 4 is logically the same as the CASE expression in Listing 2. Therefore when I run the code in Listing 4 it produces the same results as shown in Result 1.
如果您查看清单4中的代码,您可以看到,WHEN子句直接跟随CASE子句之后,两个子句之间没有文本。这告诉SQL Server这是一个搜索的CASE表达式。还要注意每个WHEN子句后面的布尔表达式。正如您所看到的,并不是所有的布尔表达式都使用相等运算符,最后一个表达式使用小于(“<”)运算符。清单4中的CASE表达式在逻辑上与清单2中的CASE表达式相同。因此,当我运行清单4中的代码时,它产生的结果与结果1中的结果相同。
What expression is returned if multiple WHEN expressions evaluate to TRUE?
当表达式求值为TRUE时,返回什么表达式?
There may be situations where different WHEN expressions evaluate to TRUE in a single CASE expression. When this happens SQL Server will return the result expression associated with the first WHEN expression that evaluates to true. Therefore the order of your WHEN clauses will control what result you get returned from your CASE expression if multiple WHEN clauses evaluate to TRUE.
To demonstrate this let’s use the CASE expression to display “200 dollar Order” when OrderAmt is within the $200 range, “100 Dollar Order” when the OrderAmt is within the $100 range and “< 100 Dollar Order” when the OrderAmt is less than $100When an OrderAmt doesn’t fall into any of these categories then categorize the order as a “300 Dollar and above Order”. Let’s review the code in Listing 5 to demonstrate what happens when multiple WHEN expressions evaluate to TRUE when trying to categorize orders into one of these OrderAmt_Category values.
在一个单独的CASE表达式中,当表达式计算为TRUE时,可能会出现不同的情况。当这种情况发生时,SQL Server将返回与第一个表达式相关联的结果表达式。因此,当子句计算为TRUE时,当子句将控制你从CASE表达式返回的结果时。
展示我们用这样的表情来显示“200美元的秩序”OrderAmt 200美元的范围内时,“100美元的秩序”OrderAmt时100美元的范围内和“< 100美元订单“当OrderAmt小于100美元当一个OrderAmt不属于任何这些类别的分类顺序为“300美元以上的秩序”。让我们回顾一下清单5中的代码,以演示当在尝试将订单分类为这些OrderAmt_Category值时,当多个表达式对TRUE执行时,会发生什么情况。
清单5:当表达式计算为TRUE时的倍数
When I run the code in Listing 5 I get the output in Result 3.
当我运行清单5中的代码时,我得到了结果3中的输出。
结果3:清单5中的运行结果
By reviewing the results in Result 3 you can see that every order is reported to be either a 200 or a 300 and above order, and we know this is incorrect. This happened because I only used the less than (“<”) operator to simplistically categorize Orders which lead to multiple WHEN expressions evaluating to TRUE in my CASE expression. The ordering of the WHEN clauses did not allow the correct expression to be returned.
By re-ordering my WHEN clauses I can get the results I want. The code in Listing 6 is the same as Listing 5 but I have re-ordered the WHEN clauses to correctly categorized my orders.
通过回顾结果3,你可以看到每个订单都被报告为200或300以上的订单,我们知道这是不正确的。之所以发生这种情况,是因为我只使用了小于(“<”)运算符来简化排序,当表达式在我的CASE表达式中值为TRUE时,会导致多个命令。WHEN从句的顺序不允许返回正确的表达式。
通过重新排序我的WHEN条款,我可以得到我想要的结果。清单6中的代码与清单5相同,但是我重新排序了WHEN子句来正确地分类我的命令。
清单6:类似清单5的代码,但是当子句的顺序不同时
When I run the code in Listing 5 I get the output in Result 4.
当我运行清单5是,我得到结果4的输出
结果4:运行清单6的结果
By reviewing the output in Result 4, you can see that by changing the order of the WHEN expressions I got the correct results for each order.
通过查看结果4中的输出,您可以看到,通过更改表达式的顺序,我得到了每个命令的正确结果。
Nesting CASE expressions
嵌入CASE的表达式
清单7:嵌入CASE语句
Occasionally you might have a need to do additional testing to further categorize data using the CASE expression. When that occurs you can use a nested CASE expression. The code in Listing 7 shows an example of nesting the CASE expression to further categorize orders in the MyOrder table to determine if the order was purchased using the Layaway value when an order is over $200.
有时候,您可能需要做额外的测试,以使用CASE表达式进一步对数据进行分类。当出现这种情况时,可以使用嵌套的CASE表达式。清单7中的代码展示了嵌套这个CASE表达式的示例,以进一步对MyOrder表中的订单进行分类,以确定订单是否在订单超过200美元时使用Layaway值购买。
The code in Listing 7 is similar to the code in Listing 6. The only difference is I added an additional CASE expression to see if an order in MyOrder table was purchased using the Layaway option, which is only allowed on purchases over $200. Keep in mind when you nest CASE expressions SQL Server only allow you to have up to 10 levels of nesting.
清单7中的代码与清单6中的代码类似。唯一的区别是,我添加了一个额外的CASE表达式,以查看MyOrder表中的订单是否使用了Layaway选项购买,该选项仅允许购买超过200美元。请记住,当您的nest CASE表达式SQL服务器只允许您有多达10级的嵌套。
Other Places where the CASE expression can be used
其它可以使用CASE表达式的地方
So far, all of my examples used the CASE expression to create a result string by placing the CASE expression in the select list of a TSQL SELECT statement. You can also use a CASE expression in an UPDATE, DELETE and SET statement. Additionally the CASE expression can be used in conjunction with the IN, WHERE, ORDER BY and HAVING clauses. In Listing 8 I am using a CASE expressing a WHERE clause.
到目前为止,我的所有示例都使用CASE表达式,通过将CASE表达式放置在TSQL select语句的select列表中来创建一个结果字符串。您还可以在UPDATE、DELETE和SET语句中使用CASE表达式。另外,CASE表达式可以与in、WHERE、ORDER BY和HAVING子句一起使用。在清单8中,我使用了一个表示WHERE子句的案例。
清单8:在WHERE子句中使用CASE表达式
In Listing 8 I only want to return an order from the MyOrder table for rows in “Year 1”. To accomplish this I place the same CASE expression as I used in Listing 2 in the WHERE clause. I used the CASE expression as the left-hand part of the WHERE condition so it would produce different “Year…” strings based on the OrderDT column. I then tested the string that was produced from the CASE expression to see if it was equal to the value “Year 1”, when it was a row would be returned from MyOrder table. Keep in mind I would not recommend use a CASE expression to select date from a date column using a sting like “Year 1”, when there are other better methods like using the YEAR function to select rows for a given year. I have only done it here to demonstrate how to use a CASE statement in a WHERE clause.
在清单8中,我只希望从MyOrder表中返回“Year 1”中的行。为了实现这一点,我将相同的CASE表达式放在WHERE子句中使用的清单2中。我用条件表达式的左边部分,所以它会产生不同的“…”基于OrderDT列字符串。然后我测试了从CASE表达式生成的字符串,看它是否等于“第一年”的值,当它是一行的时候,将从MyOrder表返回。请记住,我不建议使用一个CASE表达式,使用像“Year 1”这样的“sting”来从日期列中选择日期,这时还有其他更好的方法,比如使用Year函数来选择给定年份的行。我只是在这里演示了如何在WHERE子句中使用CASE语句。
Shortcutting the CASE Expression using the IIF Function
利用IIF函数缩短案例表达
With the introduction of SQL Server 2012, Microsoft added the IIF function. The IIF function can be considered a shortcut to the CASE statement. In Figure 2 you can find the syntax for the IIF function.
随着SQL Server 2012的推出,微软增加了IIF功能。IIF函数可以被认为是CASE语句的快捷方式。在图2中,您可以找到IIF函数的语法。
图2:IIF函数的语法
The “Boolean_expression” is a valid Boolean expression that equates to TRUE or FALSE. When the Boolean expression equates to a TRUE value then the “true_value” expression is executed. If the Boolean expression equates to FALSE then the “false_value” is executed. Just like the CASE expression the IIF function can be nested up to 10 levels.
“Boolean_expression”是一个有效的布尔表达式,它等于TRUE或FALSE。当布尔表达式等价于一个TRUE值时,执行“true_value”表达式。如果布尔表达式等于FALSE,则执行“虚值”。与CASE表达式一样,IIF函数可以嵌套到10级。
Example of Using IIF
使用IIF的例子
To demonstrate how the IIF function can be used to replace the CASE expression, let’s review the code that uses a searched CASE expression in Listing 9.
为了演示如何使用IIF函数来替换CASE表达式,让我们来回顾一下使用清单9中的搜索用例表达式的代码。
清单9:简单的CASE表达式示例
The code in Listing 9 has just a single WHEN expression that is used to determine if the OrderAmt is either a high or low dollar order. If the WHEN expression “OrderAMT > 200” evaluates to TRUE then the OrderType value is set to “High $ Order”. If the WHEN expression evaluates to FALSE then “Low $ Order” is set for the OrderType value.
The re-written code that uses an IIF function instead of a CASE expression can be found in Listing 10.
清单9中的代码只有一个单独的表达式,用于确定OrderAmt是高还是低的订单。如果“OrderAMT > 200”的表达式为TRUE,那么OrderType值将被设置为“High $ Order”。如果当表达式计算为FALSE时,则为OrderType值设置“低$ Order”。
使用IIF函数而不是CASE表达式的重写代码可以在清单10中找到。
清单10:使用IIF函数的例子
By looking at Listing 10 you can see why the IIF function is considered a shorthand version of the CASE expression. The word CASE is replaced with the “IIF(“ string, the “THEN” clause is replaced with a comma, the “ELSE” clause is replaced with a comma and the word “END” is replaced with a “)”. When the Boolean expression “OrderAmt > 200” is TRUE the value “High $ Order” is displayed. When the Boolean expression ‘OrderAmt > 200” is evaluated to FALSE then the “Low $ Order” is displayed. If you run the code in Listing 9 and 10 you will see they both produce the exact same output.
通过查看清单10,您可以看到为什么IIF函数被认为是CASE表达式的简写版本。单词CASE被替换为“IIF(”字符串,“THEN”子句被替换为逗号,“ELSE”子句替换为逗号,“END”替换为“)”。当布尔表达式“OrderAmt > 200”为TRUE时显示值“高$ Order”。当布尔表达式“OrderAmt > 200”被评估为FALSE时,显示“低$ Order”。如果您运行清单9和10中的代码,您将看到它们都产生完全相同的输出。
Example of Nesting the IIF Function
嵌套IIF函数的示例
Just like the CASE expression SQL Server allows you to nest IIF functions. In Listing 11 is an example of nesting the IIF function.
就像CASE表达式SQL Server允许你嵌套IIF函数。清单11是嵌套IIF函数的一个示例。
清单11:IIF函数的嵌套示例
In this example you can see that I have used the IIF function multiple times. Each additional one is either used in the “true value” or the “false value” of the IIF function. The code in Listing 11 is equivalent to the code that uses the nested CASE expression in Listing 7.
在这个示例中,您可以看到我多次使用了IIF函数。每一个额外的一个被用在“真值”或“假值”的IIF函数。清单11中的代码与清单7中使用嵌套CASE表达式的代码相当。
Limitations
限制
As with most TSQL functionality there are limitations. Below are some limitations to note regarding the CASE and IIF constructs.
与大多数TSQL功能一样,它也有局限性。下面是关于案例和IIF构造的一些限制。
CASE Expression Limitations:
- You can only have up to 10 levels of nesting in CASE expressions.
- CASE expressions cannot be used to control the flow of execution of TSQL statements.
IIF Function Limitation:
- You can only have up to 10 levels of nesting of the IIF clause.
案例表达的局限性:
您只能有多达10级的嵌套在情况表达式。
不能使用CASE表达式来控制TSQL语句的执行流。
IIF功能限制:
您只能有多达10层的IIF子句嵌套。
Summary
总结
The CASE expression and IIF function allow you to place expression logic within TSQL code that will change the results of your code based on the evaluated result of an expression. By using the comparison expression supported by the IIF function and the CASE expression you can have different code blocks executed depending on whether the comparison expression evaluates to TRUE or FALSE. The CASE expression and the IIF function provide you programmatic control to meet business requirements that you might not otherwise have.
CASE表达式和IIF函数允许您将表达式逻辑放置在TSQL代码中,这将根据表达式的计算结果更改代码的结果。通过使用IIF函数和CASE表达式支持的比较表达式,您可以根据比较表达式计算为TRUE或FALSE执行不同的代码块。CASE表达式和IIF函数为您提供了编程控制,以满足您可能不具备的业务需求。
Question and Answer
In this section you can review how well you have understood using the CASE and IIF constructs by answering the following questions.
问题和答案
在本节中,您可以通过回答下列问题来回顾您对案例和IIF构造的理解。
Question 1:
There are two different syntax variations for the CASE expression: Simple and Searched. Which two statements below best describe the difference between a Simple and Searched CASE expression (Pick two).
- The Simple CASE syntax only supports the equality operator, whereas the Searched CASE syntax supports multiple operators
- The Simple CASE syntax supports multiple operators, whereas the Searched CASE syntax supports only the equality operator
- The Simple CASE syntax has its Boolean expression specified after the WHEN clause, whereas the Searched CASE syntax has the left side of the Boolean expression right after the CASE statement, and the right side of the Boolean expression after the WHEN clause.
- The Simple CASE syntax has the left side of the Boolean expression right after the CASE statement and the right side of the Boolean expression after the WHEN clause, whereas the Searched CASE expression has its Boolean expression after the WHEN clause
问题1:
对于CASE表达式有两种不同的语法变体:Simple和搜索。以下两个语句最好地描述了简单和搜索的CASE表达式之间的区别(选择两个)。
a.简单的CASE语法只支持等式运算符,而搜索的CASE语法支持多个运算符
b.简单的CASE语法支持多个运算符,而搜索的CASE语法只支持相等运算符
C.简单的CASE语法有在WHEN子句之后指定的布尔表达式,而搜索的CASE语法在CASE语句后面有布尔表达式的左边,在WHEN子句后面的布尔表达式的右边。
d.简单的CASE语法在CASE语句后面有布尔表达式的左边,在WHEN子句后面是布尔表达式的右侧,而搜索的CASE表达式在WHEN子句之后有它的布尔表达式
Question 2:
If the CASE expression has multiple WHEN clauses that evaluate to TRUE, which THEN/ELSE clause is executed?
- The THEN expression of the last WHEN clause that evaluates to TRUE is executed.
- The THEN expression of the first WHEN clause that evaluates to TRUE is executed.
- All THEN expressions of the WHEN clauses that evaluates to TRUE are executed.
- THE ELSE expression is executed
问题2:
如果CASE表达式有多个子句,值为TRUE,那么哪个子句执行呢?
a.最后一个WHEN子句的表达式是执行的。
b.然后执行计算为TRUE的第一个WHEN子句的表达式。
c.然后执行计算为TRUE的WHEN子句的所有表达式。
d.另一个表达式被执行
Question 3:
How many nesting levels can a CASE expression or IIF function have?
- 8
- 10
- 16
- 32
问题3:
一个CASE表达式或IIF函数有多少嵌套级别?
a.8个
b.10个
c.16
d.32
Answers:
回答
Question 1:
The answer is a and d. A Simple CASE statement can only use the equality operator, whereas the Searched CASE expression can handle multiple operators and as well as complex Boolean expressions. Additionally the Simple CASE syntax has the left hand part of the equality operator right after the word CASE and the right hand part of the equality operator right after the word WHEN. A Searched CASE expression has to complete Boolean operation (left hand part, operator, right hand part) right after the WHEN clause
问题1:
答案是a和d。一个简单的CASE语句只能使用相等运算符,而搜索的CASE表达式可以处理多个运算符,以及复杂的布尔表达式。另外,简单的CASE语法在单词CASE后面有等式运算符的左边部分,而等号右边的右边部分则是相等运算符的右边部分。在WHEN子句之后,搜索的CASE表达式必须完成布尔操作(左手部分,操作符,右手部分)
Question 2:
The correct answer is b. If multiple WHEN clauses evaluate to TRUE then SQL Server only executes the THEN portion of the first WHEN clause that evaluates to TRUE. All other THEN clauses for any other WHEN clauses that are evaluated to TRUE are skipped.
问题2:
正确的答案是b。如果多个WHEN子句值为TRUE,那么SQL Server只执行第一个WHEN子句的TRUE。当被评估为TRUE的子句被跳过时,所有其他的子句都将被省略。
Question 3:
The correct answer is b. The CASE expression and the IIF function only supports up to 10 nesting levels.
问题3:
正确的答案是b . CASE表达式和IIF函数只支持多达10个嵌套级别。
以上是关于通往t - sql的阶梯:超越基本级别6:使用CASE表达式和IIF函数的主要内容,如果未能解决你的问题,请参考以下文章
#翻译#通往t - sql的阶梯:超越基本级别3:构建相关子查询,原文链接:Stairway to T-SQL: Beyond The Basics Level 3: Building a Corre
T-SQL的进阶:超越基本级别3:构建相关子查询——701小组
#翻译#通往SQL Server安全级别2的阶梯:身份验证; 唐?吉利; 原文链接:Stairway to SQL Server Security Level 2: Authentication ht