Introduction To Programming With T-SQL
Introduction To Programming With T-SQL
In this lab, you’ll use get an introduction to programming using T-SQL techniques
using the adventureworks database. For your reference, the following diagram
shows the tables in the database (you may need to resize the pane to see them
clearly).
CodeCopy
mynumber
8. In the query pane, type the following T-SQL code after the previous one:
CodeCopy
DECLARE
@num1 int,
@num2 int;
SET @num1 = 4;
SET @num2 = 6;
totalnum
10
11. You’ve now seen how to declare variables and how to retrieve values.
DECLARE
@empname nvarchar(30),
@empid int;
SET @empid = 5;
employee
Lucy Harrington
5. Change the @empid variable’s value from 5 to 2 and execute the modified T-
SQL code you’ll get:
employee
Keith Harris
6. Now, in the code you just copied, add the batch delimiter GO before this
statement:
CodeCopy
CodeCopy
DECLARE
@empname nvarchar(30),
@empid int;
SET @empid = 5;
SET @empname = (SELECT FirstName + N' ' + LastName FROM SalesLT.Customer
WHERE CustomerID = @empid)
GO
SELECT @empname AS employee;
Variables are local to the batch in which they’re defined. If you try to refer to a
variable that was defined in another batch, you get an error saying that the variable
wasn’t defined. Also, keep in mind that GO is a client command, not a server T-SQL
command.
CodeCopy
DECLARE
@i int = 8,
@result nvarchar(20);
IF @i < 5
SET @result = N'Less than 5'
ELSE IF @i <= 10
SET @result = N'Between 5 and 10'
ELSE if @i > 10
SET @result = N'More than 10'
ELSE
SET @result = N'Unknown';
result
Between 5 and 10
5. In the query pane, type the following T-SQL code after the previous code:
CodeCopy
DECLARE
@i int = 8,
@result nvarchar(20);
SET @result =
CASE
WHEN @i < 5 THEN
N'Less than 5'
WHEN @i <= 10 THEN
N'Between 5 and 10'
WHEN @i > 10 THEN
N'More than 10'
ELSE
N'Unknown'
END;
This code uses a CASE expression and only one SET expression to get the same result
as the previous T-SQL code. Remember to use a CASE expression when it’s a matter
of returning an expression. However, if you need to execute multiple statements, you
can’t replace IF with CASE.
result
Between 5 and 10
CodeCopy
DECLARE @i int = 1;
WHILE @i <= 10
BEGIN
PRINT @i;
SET @i = @i + 1;
END;
3. Highlight the written T-SQL code and select ⏵Run.
4. This will result in:
10
Challenges
Now it’s time to try using what you’ve learnt.
Tip: Try to determine the appropriate solutions for yourself. If you get stuck, suggested answers are
provided at the end of this lab.
You are developing a new T-SQL application that needs to temporarily store values
drawn from the database, and depending on their values, display the outcome to the
user.
1. Create your variables.
o Write a T-SQL statement to declare two variables. The first is an
nvarchar with length 30 called salesOrderNumber, and the other is an
integer called customerID.
2. Assign a value to the integer variable.
o Extend your TSQL code to assign the value 29847 to the customerID.
3. Assign a value from the database and display the result.
o Extend your TSQL to set the value of the variable salesOrderNumber
using the column salesOrderNUmber from the SalesOrderHeader
table, filter using the customerID column and the customerID variable.
Display the result to the user as OrderNumber.
The sales manager would like a list of the first 10 customers that registered and
made purchases online as part of a promotion. You’ve been asked to build the list.
Challenge Solutions
This section contains suggested solutions for the challenge queries.
Challenge 1
CodeCopy
DECLARE
@salesOrderNUmber nvarchar(30),
@customerID int;
CodeCopy
DECLARE
@salesOrderNUmber nvarchar(30),
@customerID int;
CodeCopy
DECLARE
@salesOrderNUmber nvarchar(30),
@customerID int;
Challenge 2
The sales manager would like a list of the first 10 customers that registered and
made purchases online as part of a promotion. You’ve been asked to build the list.
CodeCopy
3. Select the customer first name and last name and display:
CodeCopy