Академический Документы
Профессиональный Документы
Культура Документы
Introduction to T-SQL
Querying
Module Overview
Introducing T-SQL
Understanding Sets
Understanding Predicate Logic
Understanding the Logical Order of Operations in SELECT
Statements
Lesson 1: Introducing T-SQL
About T-SQL
Categories of T-SQL Statements
T-SQL Language Elements
T-SQL Language Elements: Predicates and Operators
T-SQL Language Elements: Functions
T-SQL Language Elements: Variables
T-SQL Language Elements: Expressions
T-SQL Language Elements: Control of Flow, Errors, and
Transactions
T-SQL Language Elements: Comments
T-SQL Language Elements: Batch Separators
About T-SQL
Structured Query Language (SQL)
Developed by IBM in 1970s
Adopted as a standard by ANSI and ISO standards bodies
Widely used in industry
PL/SQL, SQL Procedural Language, Transact-SQL
Microsofts implementation is Transact-SQL
Referred to as T-SQL
Query language for SQL Server 2012
SQL is declarative, not procedural
Describe what you want, dont specify steps
Categories of T-SQL Statements
Predicates
Predicates and
and Batch
Batch Separators
Separators
Operators
Operators
Functions
Functions Control
Control of
of Flow
Flow
Variables
Variables Comments
Comments
Expressions
Expressions
T-SQL Language Elements: Predicates and Operators
Predicates and
Elements:
Operators:
Arithmetic Operators +, -, *, /, %
Concatenation +
DECLARE
DECLARE @MyVar
@MyVar int
int =
= 30;
30;
T-SQL Language Elements: Expressions
Combination of identifiers, values, and operators evaluated
to obtain a single result
Can be used in SELECT statements
SELECT clause
WHERE clause
Can be single constant, single-valued function, or variable
Can be combined if expressions have same the data type
SELECT
SELECT YEAR(orderdate)
YEAR(orderdate) +
+ 1 ...
...
SELECT
SELECT qty
qty *
* unitprice
unitprice ...
...
T-SQL Language Elements: Control of Flow,
Errors, and Transactions
Allow you to control the flow of execution within code,
handle errors, and maintain transactions
Used in programmatic code objects
Stored procedures, triggers, statement blocks
IF...ELSE TRY...CATCH
TRY...CATCH BEGIN
WHILE TRANSACTION
BREAK COMMIT
CONTINUE TRANSACTION
BEGIN...END ROLLBACK
TRANSACTION
T-SQL Language Elements: Comments
Marks T-SQL code as a comment:
For a block, enclose it between /* and */ characters
/*
/*
This is a block
of
of commented
commented code
code
*/
For inline text, precede the comments with --
--
-- This
This line
line of
of text
text will
will be
be ignored
ignored
T-SQL Editors such as SSMS will typically color-code
comments, as shown above
T-SQL Language Elements: Batch Separators
Batches are sets of commands sent to SQL Server as
a unit
Batches determine variable scope, name resolution
To separate statements into batches, use a separator:
SQL Server tools use the GO keyword
GO is not a SQL Server T-SQL command!
GO n times feature added in SQL Server 2005
Demonstration: T-SQL Language Elements
In this demonstration, you will see how to use some of the
T-SQL language elements covered in this lesson.
Note that some elements will be covered in more depth in
later modules. They are provided for illustration only in the
queries.
Lesson 2: Understanding Sets
Set Theory and SQL Server
Set Theory Applied to SQL Server Queries
Set Theory and SQL Server
Set theory is a mathematical basis for the relational
database model
What is a set?
A collection of distinct objects considered as a whole
"All customers who live in Portland"
Characteristics of Set
Example
Element
Filtering
Filtering data
data in
in queries
queries (WHERE
(WHERE and
and HAVING
HAVING clauses)
clauses)
Providing
Providing conditional
conditional logic
logic to
to CASE
CASE expressions
expressions
Joining
Joining tables
tables (ON
(ON filter)
filter)
Defining
Defining subqueries
subqueries
Enforcing
Enforcing data
data integrity
integrity (CHECK
(CHECK constraints)
constraints)
Control
Control of
of flow
flow (IF
(IF statement)
statement)
Lesson 4: Understanding the Logical Order of
Operations in SELECT Statements
Elements of a SELECT Statement
Logical Query Processing
Applying the Logical Order of Operations to Writing
SELECT Statements
Elements of a SELECT Statement
5:
5: SELECT
SELECT <select
<select list>
list>
1:
1: FROM
FROM <table
<table source>
source>
2:
2: WHERE
WHERE <search
<search condition>
condition>
3:
3: GROUP
GROUP BY
BY <group
<group by
by list>
list>
4:
4: HAVING
HAVING <search
<search condition>
condition>
6:
6: ORDER
ORDER BY
BY <order
<order by
by list>
list>
Applying the Logical Order of Operations to
Writing SELECT Statements
USE
USE TSQL2012;
TSQL2012;
SELECT
SELECT empid,
empid, YEAR(orderdate)
YEAR(orderdate) AS
AS orderyear
orderyear
FROM
FROM Sales.Orders
Sales.Orders
WHERE
WHERE custid
custid =71
=71
GROUP
GROUP BY
BY empid,
empid, YEAR(orderdate)
YEAR(orderdate)
HAVING
HAVING COUNT(*)
COUNT(*) > 11
ORDER
ORDER BY
BY empid,
empid, orderyear;
orderyear;
Demonstration: Logical Query Processing
In this demonstration, you will see the output of queries
illustrating the logical order of query processing
Lab: Introduction to T-SQL Querying
Exercise 1: Executing Basic SELECT Statements
Exercise 2: Executing Queries That Filter Data Using
Predicates
Exercise 3: Executing Queries That Sort Data Using
ORDER BY
Logon information
Password Pa$$w0rd