1. 程式人生 > >通往t - sql的階梯:超越基本級別6:使用CASE表達式和IIF函數

通往t - sql的階梯:超越基本級別6:使用CASE表達式和IIF函數

簡化 只有一個 multi 基本 腳本 需要 mos named end

摘要:此文章轉自:

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所示。

技術分享

1CASE語法的表達

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).

  1. The Simple CASE syntax only supports the equality operator, whereas the Searched CASE syntax supports multiple operators
  2. The Simple CASE syntax supports multiple operators, whereas the Searched CASE syntax supports only the equality operator
  3. 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.
  4. 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?

  1. The THEN expression of the last WHEN clause that evaluates to TRUE is executed.
  2. The THEN expression of the first WHEN clause that evaluates to TRUE is executed.
  3. All THEN expressions of the WHEN clauses that evaluates to TRUE are executed.
  4. 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?

  1. 8
  2. 10
  3. 16
  4. 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函數