Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

cdn.s3techtraining.com
from cdn.s3techtraining.com More from this publisher
17.06.2013 Views

Chapter 11: Writing Scripts and Batches Now, I’ve mixed all sorts of uses of the IF statement there. I have the most basic IF statement — with no BEGIN...END or ELSE. In my other IF statement, the IF portion uses a BEGIN...END block, but the ELSE does not. I did this one this way just to illustrate how you can mix them. That said, I recommend you go back to my old axiom of “be consistent.” It can be really hard to deal with what statement is being controlled by what IF...ELSE condition if you are mixing the way you group things. In practice, if I’m using BEGIN...END on any statement within a given IF, then I use them for every block of code in that IF statement even if there is only one statement for that particular condition. The CASE Statement 354 The CASE statement is, in some ways, the equivalent of one of several different statements depending on the language from which you’re coming. Statements in procedural programming languages that work in a similar way to CASE include: ❑ Switch: C, C#, C++, Java, php, Perl, Delphi ❑ Select Case: Visual Basic ❑ Do Case: Xbase ❑ Evaluate: COBOL I’m sure there are others — these are just from the languages that I’ve worked with in some form or another over the years. The big drawback in using a CASE statement in T-SQL is that it is, in many ways, more of a substitution operator than a control-of-flow statement. There is more than one way to write a CASE statement — with an input expression or a Boolean expression. The first option is to use an input expression that will be compared with the value used in each WHEN clause. The SQL Server documentation refers to this as a simple CASE: CASE WHEN THEN [...n] [ELSE ] END Option number two is to provide an expression with each WHEN clause that will evaluate to TRUE/FALSE. The docs refer to this as a searched CASE: CASE WHEN THEN [...n] [ELSE ] END Perhaps what’s nicest about CASE is that you can use it “inline” with (that is, as an integral part of) a SELECT statement. This can actually be quite powerful.

A Simple CASE A simple CASE takes an expression that equates to a Boolean result. Let’s get right to an example: Use AdventureWorks2008; GO SELECT TOP 10 SalesOrderID, SalesOrderID % 10 AS ‘Last Digit’, Position = CASE SalesOrderID % 10 WHEN 1 THEN ‘First’ WHEN 2 THEN ‘Second’ WHEN 3 THEN ‘Third’ WHEN 4 THEN ‘Fourth’ ELSE ‘Something Else’ END FROM Sales.SalesOrderHeader; For those of you who aren’t familiar with it, the % operator is for a modulus. A modulus works in a similar manner to the divide by (/), but it gives you only the remainder — therefore, 16 % 4 = 0 (4 goes into 16 evenly), but 16 % 5 = 1 (16 divided by 5 has a remainder of 1). In the example, since we’re dividing by 10, using the modulus is giving us the last digit of the number we’re evaluating. Let’s see what we got with this: SalesOrderID Last Digit Position ------------ ----------- -------------- 43793 3 Third 51522 2 Second 57418 8 Something Else 43767 7 Something Else 51493 3 Third 72773 3 Third 43736 6 Something Else 51238 8 Something Else 53237 7 Something Else 43701 1 First (10 row(s) affected) Notice that whenever there is a matching value in the list, the THEN clause is invoked. Since we have an ELSE clause, any value that doesn’t match one of the previous values will be assigned whatever we’ve put in our ELSE. If we had left the ELSE out, then any such value would be given a NULL. Let’s go with one more example that expands on what we can use as an expression. This time, we’ll use another column from our query: USE AdventureWorks2008; GO SELECT TOP 10 SalesOrderID % 10 AS ‘OrderLastDigit’, ProductID % 10 AS ‘ProductLastDigit’, “How Close?” = CASE SalesOrderID % 10 WHEN ProductID % 1 THEN ‘Exact Match!’ WHEN ProductID % 1 - 1 THEN ‘Within 1’ WHEN ProductID % 1 + 1 THEN ‘Within 1’ Chapter 11: Writing Scripts and Batches 355

A Simple CASE<br />

A simple CASE takes an expression that equates to a Boolean result. Let’s get right to an example:<br />

Use AdventureWorks<strong>2008</strong>;<br />

GO<br />

SELECT TOP 10 SalesOrderID,<br />

SalesOrderID % 10 AS ‘Last Digit’,<br />

Position = CASE SalesOrderID % 10<br />

WHEN 1 THEN ‘First’<br />

WHEN 2 THEN ‘Second’<br />

WHEN 3 THEN ‘Third’<br />

WHEN 4 THEN ‘Fourth’<br />

ELSE ‘Something Else’<br />

END<br />

FROM Sales.SalesOrderHeader;<br />

For those of you who aren’t familiar with it, the % operator is for a modulus. A modulus works in a similar<br />

manner to the divide by (/), but it gives you only the remainder — therefore, 16 % 4 = 0 (4 goes into 16<br />

evenly), but 16 % 5 = 1 (16 divided by 5 has a remainder of 1). In the example, since we’re dividing by<br />

10, using the modulus is giving us the last digit of the number we’re evaluating.<br />

Let’s see what we got with this:<br />

SalesOrderID Last Digit Position<br />

------------ ----------- --------------<br />

43793 3 Third<br />

51522 2 Second<br />

57418 8 Something Else<br />

43767 7 Something Else<br />

51493 3 Third<br />

72773 3 Third<br />

43736 6 Something Else<br />

51238 8 Something Else<br />

53237 7 Something Else<br />

43701 1 First<br />

(10 row(s) affected)<br />

Notice that whenever there is a matching value in the list, the THEN clause is invoked. Since we have an<br />

ELSE clause, any value that doesn’t match one of the previous values will be assigned whatever we’ve<br />

put in our ELSE. If we had left the ELSE out, then any such value would be given a NULL.<br />

Let’s go with one more example that expands on what we can use as an expression. This time, we’ll use<br />

another column from our query:<br />

USE AdventureWorks<strong>2008</strong>;<br />

GO<br />

SELECT TOP 10 SalesOrderID % 10 AS ‘OrderLastDigit’,<br />

ProductID % 10 AS ‘ProductLastDigit’,<br />

“How Close?” = CASE SalesOrderID % 10<br />

WHEN ProductID % 1 THEN ‘Exact Match!’<br />

WHEN ProductID % 1 - 1 THEN ‘Within 1’<br />

WHEN ProductID % 1 + 1 THEN ‘Within 1’<br />

Chapter 11: Writing Scripts and Batches<br />

355

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!