Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
Beginning Microsoft SQL Server 2008 ... - S3 Tech Training Beginning Microsoft SQL Server 2008 ... - S3 Tech Training
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
- Page 342 and 343: Chapter 10: Views Try It Out Using
- Page 344 and 345: Chapter 10: Views 306 soh.SalesOrde
- Page 346 and 347: Chapter 10: Views AW00000676 43659
- Page 348 and 349: Chapter 10: Views NULL values will
- Page 350 and 351: Chapter 10: Views Editing V iews wi
- Page 352 and 353: Chapter 10: Views 314 There are fou
- Page 354 and 355: Chapter 10: Views Editing Views in
- Page 356 and 357: Chapter 10: Views 318 In addition,
- Page 358 and 359: Chapter 10: Views 320 from the firs
- Page 360 and 361: Chapter 10: Views You can get the y
- Page 363 and 364: 11 Writing Scripts and Batches Whet
- Page 365 and 366: Next we have a DECLARE statement to
- Page 367 and 368: I’m not going to pick any bones a
- Page 369 and 370: Using @@IDENTITY @@IDENTITY is one
- Page 371 and 372: How It Works What we’re doing in
- Page 373 and 374: DECLARE @RowCount int; --Notice the
- Page 375 and 376: When the editing tool encounters a
- Page 377 and 378: When you think about it, this seems
- Page 379 and 380: So, let’s try a quick query direc
- Page 381 and 382: We now have our text file source fo
- Page 383 and 384: Let’s build an example in the Adv
- Page 385 and 386: DECLARE @InVar varchar(50); DECLARE
- Page 387 and 388: -- This won’t work DECLARE @Numbe
- Page 389 and 390: -- Now we’re run our conditional
- Page 391: Out of the condition from inner con
- Page 395 and 396: 3 8 More Than One Apart 2 2 Ends Wi
- Page 397 and 398: Now, I don’t know about you, but
- Page 399 and 400: The WAITFOR statement does exactly
- Page 401 and 402: IF @ErrorNo = 2714 -- Object exists
- Page 403: Chapter 11: Writing Scripts and Bat
- Page 406 and 407: Chapter 12: Stored Procedures Creat
- Page 408 and 409: Chapter 12: Stored Procedures Dropp
- Page 410 and 411: Chapter 12: Stored Procedures Suppl
- Page 412 and 413: Chapter 12: Stored Procedures 374 [
- Page 414 and 415: Chapter 12: Stored Procedures Confi
- Page 416 and 417: Chapter 12: Stored Procedures Now,
- Page 418 and 419: Chapter 12: Stored Procedures SQL S
- Page 420 and 421: Chapter 12: Stored Procedures 382 c
- Page 422 and 423: Chapter 12: Stored Procedures It wo
- Page 424 and 425: Chapter 12: Stored Procedures 386 r
- Page 426 and 427: Chapter 12: Stored Procedures 388 -
- Page 428 and 429: Chapter 12: Stored Procedures Note
- Page 430 and 431: Chapter 12: Stored Procedures 392 n
- Page 432 and 433: Chapter 12: Stored Procedures All t
- Page 434 and 435: Chapter 12: Stored Procedures Sproc
- Page 436 and 437: Chapter 12: Stored Procedures When
- Page 438 and 439: Chapter 12: Stored Procedures 400 @
- Page 440 and 441: Chapter 12: Stored Procedures I’d
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