Tải bản đầy đủ (.pdf) (10 trang)

Tài liệu Fundamentals of Transact-SQL ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (36.7 KB, 10 trang )


Fundamentals of Transact-SQL
In this section, you'll learn some of the essential programming constructs available in T-
SQL. Specifically, you'll see how to use variables, comments, and conditional logic.
You'll also see how to use a number of statements that allow you to perform jumps and
loops. Finally, you'll examine cursors, which allow you to process rows returned from the
database one at a time.
Let's start by looking at variables.
Using Variables
A variable allows you to store a value in the memory of a computer. Each variable has a
type that indicates the kind of value that will be stored in that variable. You can use any
of the types shown earlier in Table 2.3
of Chapter 2, "Introduction to Databases."
You declare a variable using the DECLARE statement, followed by the variable name
and the type. You place an at character (@) before the start of the variable name. The
following syntax illustrates the use of the DECLARE statement:
DECLARE @name type
Where name is the name of your variable, and type is the variable type.
For example, the following statements declare two variables named MyProductName and
MyProductID:
DECLARE @MyProductName nvarchar(40)
DECLARE @MyProductID int
As you can see, MyProductName is of the nvarchar type, and MyProductID is of the int
type.
You can place more than one variable declaration on the same line. For example:
DECLARE @MyProductName nvarchar(40), @MyProductID int
Variables are initially set to null. You set a variable's value using the SET statement. For
example, the following statements set MyProductName to Chai and MyProductID to 7:
SET @MyProductName = 'Chai'
SET @MyProductID = 7
The following SELECT statement then uses these variables in the WHERE clause:


SELECT ProductID, ProductName, UnitPrice
FROM Products
WHERE ProductID = @MyProductID
OR ProductName = @MyProductName;
You can execute T-SQL using Query Analyzer, and Figure 4.1
shows the output from the
examples shown in this section.

Figure 4.1: Executing T-SQL using Query Analyzer
Using Comments
You add comments to describe your code, making it more understandable for both
yourself and other programmers. You might think you understand your own code inside
out, but when you return to it for maintenance six months later, you might have forgotten
the intricacies of your own creation! The point is that you should add comments to your
code to aid understanding, but don't think you have to comment every line. Use
comments judiciously.
You need to mark your comments with specific characters so SQL Server ignores them
and doesn't try to process them as code. There are two types of comments: single-line and
multi-line. A single-line comment uses two negative signs (--) and may span only one
line, as shown here:
-- A single-line comment may only span one line.
The -- tells SQL Server to ignore everything up to the end of that line.
A multi-line comment begins with an open comment mark (/*) and ends with a close
comment mark (*/):
/* A multi-line comment
may span more than one
line. */
The /* tells SQL Server to ignore everything up to the next */ mark, no matter how many
lines forward it is. If you were to use single-line comments in this example, you would
have to add -- characters at the beginning of every line that made up the comment.

Multi-line comments can of course also span only one line:
/* Another comment */
Using Conditional Logic
Conditional logic allows you to execute different branches of code based on the Boolean
true or false value of a given expression. For example, you might want to check if an
error condition is true and display a message. You use the IF and optional ELSE
keywords to perform conditional logic. The following syntax illustrates the use of
conditional logic:
IF condition
statement1
[ELSE
statement2]
Where condition is a Boolean expression that evaluates to true or false. If condition is
true, then statement1 is executed, otherwise statement2 is executed.

Note You can replace a single statement with multiple statements by placing those
statements within BEGIN and END statements. This rule applies to all T-SQL
programming constructs.
The following syntax shows the replacement of single statements with a block of
statements placed within BEGIN and END:
IF condition
BEGIN
statements1
END
ELSE
BEGIN
statements2
END
Where statements1 and statements2 are multiple statements. You can also use an optional
ELSE statement to execute a different branch of code if the condition is false.


Note You can nest IF statements to any level.
The following example displays the ProductID, ProductName, and UnitPrice columns for
any rows from the Products table that have a UnitPrice of less than $5. You'll notice the
use of the PRINT statement to output a line in this example.
IF (SELECT COUNT(*) FROM Products WHERE UnitPrice < 5) > 0
BEGIN
PRINT 'The following products have a UnitPrice of less than $5:'
SELECT ProductID, ProductName, UnitPrice
FROM Products
WHERE UnitPrice < 5
END
ELSE
BEGIN
PRINT 'There are no products that have a UnitPrice of less than $5'
END
Using CASE Statements
You use the CASE statement to compare a value against a list of values and execute one
or more statements when a match is found. For example, the following CASE statement
returns Massachusetts:
CASE 'MA'
WHEN 'CA' THEN 'California'
WHEN 'MA' THEN 'Massachusetts'
WHEN 'NY' THEN 'New York'
END
The next example uses a SELECT statement to retrieve the value Massachusetts returned
by the CASE statement:
DECLARE @State nchar(2)
SET @State = 'MA'
DECLARE @StateName nvarchar(15)

SELECT CASE @State
WHEN 'CA' THEN 'California'
WHEN 'MA' THEN 'Massachusetts'
WHEN 'NY' THEN 'New York'
END

You can store the value retrieved by the SELECT statement in a variable, as shown in the
next example:
DECLARE @State nchar(2)
SET @State = 'MA'
DECLARE @StateName nvarchar(15)
SELECT @StateName =
CASE @State
WHEN 'CA' THEN 'California'
WHEN 'MA' THEN 'Massachusetts'
WHEN 'NY' THEN 'New York'
END
PRINT @StateName
The output from this example is as follows:
Massachusetts
You can also compare a column value in a CASE statement. For example:
SELECT Price =
CASE
WHEN UnitPrice IS NULL THEN 'Unknown'
WHEN UnitPrice < 10 THEN 'Less than $10'
WHEN UnitPrice = 10 THEN '$10'
ELSE 'Greater than $10'
END
FROM Products
You'll notice from this example that you can also supply a catchall ELSE condition in a

CASE statement.
Using WHILE Loops
You use a WHILE loop to run one or more statements multiple times. A WHILE loop
runs until a specified condition evaluates to false. The syntax for a WHILE loop is as
follows:
WHILE condition
statement

×