T-SQL in One Hour a Day, Sams Teach Yourself by Alison Balter

T-SQL in One Hour a Day, Sams Teach Yourself by Alison Balter

Author:Alison Balter [Balter, Alison]
Language: eng
Format: azw3, pdf
Publisher: Pearson Education
Published: 2015-10-23T04:00:00+00:00


FIGURE 13.14 An example of executing a stored procedure that includes the RETURN statement.

FIGURE 13.15 An example of executing the same stored procedure with a different value for the month of the current date.

Working with the CASE Statement

Most developers use the CASE statement to compare a result from a SQL statement against a set of simple responses. The CASE statement returns the appropriate value. The CASE statement looks like this:

Click here to view code image

CASE InputExpression

WHEN WhenExpression THEN ResultExpression

[...n]

[ELSE ElseResultExpression]

END

Here’s an example of this use of a case statement:

Click here to view code image

SELECT SalesOrderID, ShipDate,

CASE ShipMethodID

WHEN 1 THEN 'UPS'

WHEN 3 THEN 'FedEx'

WHEN 5 THEN 'U.S. Mail'

END AS Shipper,

Freight FROM Sales.SalesOrderHeader

WHERE ShipDate BETWEEN '1/1/13' AND '2/28/13'

AND Freight <= 50

The expression selects the SalesOrderID, ShipDate, and Freight fields from the Sales.SalesOrderHeader table. The CASE statement evaluates the contents of the ShipMethodID field. It returns an appropriate string depending on the value in the ShipMethodID field. Figure 13.16 illustrates this example.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.