Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
10774A
Querying Microsoft® SQL Server® 2012
Companion Content
2 Querying Microsoft® SQL Server® 2012
Information in this document, including URL and other Internet Web site references, is subject to change
without notice. Unless otherwise noted, the example companies, organizations, products, domain names,
e-mail addresses, logos, people, places, and events depicted herein are fictitious, and no association with
any real company, organization, product, domain name, e-mail address, logo, person, place or event is
intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the
user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in
or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical,
photocopying, recording, or otherwise), or for any purpose, without the express written permission of
Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property
rights covering subject matter in this document. Except as expressly provided in any written license
agreement from Microsoft, the furnishing of this document does not give you any license to these
patents, trademarks, copyrights, or other intellectual property.
The names of manufacturers, products, or URLs are provided for informational purposes only and
Microsoft makes no representations and warranties, either expressed, implied, or statutory, regarding
these manufacturers or the use of the products with any Microsoft technologies. The inclusion of a
manufacturer or product does not imply endorsement of Microsoft of the manufacturer or product. Links
may be provided to third party sites. Such sites are not under the control of Microsoft and Microsoft is not
responsible for the contents of any linked site or any link contained in a linked site, or any changes or
updates to such sites. Microsoft is not responsible for webcasting or any other form of transmission
received from any linked site. Microsoft is providing these links to you only as a convenience, and the
inclusion of any link does not imply endorsement of Microsoft of the site or the products contained
therein.
© 2012 Microsoft Corporation. All rights reserved.
Microsoft, and Windows are either registered trademarks or trademarks of Microsoft Corporation in the
United States and/or other countries.
All other trademarks are property of their respective owners.
Released: 05/2012
Querying Microsoft® SQL Server® 2012 3
4 Querying Microsoft® SQL Server® 2012
Querying Microsoft® SQL Server® 2012 5
6 Querying Microsoft® SQL Server® 2012
Querying Microsoft® SQL Server® 2012 7
Module 1
Introduction to Microsoft SQL Server 2012
Contents:
Lesson 1: Introducing Microsoft SQL Server 2012 8
Lesson 1
Introducing Microsoft SQL Server 2012
Contents:
Additional Reading 9
Querying Microsoft® SQL Server® 2012 9
Additional Reading
SQL Server Editions
• SQL Server 2012 Editions guide
10 Querying Microsoft® SQL Server® 2012
Lesson 2
Getting Started with SQL Server Management Studio
Contents:
Detailed Demonstration Steps 11
Additional Reading 12
Querying Microsoft® SQL Server® 2012 11
2. If the Object Explorer pane is not visible, click View and click Object Explorer.
8. Show the list of columns, and point out the data type information for the ModifiedDate column.
9. If the Solution Explorer pane is not visible, click View and click Solution Explorer. It will be empty,
initially.
USE AdventureWorks2008R2;
GO
SELECT CustomerID, AccountNumber
FROM Sales.Customer;
Additional Reading
Starting SSMS
• Books Online
Querying Microsoft® SQL Server® 2012 13
Question: If no T-SQL code is selected in a script window, which lines will be run when you
click the Execute button?
Answer: Projects.
14 Querying Microsoft® SQL Server® 2012
Module 2
Getting Started with SQL Azure
Contents:
Lesson 1: Overview of SQL Azure 15
Lesson 1
Overview of SQL Azure
Contents:
Detailed Demonstration Steps 16
Additional Reading 17
16 Querying Microsoft® SQL Server® 2012
4. To create and populate the sample database on the SQL Azure server, open the script file 00 –
Setup.sql from within Solution Explorer and follow the instruction that are embedded as inline
comments.
Querying Microsoft® SQL Server® 2012 17
Additional Reading
What is SQL Azure?
• SQL Azure Landing Page
Key Distinctions Between SQL Server Azure and On-Premises SQL Server
• Transact-SQL Support (SQL Azure Database)
Question: How do you switch from one user database to another on a SQL Azure server?
Answer: If you are connected to the master database, you can switch to a user database the
database menu in SSMS, but not with the USE statement. If you are connected to a user
database, you must disconnect and reconnect to the other user database.
Question: What choices are there for creating a database on a SQL Azure server
Answer: Use the Azure Management Portal or connect to the master database in SSMS and
execute a script which creates the database.
Querying Microsoft® SQL Server® 2012 19
Module 3
Introduction to T-SQL Querying
Contents:
Lesson 1: Introducing T-SQL 20
Lesson 1
Introducing T-SQL
Contents:
Detailed Demonstration Steps 21
Additional Reading 22
Querying Microsoft® SQL Server® 2012 21
2. On the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script file from within
Solution Explorer.
4. Follow the instructions contained within the comments of the script file.
22 Querying Microsoft® SQL Server® 2012
Additional Reading
Categories of T-SQL Statements
• DML, DDL, and DCL commands can be found in Books Online
Lesson 2
Understanding Sets
Contents:
Additional Reading 24
24 Querying Microsoft® SQL Server® 2012
Additional Reading
Set Theory and SQL Server
• More information on the set theory and its application to SQL Server queries can be found in Chapter
1 of Itzik Ben-Gan’s Inside Microsoft® SQL Server® 2008: T-SQL Querying (Microsoft Press, 2009) and
Chapter 2 of Itzik Ben-Gan’s Microsoft SQL Server 2008: T-SQL Fundamentals (Microsoft Press, 2008).
For more information on the use of "Set of all…" see Joe Celko's Thinking in Sets (Morgan Kaufman,
2008).
Querying Microsoft® SQL Server® 2012 25
Lesson 3
Understanding Predicate Logic
Contents:
Additional Reading 26
26 Querying Microsoft® SQL Server® 2012
Additional Reading
Set Theory and SQL Server
• More information on the set theory and its application to SQL Server queries can be found in Chapter
1 of Itzik Ben-Gan’s Inside Microsoft® SQL Server® 2008: T-SQL Querying (Microsoft Press, 2009) and
Chapter 2 of Itzik Ben-Gan’s Microsoft SQL Server 2008: T-SQL Fundamentals (Microsoft Press, 2008).
For more information on the use of "Set of all…" see Joe Celko's Thinking in Sets (Morgan Kaufman,
2008).
Querying Microsoft® SQL Server® 2012 27
Lesson 4
Understanding the Logical Order of Operations in SELECT
Statements
Contents:
Detailed Demonstration Steps 28
28 Querying Microsoft® SQL Server® 2012
2. From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script file from
within Solution Explorer.
3. Open the 21 – Demonstration B.sql script file.
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 29
Answer: DML
Question: Which SELECT statement element will be processed before a WHERE clause?
Answer: FROM
30 Querying Microsoft® SQL Server® 2012
Module 4
Writing SELECT Queries
Contents:
Lesson 1: Writing Simple SELECT Statements 31
Lesson 1
Writing Simple SELECT Statements
Contents:
Detailed Demonstration Steps 32
Additional Reading 33
32 Querying Microsoft® SQL Server® 2012
2. On the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script file from
within Solution Explorer.
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 33
Additional Reading
Elements of the SELECT Statement
• SELECT elements
34 Querying Microsoft® SQL Server® 2012
Lesson 2
Eliminating Duplicates with DISTINCT
Contents:
Detailed Demonstration Steps 35
Querying Microsoft® SQL Server® 2012 35
4. Follow the instructions contained within the comments of the script file.
36 Querying Microsoft® SQL Server® 2012
Lesson 3
Using Column and Table Aliases
Contents:
Detailed Demonstration Steps 37
Querying Microsoft® SQL Server® 2012 37
2. From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script file from
within Solution Explorer.
4. Follow the instructions contained within the comments of the script file.
38 Querying Microsoft® SQL Server® 2012
Lesson 4
Writing Simple CASE Expressions
Contents:
Detailed Demonstration Steps 39
Querying Microsoft® SQL Server® 2012 39
2. From the View menu, click Solution Explorer. Open and execute the 00 – Setup.sql script file from
within Solution Explorer.
4. Follow the instructions contained within the comments of the script file.
40 Querying Microsoft® SQL Server® 2012
Answer: Looking for 2 answers: 1) * asks for all columns, which is typically too much; 2)
Query exposed to changes in underlying table structure.
Question: What will happen if you omit a comma between column names in a SELECT
clause?
Best Practices
1. Terminate all SQL statements with a semicolon. This will make your code more readable, avoid
certain parsing errors, and will protect your code against changes in future versions of SQL
Server.
2. Consider standardizing your code on the AS keyword for labeling column and table aliases. This
will make your code easier to read and avoids accidental aliases.
Querying Microsoft® SQL Server® 2012 41
Module 5
Querying Multiple Tables
Contents:
Lesson 1: Understanding Joins 42
Lesson 1
Understanding Joins
Contents:
Detailed Demonstration Steps 43
Querying Microsoft® SQL Server® 2012 43
4. Follow the instructions contained within the comments of the script file.
44 Querying Microsoft® SQL Server® 2012
Lesson 2
Querying with Inner Joins
Contents:
Detailed Demonstration Steps 45
Querying Microsoft® SQL Server® 2012 45
4. Follow the instructions contained within the comments of the script file.
46 Querying Microsoft® SQL Server® 2012
Lesson 3
Querying with Outer Joins
Contents:
Detailed Demonstration Steps 47
Querying Microsoft® SQL Server® 2012 47
4. Follow the instructions contained within the comments of the script file.
48 Querying Microsoft® SQL Server® 2012
Lesson 4
Querying with Cross Joins and Self-Joins
Contents:
Detailed Demonstration Steps 49
Querying Microsoft® SQL Server® 2012 49
4. Follow the instructions contained within the comments of the script file.
50 Querying Microsoft® SQL Server® 2012
Answer: An inner join filters out rows which do not satisfy the predicate in the ON clause. An
outer join includes all rows from both tables and includes NULLs for attributes where no
match is found.
Best Practices
1. Table aliases should always be defined when joining tables.
2. Joins should be expressed using SQL-92 syntax, with JOIN and ON keywords.
Querying Microsoft® SQL Server® 2012 51
Module 6
Sorting and Filtering Data
Contents:
Lesson 1: Sorting Data 52
Lesson 1
Sorting Data
Contents:
Question and Answers 53
Additional Reading 55
Querying Microsoft® SQL Server® 2012 53
Answer: No.
54 Querying Microsoft® SQL Server® 2012
3. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 55
Additional Reading
Using the ORDER BY Clause
• ORDER BY Clause (Transact-SQL)
56 Querying Microsoft® SQL Server® 2012
Lesson 2
Filtering Data with Predicates
Contents:
Detailed Demonstration Steps 57
Querying Microsoft® SQL Server® 2012 57
3. Follow the instructions contained within the comments of the script file.
58 Querying Microsoft® SQL Server® 2012
Lesson 3
Filtering Data with TOP and OFFSET-FETCH
Contents:
Detailed Demonstration Steps 59
Additional Reading 60
Querying Microsoft® SQL Server® 2012 59
3. Follow the instructions contained within the comments of the script file.
60 Querying Microsoft® SQL Server® 2012
Additional Reading
Filtering in the SELECT Clause Using the TOP Option
• TOP (Transact-SQL)
Lesson 4
Working with Unknown Values
Contents:
Detailed Demonstration Steps 62
62 Querying Microsoft® SQL Server® 2012
3. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 63
Question: Can column aliases defined in a SELECT clause be used in an ORDER BY clause?
Why or why not?
Question: What is the relationship between the ORDER BY clause and an OFFSET-FETCH
clause?
Module 7
Working with SQL Server 2012 Data Types
Contents:
Lesson 1: Introducing SQL Server 2012 Data Types 65
Lesson 1
Introducing SQL Server 2012 Data Types
Contents:
Detailed Demonstration Steps 66
Additional Reading 67
66 Querying Microsoft® SQL Server® 2012
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 67
Additional Reading
Numeric Data Types
• decimal and numeric (Transact SQL)
• See course 10776: Developing Microsoft® SQL Server® 2012 Databases for additional information
about the hierarchyid data type.
Lesson 2
Working with Character Data
Contents:
Detailed Demonstration Steps 69
Additional Reading 70
Querying Microsoft® SQL Server® 2012 69
4. Follow the instructions contained within the comments of the script file.
70 Querying Microsoft® SQL Server® 2012
Additional Reading
String Concatenation
• CONCAT (Transact-SQL)
Lesson 3
Working with Date and Time Data
Contents:
Detailed Demonstration Steps 72
72 Querying Microsoft® SQL Server® 2012
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 73
Question: What data type is suitable for storing flag information, such as TRUE or FALSE?
Answer: bit
Question: What logical operators are useful for retrieving ranges of date and time values?
Module 8
Using Built-In Functions
Contents:
Lesson 1: Writing Queries with Built-In Functions 75
Lesson 1
Writing Queries with Built-In Functions
Contents:
Detailed Demonstration Steps 76
Additional Reading 77
76 Querying Microsoft® SQL Server® 2012
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 77
Additional Reading
Scalar Functions
• Built-in Functions (Transact-SQL)
78 Querying Microsoft® SQL Server® 2012
Lesson 2
Using Conversion Functions
Contents:
Detailed Demonstration Steps 79
Additional Reading 80
Querying Microsoft® SQL Server® 2012 79
4. Follow the instructions contained within the comments of the script file.
80 Querying Microsoft® SQL Server® 2012
Additional Reading
Implicit and Explicit Data Type Conversions
• CAST and CONVERT (Transact-SQL)
Lesson 3
Using Logical Functions
Contents:
Question and Answers 82
Additional Reading 84
82 Querying Microsoft® SQL Server® 2012
Answer: Answers will vary, but may include logic to replace invalid data with a substitute.
Querying Microsoft® SQL Server® 2012 83
4. Follow the instructions contained within the comments of the script file.
84 Querying Microsoft® SQL Server® 2012
Additional Reading
Performing Conditional Tests with IIF
• IIF (Transact-SQL)
Lesson 4
Using Functions to Work with NULL
Contents:
Detailed Demonstration Steps 86
Additional Reading 87
86 Querying Microsoft® SQL Server® 2012
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 87
Additional Reading
Converting NULL with ISNULL
• ISNULL (Transact-SQL)
Module 9
Grouping and Aggregating Data
Contents:
Lesson 1: Using Aggregate Functions 89
Lesson 1
Using Aggregate Functions
Contents:
Detailed Demonstration Steps 90
Additional Reading 91
90 Querying Microsoft® SQL Server® 2012
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 91
Additional Reading
Working with Aggregate Functions
• Aggregate Functions (Transact-SQL)
Lesson 2
Using the GROUP BY Clause
Contents:
Detailed Demonstration Steps 93
Additional Reading 94
Querying Microsoft® SQL Server® 2012 93
4. Follow the instructions contained within the comments of the script file.
94 Querying Microsoft® SQL Server® 2012
Additional Reading
Using the GROUP BY Clause
• GROUP BY (Transact SQL)
Lesson 3
Filtering Groups with HAVING
Contents:
Detailed Demonstration Steps 96
Additional Reading 97
96 Querying Microsoft® SQL Server® 2012
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 97
Additional Reading
Filtering Grouped Data Using the HAVING clause
• HAVING (Transact-SQL)
98 Querying Microsoft® SQL Server® 2012
Question: Can a WHERE clause and a HAVING clause in a query filter on the same column?
Answer: Yes.
Querying Microsoft® SQL Server® 2012 99
Module 10
Using Subqueries
Contents:
Lesson 1: Writing Self-Contained Subqueries 100
Lesson 1
Writing Self-Contained Subqueries
Contents:
Detailed Demonstration Steps 101
4. Follow the instructions contained within the comments of the script file.
102 Querying Microsoft® SQL Server® 2012
Additional Reading
Working with Subqueries
• Subquery Fundamentals
Lesson 2
Writing Correlated Subqueries
Contents:
Question and Answers 104
Answer: The subquery depends on input from the outer query for its values.
Querying Microsoft® SQL Server® 2012 105
4. Follow the instructions contained within the comments of the script file.
106 Querying Microsoft® SQL Server® 2012
Additional Reading
Writing Correlated Subqueries
• Correlated Subqueries
Querying Microsoft® SQL Server® 2012 107
Lesson 3
Using the EXISTS Predicate with Subqueries
Contents:
Detailed Demonstration Steps 108
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 109
Additional Reading
Writing Queries Using EXISTS with Subqueries
• Subqueries with EXISTS
110 Querying Microsoft® SQL Server® 2012
Answer: Yes.
Question: What columns should appear in the SELECT list of a subquery following the EXISTS
predicate?
Module 11
Using Table Expressions
Contents:
Lesson 1: Using Views 112
Lesson 1
Using Views
Contents:
Detailed Demonstration Steps 113
4. Follow the instructions contained within the comments of the script file.
114 Querying Microsoft® SQL Server® 2012
Additional Reading
Writing Queries That Return Results From Views
• For more information on database security, see Microsoft course 10775: Administering Microsoft®
SQL Server® 2012 Databases.
Querying Microsoft® SQL Server® 2012 115
Lesson 2
Using Inline Table-Valued Functions
Contents:
Detailed Demonstration Steps 116
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 117
Additional Reading
Writing Queries That Use Inline Table-Valued Functions
• CREATE FUNCTION (Transact-SQL)
118 Querying Microsoft® SQL Server® 2012
Lesson 3
Using Derived Tables
Contents:
Detailed Demonstration Steps 119
Querying Microsoft® SQL Server® 2012 119
4. Follow the instructions contained within the comments of the script file.
120 Querying Microsoft® SQL Server® 2012
Lesson 4
Using Common Table Expressions
Contents:
Detailed Demonstration Steps 121
4. Follow the instructions contained within the comments of the script file.
122 Querying Microsoft® SQL Server® 2012
Additional Reading
Writing Queries with Common Table Expressions
• Recursive Queries Using Common Table Expression
Querying Microsoft® SQL Server® 2012 123
Question: Which table expressions allow variables to be passed in as parameters to the expression?
Module 12
Using Set Operators
Contents:
Lesson 1: Writing Queries with the UNION Operator 125
Lesson 1
Writing Queries with the UNION Operator
Contents:
Detailed Demonstration Steps 126
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 127
Additional Reading
Using the UNION Operator
• UNION (Transact-SQL)
128 Querying Microsoft® SQL Server® 2012
Lesson 2
Using EXCEPT and INTERSECT
Contents:
Detailed Demonstration Steps 129
4. Follow the instructions contained within the comments of the script file.
130 Querying Microsoft® SQL Server® 2012
Additional Reading
Using the INTERSECT Operator
• EXCEPT and INTERSECT (Transact-SQL)
Querying Microsoft® SQL Server® 2012 131
Lesson 3
Using APPLY
Contents:
Detailed Demonstration Steps 132
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 133
Additional Reading
Using the APPLY Operator
• FROM (Transact-SQL)
• Using APPLY
134 Querying Microsoft® SQL Server® 2012
Question: Which APPLY form will not return rows from the left table if the result of the right table
expression was empty?
Module 13
Using Window Ranking, Offset, and Aggregate Functions
Contents:
Lesson 1: Creating Windows with OVER 136
Lesson 1
Creating Windows with OVER
Contents:
Detailed Demonstration Steps 137
4. Follow the instructions contained within the comments of the script file.
138 Querying Microsoft® SQL Server® 2012
Additional Reading
Using OVER
• OVER Clause (Transact-SQL)
Querying Microsoft® SQL Server® 2012 139
Lesson 2
Exploring Window Functions
Contents:
Detailed Demonstration Steps 140
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 141
Additional Reading
Window Ranking Functions
• Ranking Functions (Transact-SQL)
142 Querying Microsoft® SQL Server® 2012
Question: Which ranking function would you use to return the values 1,1,3? Which would return
1,1,2?
Question: Can a window frame extend beyond the boundaries of the window partition defined in the
same OVER() clause?
Answer: No.
Querying Microsoft® SQL Server® 2012 143
Module 14
Pivoting and Grouping Sets
Contents:
Lesson 1: Writing Queries with PIVOT and UNPIVOT 144
Lesson 1
Writing Queries with PIVOT and UNPIVOT
Contents:
Detailed Demonstration Steps 145
4. Follow the instructions contained within the comments of the script file.
146 Querying Microsoft® SQL Server® 2012
Additional Reading
What is Pivoting?
• Using PIVOT and UNPIVOT
Querying Microsoft® SQL Server® 2012 147
Lesson 2
Working with Grouping Sets
Contents:
Detailed Demonstration Steps 148
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 149
Additional Reading
Writing Queries with Grouping Sets
• Using GROUP BY with ROLLUP, CUBE, and GROUPING SETS
150 Querying Microsoft® SQL Server® 2012
Question: What are the possible sources of NULLs returned by a query using grouping sets to create
aggregations?
Answer: NULLs might be present in the underlying source data, or may be placeholders for rows that
do not participate in the group member.
Question: Which subclause infers a hierarchy of columns to create meaningful grouping sets?
Answer: ROLLUP.
Querying Microsoft® SQL Server® 2012 151
Module 15
Querying SQL Server Metadata
Contents:
Lesson 1: Querying System Catalog Views and Functions 152
Lesson 1
Querying System Catalog Views and Functions
Contents:
Detailed Demonstration Steps 153
4. Follow the instructions contained within the comments of the script file.
154 Querying Microsoft® SQL Server® 2012
Additional Reading
System Catalog Views
• Querying the SQL Server System Catalog FAQ
Lesson 2
Executing System Stored Procedures
Contents:
Detailed Demonstration Steps 156
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 157
Additional Reading
Executing Stored Procedures
• Execute a Stored Procedure
Lesson 3
Querying Dynamic Management Objects
Contents:
Detailed Demonstration Steps 159
4. Follow the instructions contained within the comments of the script file.
160 Querying Microsoft® SQL Server® 2012
Additional Reading
About Dynamic Management Objects
• Dynamic Management Views and Functions (Transact-SQL)
Querying Microsoft® SQL Server® 2012 161
Answer: Views may be filtered, joined and further processed, unlike procedures.
Question: What issues might you face later if your application used SELECT * to query system catalog
views?
Answer: System views may be changed in future releases of SQL Server and columns may be added.
162 Querying Microsoft® SQL Server® 2012
Module 16
Using Table Expressions
Contents:
Lesson 1: Querying Data with Stored Procedures 163
Lesson 1
Querying Data with Stored Procedures
Contents:
Detailed Demonstration Steps 164
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 165
Additional Reading
Examining Stored Procedures
• Stored Procedures (Database Engine)
Lesson 2
Passing Parameters to Stored Procedures
Contents:
Detailed Demonstration Steps 167
4. Follow the instructions contained within the comments of the script file.
168 Querying Microsoft® SQL Server® 2012
Additional Reading
Passing Input Parameters to Stored Procedures
• Specify Parameters
Querying Microsoft® SQL Server® 2012 169
Lesson 3
Creating Simple Stored Procedures
Contents:
Detailed Demonstration Steps 170
170 Querying Microsoft® SQL Server® 2012
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 171
Lesson 4
Working with Dynamic SQL
Contents:
Detailed Demonstration Steps 172
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 173
Additional Reading
Constructing Dynamic SQL
• EXECUTE (Transact-SQL)
• SQL Injection
Answer: Answers may vary, but ability to accept parameters is what we're looking for.
Question: What form should parameter and value pairs take when passed to a stored procedure in
the EXECUTE statement?
Question: Which method for constructing dynamic SQL allows parameters to be passed at runtime?
Module 17
Programming with T-SQL
Contents:
Lesson 1: T-SQL Programming Elements 176
Lesson 1
T-SQL Programming Elements
Contents:
Detailed Demonstration Steps 177
4. Follow the instructions contained within the comments of the script file.
178 Querying Microsoft® SQL Server® 2012
Additional Reading
Introducing T-SQL Batches
• Batches
Lesson 2
Controlling Program Flow
Contents:
Detailed Demonstration Steps 180
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 181
Additional Reading
Understanding T-SQL Control-of-Flow Language
• Control-of-Flow Language (Transact-SQL)
Answer: No, variables are local to the batch in which they are declared.
Question: Can a synonym be created that references an object that doesn’t exist?
Question: Will a WHILE loop exit when the predicate evaluates to NULL?
Answer: Yes.
Querying Microsoft® SQL Server® 2012 183
Module 18
Implementing Error Handling
Contents:
Lesson 1: Using TRY / CATCH Blocks 184
Lesson 1
Using TRY / CATCH Blocks
Contents:
Detailed Demonstration Steps 185
4. Follow the instructions contained within the comments of the script file.
186 Querying Microsoft® SQL Server® 2012
Additional Reading
Structured Exception Handling
• Using TRY...CATCH in Transact-SQL
Querying Microsoft® SQL Server® 2012 187
Lesson 2
Working with Error Information
Contents:
Detailed Demonstration Steps 188
188 Querying Microsoft® SQL Server® 2012
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 189
Answer: Yes.
Module 19
Implementing Transactions
Contents:
Lesson 1: Transactions and the Database Engine 191
Lesson 1
Transactions and the Database Engine
Contents:
Detailed Demonstration Steps 192
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 193
Additional Reading
Defining Transactions
• Transaction Statements (Transact-SQL)
• For more information on locking, see Microsoft course 10776: Developing Microsoft® SQL Server®
2012 Databases.
194 Querying Microsoft® SQL Server® 2012
Lesson 2
Controlling Transactions
Contents:
Detailed Demonstration Steps 195
4. Follow the instructions contained within the comments of the script file.
196 Querying Microsoft® SQL Server® 2012
Additional Reading
BEGIN TRANSACTION
• BEGIN TRANSACTION (Transact-SQL)
• Nesting Transactions
ROLLBACK TRANSACTION
• Transaction Statements (Transact-SQL)
Using XACT_ABORT
• SET XACT_ABORT (Transact-SQL)
Querying Microsoft® SQL Server® 2012 197
Answer: The inner transaction is also rolled back. Therefore nested transactions are not typically
useful in user code.
Question: When a runtime error occurs in a transaction and SET XACT_ABORT is ON, is the
transaction always automatically rolled back?
Module 20
Improving Query Performance
Contents:
Lesson 1: Factors in Query Performance 199
Lesson 1
Factors in Query Performance
Contents:
Detailed Demonstration Steps 200
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 201
Additional Reading
Writing Well-Performing Queries
• Query Performance
Distribution Statistics
• Statistics Used by the Query Optimizer in Microsoft SQL Server 2008
Defining Cursors
• Cursors
202 Querying Microsoft® SQL Server® 2012
Lesson 2
Displaying Query Performance Data
Contents:
Detailed Demonstration Steps 203
4. Follow the instructions contained within the comments of the script file.
204 Querying Microsoft® SQL Server® 2012
Additional Reading
What Is an Execution Plan?
• Display and Save Execution Plans
Answer: Answers will vary, but should include performance and risk of changes to table design
breaking a client application
Answer: One.
Question: Which type of execution plan can be displayed without running a query?
Answer: Estimated.
206 Querying Microsoft® SQL Server® 2012
Appendix A
Using DML to Modify Data
Contents:
Lesson 1: Adding Data to Tables 207
Lesson 1
Adding Data to Tables
Contents:
Additional Reading 208
208 Querying Microsoft® SQL Server® 2012
Additional Reading
Using INSERT to Add Data
• INSERT (Transact-SQL)
Lesson 2
Modifying and Removing Data
Contents:
Additional Reading 210
210 Querying Microsoft® SQL Server® 2012
Additional Reading
Using UPDATE to Modify Data
• Books Online
Lesson 3
Generating Numbers
Contents:
Additional Reading 212
212 Querying Microsoft® SQL Server® 2012
Additional Reading
Using IDENTITY
• SET IDENTITY_INSERT (Transact-SQL)
Using Sequences
• CREATE SEQUENCE (Transact-SQL)
Querying Microsoft® SQL Server® 2012 213
Question: The presence of what constraint prevents TRUNCATE TABLE from executing?
Appendix B
Using DDL to Create and Modify Objects
Contents:
Lesson 1: Creating, Altering, and Dropping Tables 215
Lesson 1
Creating, Altering, and Dropping Tables
Contents:
Additional Reading 216
216 Querying Microsoft® SQL Server® 2012
Additional Reading
Defining Tables
• CREATE TABLE (Transact-SQL)
Lesson 2
Creating, Altering and Dropping Views
Contents:
Additional Reading 218
218 Querying Microsoft® SQL Server® 2012
Additional Reading
Defining Views
• CREATE VIEW (Transact-SQL)
Answer: Yes, unless the view is defined with the SCHEMABINDING option.
220 Querying Microsoft® SQL Server® 2012
Appendix C
Using Table Expressions
Contents:
Lesson 2: Querying Full-Text Data 221
Lesson 2
Querying Full-Text Data
Contents:
Detailed Demonstration Steps 222
222 Querying Microsoft® SQL Server® 2012
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 223
Lesson 3
Working with XML Data
Contents:
Additional Reading 224
224 Querying Microsoft® SQL Server® 2012
Additional Reading
Components of XML
• The complete XML specification
Querying Microsoft® SQL Server® 2012 225
Lesson 4
Converting Query Results to XML with FOR XML
Contents:
Detailed Demonstration Steps 226
4. Follow the instructions contained within the comments of the script file.
Querying Microsoft® SQL Server® 2012 227
Additional Reading
Using FOR XML in a SELECT Statement
• Use FOR XML and OPENXML to Publish and Process XML Data
Lesson 5
Querying XML Data
Contents:
Detailed Demonstration Steps 229
4. Follow the instructions contained within the comments of the script file.
230 Querying Microsoft® SQL Server® 2012
Additional Reading
Working with the XML Data Type
• xml data type methods
Querying Microsoft® SQL Server® 2012 231
Note Not all training products will have a Knowledge Base article – if that is the case,
please ask your instructor whether or not there are existing error log entries.
Courseware Feedback
Send all courseware feedback to support@mscourseware.com. We truly appreciate your time and effort.
We review every e-mail received and forward the information on to the appropriate team. Unfortunately,
because of volume, we are unable to provide a response but we may use your feedback to improve your
future experience with Microsoft Learning products.
Reporting Errors
When providing feedback, include the training product name and number in the subject line of your e-
mail. When you provide comments or report bugs, please include the following:
Please provide any details that are necessary to help us verify the issue.
Important All errors and suggestions are evaluated, but only those that are validated are
added to the product Knowledge Base article.