Sei sulla pagina 1di 288

M I C R O S O F T

L E A R N I N G

10776A
Developing Microsoft

Databases

P R O D U C T

SQL Server 2012

MCT USE ONLY. STUDENT USE PROHIBITED

O F F I C I A L

Developing Microsoft SQL Server 2012 Databases

MCT USE ONLY. STUDENT USE PROHIBITED

ii

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 the trademarks listed at
http://www.microsoft.com/about/legal/en/us/IntellectualProperty/Trademarks/EN-US.aspx are trademarks of
the Microsoft group of companies. All other trademarks are property of their respective owners

Product Number: 10776A


Part Number: X18-28011
Released: 05/2012

MCT USE ONLY. STUDENT USE PROHIBITED

MICROSOFT LICENSE TERMS


OFFICIAL MICROSOFT LEARNING PRODUCTS
MICROSOFT OFFICIAL COURSE Pre-Release and Final Release Versions

These license terms are an agreement between Microsoft Corporation and you. Please read them. They apply to
the Licensed Content named above, which includes the media on which you received it, if any. These license
terms also apply to any updates, supplements, internet based services and support services for the Licensed
Content, unless other terms accompany those items. If so, those terms apply.
BY DOWNLOADING OR USING THE LICENSED CONTENT, YOU ACCEPT THESE TERMS. IF YOU DO NOT ACCEPT
THEM, DO NOT DOWNLOAD OR USE THE LICENSED CONTENT.
If you comply with these license terms, you have the rights below.
1.

DEFINITIONS.

a. Authorized Learning Center means a Microsoft Learning Competency Member, Microsoft IT Academy
Program Member, or such other entity as Microsoft may designate from time to time.
b. Authorized Training Session means the Microsoft-authorized instructor-led training class using only
MOC Courses that are conducted by a MCT at or through an Authorized Learning Center.

c. Classroom Device means one (1) dedicated, secure computer that you own or control that meets or
exceeds the hardware level specified for the particular MOC Course located at your training facilities or
primary business location.
d. End User means an individual who is (i) duly enrolled for an Authorized Training Session or Private
Training Session, (ii) an employee of a MPN Member, or (iii) a Microsoft full-time employee.
e. Licensed Content means the MOC Course and any other content accompanying this agreement.
Licensed Content may include (i) Trainer Content, (ii) sample code, and (iii) associated media.
f.

Microsoft Certified Trainer or MCT means an individual who is (i) engaged to teach a training session
to End Users on behalf of an Authorized Learning Center or MPN Member, (ii) currently certified as a
Microsoft Certified Trainer under the Microsoft Certification Program, and (iii) holds a Microsoft
Certification in the technology that is the subject of the training session.

g. Microsoft IT Academy Member means a current, active member of the Microsoft IT Academy
Program.

h. Microsoft Learning Competency Member means a Microsoft Partner Network Program Member in
good standing that currently holds the Learning Competency status.
i.

Microsoft Official Course or MOC Course means the Official Microsoft Learning Product instructorled courseware that educates IT professionals or developers on Microsoft technologies.

MCT USE ONLY. STUDENT USE PROHIBITED

j.

Microsoft Partner Network Member or MPN Member means a silver or gold-level Microsoft Partner
Network program member in good standing.

k. Personal Device means one (1) device, workstation or other digital electronic device that you
personally own or control that meets or exceeds the hardware level specified for the particular MOC
Course.
l. Private Training Session means the instructor-led training classes provided by MPN Members for
corporate customers to teach a predefined learning objective. These classes are not advertised or
promoted to the general public and class attendance is restricted to individuals employed by or
contracted by the corporate customer.

m. Trainer Content means the trainer version of the MOC Course and additional content designated
solely for trainers to use to teach a training session using a MOC Course. Trainer Content may include
Microsoft PowerPoint presentations, instructor notes, lab setup guide, demonstration guides, beta
feedback form and trainer preparation guide for the MOC Course. To clarify, Trainer Content does not
include virtual hard disks or virtual machines.
2.

INSTALLATION AND USE RIGHTS. The Licensed Content is licensed not sold. The Licensed Content is
licensed on a one copy per user basis, such that you must acquire a license for each individual that
accesses or uses the Licensed Content.
2.1

Below are four separate sets of installation and use rights. Only one set of rights apply to you.

a. If you are a Authorized Learning Center:


i. If the Licensed Content is in digital format for each license you acquire you may either:
1. install one (1) copy of the Licensed Content in the form provided to you on a dedicated, secure
server located on your premises where the Authorized Training Session is held for access and
use by one (1) End User attending the Authorized Training Session, or by one (1) MCT teaching
the Authorized Training Session, or
2. install one (1) copy of the Licensed Content in the form provided to you on one (1) Classroom
Device for access and use by one (1) End User attending the Authorized Training Session, or by
one (1) MCT teaching the Authorized Training Session.
ii. You agree that:
1. you will acquire a license for each End User and MCT that accesses the Licensed Content,
2. each End User and MCT will be presented with a copy of this agreement and each individual
will agree that their use of the Licensed Content will be subject to these license terms prior to
their accessing the Licensed Content. Each individual will be required to denote their
acceptance of the EULA in a manner that is enforceable under local law prior to their accessing
the Licensed Content,
3. for all Authorized Training Sessions, you will only use qualified MCTs who hold the applicable
competency to teach the particular MOC Course that is the subject of the training session,
4. you will not alter or remove any copyright or other protective notices contained in the
Licensed Content,

MCT USE ONLY. STUDENT USE PROHIBITED

5. you will remove and irretrievably delete all Licensed Content from all Classroom Devices and
servers at the end of the Authorized Training Session,
6. you will only provide access to the Licensed Content to End Users and MCTs,
7. you will only provide access to the Trainer Content to MCTs, and
8. any Licensed Content installed for use during a training session will be done in accordance
with the applicable classroom set-up guide.

b. If you are a MPN Member.


i. If the Licensed Content is in digital format for each license you acquire you may either:
1. install one (1) copy of the Licensed Content in the form provided to you on (A) one (1)
Classroom Device, or (B) one (1) dedicated, secure server located at your premises where
the training session is held for use by one (1) of your employees attending a training session
provided by you, or by one (1) MCT that is teaching the training session, or
2. install one (1) copy of the Licensed Content in the form provided to you on one (1)
Classroom Device for use by one (1) End User attending a Private Training Session, or one (1)
MCT that is teaching the Private Training Session.
ii. You agree that:
1. you will acquire a license for each End User and MCT that accesses the Licensed Content,
2. each End User and MCT will be presented with a copy of this agreement and each individual
will agree that their use of the Licensed Content will be subject to these license terms prior
to their accessing the Licensed Content. Each individual will be required to denote their
acceptance of the EULA in a manner that is enforceable under local law prior to their
accessing the Licensed Content,
3. for all training sessions, you will only use qualified MCTs who hold the applicable
competency to teach the particular MOC Course that is the subject of the training session,
4. you will not alter or remove any copyright or other protective notices contained in the
Licensed Content,
5. you will remove and irretrievably delete all Licensed Content from all Classroom Devices and
servers at the end of each training session,
6. you will only provide access to the Licensed Content to End Users and MCTs,
7. you will only provide access to the Trainer Content to MCTs, and
8. any Licensed Content installed for use during a training session will be done in accordance
with the applicable classroom set-up guide.
c. If you are an End User:
You may use the Licensed Content solely for your personal training use. If the Licensed Content is in
digital format, for each license you acquire you may (i) install one (1) copy of the Licensed Content in
the form provided to you on one (1) Personal Device and install another copy on another Personal
Device as a backup copy, which may be used only to reinstall the Licensed Content; or (ii) print one (1)
copy of the Licensed Content. You may not install or use a copy of the Licensed Content on a device
you do not own or control.

MCT USE ONLY. STUDENT USE PROHIBITED

d. If you are a MCT.


i. For each license you acquire, you may use the Licensed Content solely to prepare and deliver an
Authorized Training Session or Private Training Session. For each license you acquire, you may
install and use one (1) copy of the Licensed Content in the form provided to you on one (1) Personal
Device and install one (1) additional copy on another Personal Device as a backup copy, which may
be used only to reinstall the Licensed Content. You may not install or use a copy of the Licensed
Content on a device you do not own or control.
ii.

Use of Instructional Components in Trainer Content. You may customize, in accordance with the
most recent version of the MCT Agreement, those portions of the Trainer Content that are logically
associated with instruction of a training session. If you elect to exercise the foregoing rights, you
agree: (a) that any of these customizations will only be used for providing a training session, (b) any
customizations will comply with the terms and conditions for Modified Training Sessions and
Supplemental Materials in the most recent version of the MCT agreement and with this agreement.
For clarity, any use of customize refers only to changing the order of slides and content, and/or
not using all the slides or content, it does not mean changing or modifying any slide or content.

2.2 Separation of Components. The Licensed Content components are licensed as a single unit and you
may not separate the components and install them on different devices.

2.3 Reproduction/Redistribution Licensed Content. Except as expressly provided in the applicable


installation and use rights above, you may not reproduce or distribute the Licensed Content or any portion
thereof (including any permitted modifications) to any third parties without the express written permission
of Microsoft.

2.4 Third Party Programs. The Licensed Content may contain third party programs or services. These
license terms will apply to your use of those third party programs or services, unless other terms accompany
those programs and services.
2.5 Additional Terms. Some Licensed Content may contain components with additional terms,
conditions, and licenses regarding its use. Any non-conflicting terms in those conditions and licenses also
apply to that respective component and supplements the terms described in this Agreement.
3.

PRE-RELEASE VERSIONS. If the Licensed Content is a pre-release (beta) version, in addition to the other
provisions in this agreement, then these terms also apply:
a. Pre-Release Licensed Content. This Licensed Content is a pre-release version. It may not contain the
same information and/or work the way a final version of the Licensed Content will. We may change it
for the final version. We also may not release a final version. Microsoft is under no obligation to
provide you with any further content, including the final release version of the Licensed Content.

b. Feedback. If you agree to give feedback about the Licensed Content to Microsoft, either directly or
through its third party designee, you give to Microsoft without charge, the right to use, share and
commercialize your feedback in any way and for any purpose. You also give to third parties, without
charge, any patent rights needed for their products, technologies and services to use or interface with
any specific parts of a Microsoft software, Microsoft product, or service that includes the feedback. You
will not give feedback that is subject to a license that requires Microsoft to license its software,
technologies, or products to third parties because we include your feedback in them. These rights

MCT USE ONLY. STUDENT USE PROHIBITED

survive this agreement.

c. Term. If you are an Authorized Training Center, MCT or MPN, you agree to cease using all copies of the
beta version of the Licensed Content upon (i) the date which Microsoft informs you is the end date for
using the beta version, or (ii) sixty (60) days after the commercial release of the Licensed Content,
whichever is earliest (beta term). Upon expiration or termination of the beta term, you will
irretrievably delete and destroy all copies of same in the possession or under your control.
4.

INTERNET-BASED SERVICES. Classroom Devices located at Authorized Learning Centers physical location
may contain virtual machines and virtual hard disks for use while attending an Authorized Training
Session. You may only use the software on the virtual machines and virtual hard disks on a Classroom
Device solely to perform the virtual lab activities included in the MOC Course while attending the
Authorized Training Session. Microsoft may provide Internet-based services with the software included
with the virtual machines and virtual hard disks. It may change or cancel them at any time. If the
software is pre-release versions of software, some of its Internet-based services may be turned on by
default. The default setting in these versions of the software do not necessarily reflect how the features
will be configured in the commercially released versions. If Internet-based services are included with the
software, they are typically simulated for demonstration purposes in the software and no transmission
over the Internet takes place. However, should the software be configured to transmit over the Internet,
the following terms apply:
a. Consent for Internet-Based Services. The software features described below connect to Microsoft or
service provider computer systems over the Internet. In some cases, you will not receive a separate
notice when they connect. You may switch off these features or not use them. By using these features,
you consent to the transmission of this information. Microsoft does not use the information to identify
or contact you.
b. Computer Information. The following features use Internet protocols, which send to the appropriate
systems computer information, such as your Internet protocol address, the type of operating system,
browser and name and version of the software you are using, and the language code of the device
where you installed the software. Microsoft uses this information to make the Internet-based services
available to you.

Accelerators. When you use click on or move your mouse over an Accelerator, the title and full web
address or URL of the current webpage, as well as standard computer information, and any content
you have selected, might be sent to the service provider. If you use an Accelerator provided by
Microsoft, the information sent is subject to the Microsoft Online Privacy Statement, which is
available at go.microsoft.com/fwlink/?linkid=31493. If you use an Accelerator provided by a third
party, use of the information sent will be subject to the third partys privacy practices.

Automatic Updates. This software contains an Automatic Update feature that is on by default. For
more information about this feature, including instructions for turning it off, see
go.microsoft.com/fwlink/?LinkId=178857. You may turn off this feature while the software is
running (opt out). Unless you expressly opt out of this feature, this feature will (a) connect to
Microsoft or service provider computer systems over the Internet, (b) use Internet protocols to send
to the appropriate systems standard computer information, such as your computers Internet
protocol address, the type of operating system, browser and name and version of the software you
are using, and the language code of the device where you installed the software, and (c)
automatically download and install, or prompt you to download and/or install, current Updates to
the software. In some cases, you will not receive a separate notice before this feature takes effect.

MCT USE ONLY. STUDENT USE PROHIBITED

By installing the software, you consent to the transmission of standard computer information and
the automatic downloading and installation of updates.

Auto Root Update. The Auto Root Update feature updates the list of trusted certificate authorities.
you can switch off the Auto Root Update feature.

Customer Experience Improvement Program (CEIP), Error and Usage Reporting; Error Reports. This
software uses CEIP and Error and Usage Reporting components enabled by default that
automatically send to Microsoft information about your hardware and how you use this software.
This software also automatically sends error reports to Microsoft that describe which software
components had errors and may also include memory dumps. You may choose not to use these
software components. For more information please go to
<http://go.microsoft.com/fwlink/?LinkID=196910>.

Digital Certificates. The software uses digital certificates. These digital certificates confirm the
identity of Internet users sending X.509 standard encrypted information. They also can be used to
digitally sign files and macros, to verify the integrity and origin of the file contents. The software
retrieves certificates and updates certificate revocation lists. These security features operate only
when you use the Internet.

Extension Manager. The Extension Manager can retrieve other software through the internet from
the Visual Studio Gallery website. To provide this other software, the Extension Manager sends to
Microsoft the name and version of the software you are using and language code of the device
where you installed the software. This other software is provided by third parties to Visual Studio
Gallery. It is licensed to users under terms provided by the third parties, not from Microsoft. Read
the Visual Studio Gallery terms of use for more information.

IPv6 Network Address Translation (NAT) Traversal service (Teredo). This feature helps existing
home Internet gateway devices transition to IPv6. IPv6 is a next generation Internet protocol. It
helps enable end-to-end connectivity often needed by peer-to-peer applications. To do so, each
time you start up the software the Teredo client service will attempt to locate a public Teredo
Internet service. It does so by sending a query over the Internet. This query only transfers standard
Domain Name Service information to determine if your computer is connected to the Internet and
can locate a public Teredo service. If you

use an application that needs IPv6 connectivity or


configure your firewall to always enable IPv6 connectivity

by default standard Internet Protocol information will be sent to the Teredo service at Microsoft at
regular intervals. No other information is sent to Microsoft. You can change this default to use nonMicrosoft servers. You can also switch off this feature using a command line utility named netsh.

Malicious Software Removal. During setup, if you select Get important updates for installation,
the software may check and remove certain malware from your device. Malware is malicious
software. If the software runs, it will remove the Malware listed and updated at
www.support.microsoft.com/?kbid=890830. During a Malware check, a report will be sent to
Microsoft with specific information about Malware detected, errors, and other information about
your device. This information is used to improve the software and other Microsoft products and
services. No information included in these reports will be used to identify or contact you. You may
disable the softwares reporting functionality by following the instructions found at

MCT USE ONLY. STUDENT USE PROHIBITED

www.support.microsoft.com/?kbid=890830. For more information, read the Windows Malicious


Software Removal Tool privacy statement at go.microsoft.com/fwlink/?LinkId=113995.

Microsoft Digital Rights Management. If you use the software to access content that has been
protected with Microsoft Digital Rights Management (DRM), then, in order to let you play the
content, the software may automatically request media usage rights from a rights server on the
Internet and download and install available DRM updates. For more information, see
go.microsoft.com/fwlink/?LinkId=178857.

Microsoft Telemetry Reporting Participation. If you choose to participate in Microsoft Telemetry


Reporting through a basic or advanced membership, information regarding filtered URLs,
malware and other attacks on your network is sent to Microsoft. This information helps Microsoft
improve the ability of Forefront Threat Management Gateway to identify attack patterns and
mitigate threats. In some cases, personal information may be inadvertently sent, but Microsoft will
not use the information to identify or contact you. You can switch off Telemetry Reporting. For
more information on this feature, see http://go.microsoft.com/fwlink/?LinkId=130980.

Microsoft Update Feature. To help keep the software up-to-date, from time to time, the software
connects to Microsoft or service provider computer systems over the Internet. In some cases, you
will not receive a separate notice when they connect. When the software does so, we check your
version of the software and recommend or download updates to your devices. You may not receive
notice when we download the update. You may switch off this feature.

Network Awareness. This feature determines whether a system is connected to a network by either
passive monitoring of network traffic or active DNS or HTTP queries. The query only transfers
standard TCP/IP or DNS information for routing purposes. You can switch off the active query
feature through a registry setting.

Plug and Play and Plug and Play Extensions. You may connect new hardware to your device, either
directly or over a network. Your device may not have the drivers needed to communicate with that
hardware. If so, the update feature of the software can obtain the correct driver from Microsoft and
install it on your device. An administrator can disable this update feature.

Real Simple Syndication (RSS) Feed. This software start page contains updated content that is
supplied by means of an RSS feed online from Microsoft.

Search Suggestions Service. When you type a search query in Internet Explorer by using the Instant
Search box or by typing a question mark (?) before your search term in the Address bar, you will see
search suggestions as you type (if supported by your search provider). Everything you type in the
Instant Search box or in the Address bar when preceded by a question mark (?) is sent to your
search provider as you type it. In addition, when you press Enter or click the Search button, all the
text that is in the search box or Address bar is sent to the search provider. If you use a Microsoft
search provider, the information you send is subject to the Microsoft Online Privacy Statement,
which is available at go.microsoft.com/fwlink/?linkid=31493. If you use a third-party search
provider, use of the information sent will be subject to the third partys privacy practices. You can
turn search suggestions off at any time in Internet Explorer by using Manage Add-ons under the
Tools button. For more information about the search suggestions service, see
go.microsoft.com/fwlink/?linkid=128106.

SQL Server Reporting Services Map Report Item. The software may include features that retrieve
content such as maps, images and other data through the Bing Maps (or successor branded)

MCT USE ONLY. STUDENT USE PROHIBITED

application programming interface (the Bing Maps APIs). The purpose of these features is to
create reports displaying data on top of maps, aerial and hybrid imagery. If these features are
included, you may use them to create and view dynamic or static documents. This may be done only
in conjunction with and through methods and means of access integrated in the software. You may
not otherwise copy, store, archive, or create a database of the content available through the Bing
Maps APIs. you may not use the following for any purpose even if they are available through the
Bing Maps APIs:

Bing Maps APIs to provide sensor based guidance/routing, or

Any Road Traffic Data or Birds Eye Imagery (or associated metadata).

Your use of the Bing Maps APIs and associated content is also subject to the additional terms and
conditions at http://www.microsoft.com/maps/product/terms.html.

URL Filtering. The URL Filtering feature identifies certain types of web sites based upon predefined
URL categories, and allows you to deny access to such web sites, such as known malicious sites and
sites displaying inappropriate or pornographic materials. To apply URL filtering, Microsoft queries
the online Microsoft Reputation Service for URL categorization. You can switch off URL filtering. For
more information on this feature, see http://go.microsoft.com/fwlink/?LinkId=130980

Web Content Features. Features in the software can retrieve related content from Microsoft and
provide it to you. To provide the content, these features send to Microsoft the type of operating
system, name and version of the software you are using, type of browser and language code of the
device where you run the software. Examples of these features are clip art, templates, online
training, online assistance and Appshelp. You may choose not to use these web content features.

Windows Media Digital Rights Management. Content owners use Windows Media digital rights
management technology (WMDRM) to protect their intellectual property, including copyrights. This
software and third party software use WMDRM to play and copy WMDRM-protected content. If the
software fails to protect the content, content owners may ask Microsoft to revoke the softwares
ability to use WMDRM to play or copy protected content. Revocation does not affect other content.
When you download licenses for protected content, you agree that Microsoft may include a
revocation list with the licenses. Content owners may require you to upgrade WMDRM to access
their content. Microsoft software that includes WMDRM will ask for your consent prior to the
upgrade. If you decline an upgrade, you will not be able to access content that requires the upgrade.
You may switch off WMDRM features that access the Internet. When these features are off, you can
still play content for which you have a valid license.

Windows Media Player. When you use Windows Media Player, it checks with Microsoft for

compatible online music services in your region;


new versions of the player; and
codecs if your device does not have the correct ones for playing content.

You can switch off this last feature. For more information, go to
www.microsoft.com/windows/windowsmedia/player/11/privacy.aspx.

Windows Rights Management Services. The software contains a feature that allows you to create
content that cannot be printed, copied or sent to others without your permission. For more
information, go to www.microsoft.com/rms. you may choose not to use this feature

MCT USE ONLY. STUDENT USE PROHIBITED

Windows Time Service. This service synchronizes with time.windows.com once a week to provide
your computer with the correct time. You can turn this feature off or choose your preferred time
source within the Date and Time Control Panel applet. The connection uses standard NTP protocol.

Windows Update Feature. You may connect new hardware to the device where you run the
software. Your device may not have the drivers needed to communicate with that hardware. If so,
the update feature of the software can obtain the correct driver from Microsoft and run it on your
device. You can switch off this update feature.

c. Use of Information. Microsoft may use the device information, error reports, and malware reports to
improve our software and services. We may also share it with others, such as hardware and software
vendors. They may use the information to improve how their products run with Microsoft software.

d. Misuse of Internet-based Services. You may not use any Internet-based service in any way that could
harm it or impair anyone elses use of it. You may not use the service to try to gain unauthorized access
to any service, data, account or network by any means.
5.

SCOPE OF LICENSE. The Licensed Content is licensed, not sold. This agreement only gives you some rights
to use the Licensed Content. Microsoft reserves all other rights. Unless applicable law gives you more
rights despite this limitation, you may use the Licensed Content only as expressly permitted in this
agreement. In doing so, you must comply with any technical limitations in the Licensed Content that only
allows you to use it in certain ways. Except as expressly permitted in this agreement, you may not:

install more copies of the Licensed Content on devices than the number of licenses you acquired;

allow more individuals to access the Licensed Content than the number of licenses you acquired;

publicly display, or make the Licensed Content available for others to access or use;

install, sell, publish, transmit, encumber, pledge, lend, copy, adapt, link to, post, rent, lease or lend,
make available or distribute the Licensed Content to any third party, except as expressly permitted
by this Agreement.

reverse engineer, decompile, remove or otherwise thwart any protections or disassemble the
Licensed Content except and only to the extent that applicable law expressly permits, despite this
limitation;

access or use any Licensed Content for which you are not providing a training session to End Users
using the Licensed Content;

access or use any Licensed Content that you have not been authorized by Microsoft to access and
use; or

transfer the Licensed Content, in whole or in part, or assign this agreement to any third party.

6.

RESERVATION OF RIGHTS AND OWNERSHIP. Microsoft reserves all rights not expressly granted to you in
this agreement. The Licensed Content is protected by copyright and other intellectual property laws and
treaties. Microsoft or its suppliers own the title, copyright, and other intellectual property rights in the
Licensed Content. You may not remove or obscure any copyright, trademark or patent notices that
appear on the Licensed Content or any components thereof, as delivered to you.

7.

EXPORT RESTRICTIONS. The Licensed Content is subject to United States export laws and regulations. You
must comply with all domestic and international export laws and regulations that apply to the Licensed
Content. These laws include restrictions on destinations, End Users and end use. For additional
information, see www.microsoft.com/exporting.

MCT USE ONLY. STUDENT USE PROHIBITED

8.

LIMITATIONS ON SALE, RENTAL, ETC. AND CERTAIN ASSIGNMENTS. You may not sell, rent, lease, lend or
sublicense the Licensed Content or any portion thereof, or transfer or assign this agreement.

9.

SUPPORT SERVICES. Because the Licensed Content is as is, we may not provide support services for it.

10.

TERMINATION. Without prejudice to any other rights, Microsoft may terminate this agreement if you fail
to comply with the terms and conditions of this agreement. Upon any termination of this agreement, you
agree to immediately stop all use of and to irretrievable delete and destroy all copies of the Licensed
Content in your possession or under your control.

11.

LINKS TO THIRD PARTY SITES. You may link to third party sites through the use of the Licensed Content.
The third party sites are not under the control of Microsoft, and Microsoft is not responsible for the
contents of any third party sites, any links contained in third party sites, or any changes or updates to third
party sites. Microsoft is not responsible for webcasting or any other form of transmission received from
any third party sites. Microsoft is providing these links to third party sites to you only as a convenience,
and the inclusion of any link does not imply an endorsement by Microsoft of the third party site.

12.

ENTIRE AGREEMENT. This agreement, and the terms for supplements, updates and support services are
the entire agreement for the Licensed Content.

13.

APPLICABLE LAW.
a. United States. If you acquired the Licensed Content in the United States, Washington state law governs
the interpretation of this agreement and applies to claims for breach of it, regardless of conflict of laws
principles. The laws of the state where you live govern all other claims, including claims under state
consumer protection laws, unfair competition laws, and in tort.
b. Outside the United States. If you acquired the Licensed Content in any other country, the laws of that
country apply.

14.

LEGAL EFFECT. This agreement describes certain legal rights. You may have other rights under the laws of
your country. You may also have rights with respect to the party from whom you acquired the Licensed
Content. This agreement does not change your rights under the laws of your country if the laws of your
country do not permit it to do so.

15.

DISCLAIMER OF WARRANTY. THE LICENSED CONTENT IS LICENSED "AS-IS," "WITH ALL FAULTS," AND "AS
AVAILABLE." YOU BEAR THE RISK OF USING IT. MICROSOFT CORPORATION AND ITS RESPECTIVE
AFFILIATES GIVE NO EXPRESS WARRANTIES, GUARANTEES, OR CONDITIONS UNDER OR IN RELATION TO
THE LICENSED CONTENT. YOU MAY HAVE ADDITIONAL CONSUMER RIGHTS UNDER YOUR LOCAL LAWS
WHICH THIS AGREEMENT CANNOT CHANGE. TO THE EXTENT PERMITTED UNDER YOUR LOCAL LAWS,
MICROSOFT CORPORATION AND ITS RESPECTIVE AFFILIATES EXCLUDE ANY IMPLIED WARRANTIES OR
CONDITIONS, INCLUDING THOSE OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NON-INFRINGEMENT.

16.

LIMITATION ON AND EXCLUSION OF REMEDIES AND DAMAGES. TO THE EXTENT NOT PROHIBITED BY
LAW, YOU CAN RECOVER FROM MICROSOFT CORPORATION AND ITS SUPPLIERS ONLY DIRECT
DAMAGES UP TO USD$5.00. YOU AGREE NOT TO SEEK TO RECOVER ANY OTHER DAMAGES, INCLUDING
CONSEQUENTIAL, LOST PROFITS, SPECIAL, INDIRECT OR INCIDENTAL DAMAGES FROM MICROSOFT
CORPORATION AND ITS RESPECTIVE SUPPLIERS.

MCT USE ONLY. STUDENT USE PROHIBITED

This limitation applies to


o
anything related to the Licensed Content, services made available through the Licensed Content, or
content (including code) on third party Internet sites or third-party programs; and
o
claims for breach of contract, breach of warranty, guarantee or condition, strict liability, negligence,
or other tort to the extent permitted by applicable law.
It also applies even if Microsoft knew or should have known about the possibility of the damages. The
above limitation or exclusion may not apply to you because your country may not allow the exclusion or
limitation of incidental, consequential or other damages.

Please note: As this Licensed Content is distributed in Quebec, Canada, some of the clauses in this agreement
are provided below in French.
Remarque : Ce le contenu sous licence tant distribu au Qubec, Canada, certaines des clauses dans ce
contrat sont fournies ci-dessous en franais.

EXONRATION DE GARANTIE. Le contenu sous licence vis par une licence est offert tel quel . Toute
utilisation de ce contenu sous licence est votre seule risque et pril. Microsoft naccorde aucune autre garantie
expresse. Vous pouvez bnficier de droits additionnels en vertu du droit local sur la protection dues
consommateurs, que ce contrat ne peut modifier. La ou elles sont permises par le droit locale, les garanties
implicites de qualit marchande, dadquation un usage particulier et dabsence de contrefaon sont exclues.
LIMITATION DES DOMMAGES-INTRTS ET EXCLUSION DE RESPONSABILIT POUR LES DOMMAGES. Vous
pouvez obtenir de Microsoft et de ses fournisseurs une indemnisation en cas de dommages directs uniquement
hauteur de 5,00 $ US. Vous ne pouvez prtendre aucune indemnisation pour les autres dommages, y
compris les dommages spciaux, indirects ou accessoires et pertes de bnfices.
Cette limitation concerne:
tout ce qui est reli au le contenu sous licence , aux services ou au contenu (y compris le code)
figurant sur des sites Internet tiers ou dans des programmes tiers ; et
les rclamations au titre de violation de contrat ou de garantie, ou au titre de responsabilit
stricte, de ngligence ou dune autre faute dans la limite autorise par la loi en vigueur.

Elle sapplique galement, mme si Microsoft connaissait ou devrait connatre lventualit dun tel dommage.
Si votre pays nautorise pas lexclusion ou la limitation de responsabilit pour les dommages indirects,
accessoires ou de quelque nature que ce soit, il se peut que la limitation ou lexclusion ci-dessus ne sappliquera
pas votre gard.

EFFET JURIDIQUE. Le prsent contrat dcrit certains droits juridiques. Vous pourriez avoir dautres droits prvus
par les lois de votre pays. Le prsent contrat ne modifie pas les droits que vous confrent les lois de votre pays
si celles-ci ne le permettent pas.
Revised March 2012

Developing Microsoft SQL Server 2012 Databases

MCT USE ONLY. STUDENT USE PROHIBITED

xiv

Acknowledgments

MCT USE ONLY. STUDENT USE PROHIBITED

Developing Microsoft SQL Server 2012 Databases

xv

Microsoft Learning would like to acknowledge and thank the following for their contribution towards
developing this title. Their effort at various stages in the development has ensured that you have a good
classroom experience.

Design and Development

This course was designed and developed by SolidQ. SolidQ is a global provider of consulting, mentoring
and training services for Microsoft Data Management, Business Intelligence and Collaboration platforms.

Greg Low Lead Developer

Dr Greg Low is a SQL Server MVP, an MCT, and a Microsoft Regional Director for Australia. Greg has
worked with SQL Server since version 4.2 as an active mentor, consultant and trainer. Greg describes
himself as a SQL Server junkie and also describes himself as having been involved in development since
dinosaurs roamed the Earth. He has been an instructor in the Microsoft SQL Server Masters certification
program for several years and was one of the first two people to achieve the SQL Server 2008 Master
certification. He is the author of a number whitepapers on the Microsoft MSDN and TechNet web sites
and the author of a number of SQL Server related books. Greg is based in Melbourne Australia.

Herbert Albert Course Developer

Herbert Albert started his career in 1994. He works as a trainer, consultant, and author focusing on SQL
Server technologies. Herbert is a mentor and the Central European CEO for SolidQ. He is based in Vienna,
Austria. He has several Microsoft certifications including MCT which he has held since 1997. Herbert is a
regular speaker at conferences and co-author of the SQL Server 2012 Upgrade Technical Reference Guide
and SQL Server 2005 Step-by-Step Applied Techniques. Together with Gianluca Hotz, Herbert writes a
regular column at the SolidQ Journal.

Chris Barker Technical Reviewer

Chris Barker is an MCT in New Zealand and currently employed as a staff trainer at Auldhouse, one of
New Zealands major CPLS training centers in Wellington. He has been programming from the early
1970shis first program was written in assembly language and debugged in binary (literally)! While
focusing training on programming (mostly .NET) and databases (mostly Microsoft SQL Server), Chris has
also been an infrastructure trainer and has both Novell and Microsoft networking qualifications.

Mark Hions Technical Reviewer

Mark's passion for computing and skill as a communicator were well suited to his position as an instructor
at Honeywell Canada, where he started working with minicomputers, mainframes, and mature students in
1984. He first met Microsoft SQL Server when it ran on OS/2, and has delivered training on every version
since. An independent MCT and consultant for many years, he is a highly-rated presenter at TechEd, has
designed SQL Server exams for Microsoft, and has delivered deep-dive courses through the Microsoft
Partner Channel. Mark is now the Principal SQL Server Instructor and Consultant at DesTech, which is the
largest provider of SQL Server training in the Toronto area.

Developing Microsoft SQL Server 2012 Databases

Contents
Module 1: Introduction to SQL Server 2012 and its Toolset
Lesson 1: Introduction to the SQL Server Platform
Lesson 2: Working with SQL Server Tools
Lesson 3: Configuring SQL Server Services
Lab 1: Introduction to SQL Server and its Toolset

1-3
1-14
1-25
1-33

Module 2: Working with Data Types


Lesson 1: Using Data Types
Lesson 2: Working with Character Data
Lesson 3: Converting Data Types
Lesson 4: Working with Specialized Data Types
Lab 2: Working with Data Types

2-3
2-16
2-25
2-33
2-39

Module 3: Designing and Implementing Tables


Lesson 1: Designing Tables
Lesson 2: Working with Schemas
Lesson 3: Creating and Altering Tables
Lab 3: Designing and Implementing Tables

3-3
3-14
3-19
3-29

Module 4: Ensuring Data Integrity through Constraints


Lesson 1: Enforcing Data Integrity
Lesson 2: Implementing Domain Integrity
Lesson 3: Implementing Entity and Referential Integrity
Lab 4: Ensuring Data Integrity through Constraints

4-3
4-9
4-17
4-31

Module 5: Planning for SQL Server 2012 Indexing


Lesson 1: Core Indexing Concepts
Lesson 2: Data Types and Indexes
Lesson 3: Single Column and Composite Indexes
Lab 5: Planning for SQL Server Indexing

5-3
5-11
5-19
5-24

Module 6: Implementing Table Structures in SQL Server 2012


Lesson 1: SQL Server Table Structures
Lesson 2: Working with Clustered Indexes
Lesson 3: Designing Effective Clustered Indexes
Lab 6: Implementing Table Structures in SQL Server

6-3
6-13
6-19
6-22

MCT USE ONLY. STUDENT USE PROHIBITED

xvi

Module 7: Reading SQL Server 2012 Execution Plans


Lesson 1: Execution Plan Core Concepts
Lesson 2: Common Execution Plan Elements
Lesson 3: Working with Execution Plans
Lab 7: Reading SQL Server Execution Plans

7-3
7-12
7-22
7-28

Module 8: Improving Performance through Nonclustered Indexes


Lesson 1: Designing Effective Nonclustered Indexes
Lesson 2: Implementing Nonclustered Indexes
Lesson 3: Tracing and Tuning Queries
Lab 8: Improving Performance through Nonclustered Indexes

8-3
8-10
8-18
8-24

Module 9: Designing and Implementing Views


Lesson 1: Introduction to Views
Lesson 2: Creating and Managing Views
Lesson 3: Performance Considerations for Views
Lab 9: Designing and Implementing Views

9-3
9-10
9-19
9-27

Module 10: Designing and Implementing Stored Procedures


Lesson 1: Introduction to Stored Procedures
Lesson 2: Working With Stored Procedures
Lesson 3: Implementing Parameterized Stored Procedures
Lesson 4: Controlling Execution Context
Lab 10: Designing and Implementing Stored Procedures

10-3
10-11
10-22
10-31
10-37

Module 11: Merging Data and Passing Tables


Lesson 1: Using the MERGE Statement
Lesson 2: Implementing TABLE Types
Lesson 3: Using TABLE Types As Parameters
Lab 11: Passing Tables and Merging Data

11-3
11-14
11-22
11-26

Module 12: Designing and Implementing User-Defined Functions


Lesson 1: Overview of Functions
Lesson 2: Designing and Implementing Scalar Functions
Lesson 3: Designing and Implementing Table-valued Functions
Lesson 4: Considerations for Implementing Functions
Lesson 5: Alternatives to Functions
Lab 12: Designing and Implementing User-defined Functions

12-3
12-7
12-13
12-18
12-25
12-28

MCT USE ONLY. STUDENT USE PROHIBITED

Developing Microsoft SQL Server 2012 Databases

xvii

Developing Microsoft SQL Server 2012 Databases

Module 13: Creating Highly Concurrent SQL Server 2012 Applications


Lesson 1: Introduction to Transactions
Lesson 2: Introduction to Locks
Lesson 3: Management of Locking
Lesson 4: Transaction Isolation Levels
Lab 13: Creating Highly Concurrent SQL Server Applications

13-3
13-17
13-28
13-38
13-44

Module 14: Handling Errors in T-SQL Code


Lesson 1: Understanding T-SQL Error Handling
Lesson 2: Implementing T-SQL Error Handling
Lesson 3: Implementing Structured Exception Handling
Lab 14: Handling Errors in T-SQL Code

14-3
14-13
14-24
14-32

Module 15: Responding to Data Manipulation via Triggers


Lesson 1: Designing DML Triggers
Lesson 2: Implementing DML Triggers
Lesson 3: Advanced Trigger Concepts
Lab 15: Responding to Data Manipulation via Triggers

15-3
15-12
15-19
15-29

Module 16 Implementing Managed Code in SQL Server 2012


Lesson 1: Introduction to SQL CLR Integration
Lesson 2: Importing and Cataloging Assemblies
Lesson 3: Implementing SQL CLR Integration
Lab 16: Implementing Managed Code in SQL Server

16-3
16-14
16-21
16-39

Module 17: Storing XML Data in SQL Server 2012


Lesson 1: Introduction to XML and XML Schemas
Lesson 2: Storing XML Data and Schemas in SQL Server
Lesson 3: Implementing XML Indexes
Lab 17: Storing XML Data in SQL Server

17-3
17-15
17-26
17-31

Module 18: Querying XML Data in SQL Server 2012


Lesson 1: Using the T-SQL FOR XML Statement
Lesson 2: Getting Started with XQuery
Lesson 3: Shredding XML
Lab 18: Querying XML Data in SQL Server

18-3
18-15
18-26
18-35

Module 19: Working with SQL Server 2012 Spatial Data


Lesson 1: Introduction to Spatial Data
Lesson 2: Working with SQL Server Spatial Data Types
Lesson 3: Using Spatial Data in Applications
Lab 19: Working with SQL Server Spatial Data

19-3
19-12
19-25
19-35

MCT USE ONLY. STUDENT USE PROHIBITED

xviii

Module 20 Working with Full-Text Indexes and Queries


Lesson 1: Introduction to Full-Text Indexing
Lesson 2: Implementing Full-Text Indexes in SQL Server
Lesson 3: Working with Full-Text Queries
Lab 20: Working with Full-Text Indexes and Queries

20-3
20-9
20-20
20-32

Appendix: Lab Answer Keys


Module 1 Lab: Introduction to SQL Server and its Toolset
Module 2 Lab: Working with Data Types
Module 3 Lab: Designing and Implementing Tables
Module 4 Lab: Ensuring Data Integrity through Constraints
Module 5 Lab: Planning for SQL Server Indexing
Module 6 Lab: Implementing Table Structures in SQL Server
Module 7 Lab: Reading SQL Server Execution Plans
Module 8 Lab: Improving Performance through Nonclustered Indexes
Module 9 Lab: Designing and Implementing Views
Module 10 Lab: Designing and Implementing Stored Procedures
Module 11 Lab: Passing Tables and Merging Data
Module 12 Lab: Designing and Implementing User-defined Functions
Module 13 Lab: Creating Highly Concurrent SQL Server Applications
Module 14 Lab: Handling Errors in T-SQL Code
Module 15 Lab: Responding to Data Manipulation via Triggers
Module 16 Lab: Implementing Managed Code in SQL Server
Module 17 Lab: Storing XML Data in SQL Server
Module 18 Lab: Querying XML Data in SQL Server
Module 19 Lab: Working with SQL Server Spatial Data
Module 20 Lab: Working with Full-Text Indexes and Queries

L1-1
L2-5
L3-9
L4-13
L5-16
L6-20
L7-24
L8-29
L9-34
L10-38
L11-42
L12-45
L13-50
L14-52
L15-55
L16-58
L17-62
L18-65
L19-68
L20-71

MCT USE ONLY. STUDENT USE PROHIBITED

Developing Microsoft SQL Server 2012 Databases

xix

MCT USE ONLY. STUDENT USE PROHIBITED

MCT USE ONLY. STUDENT USE PROHIBITED


16-1

Module 16
Implementing Managed Code in SQL Server 2012
Contents:
Lesson 1: Introduction to SQL CLR Integration

16-3

Lesson 2: Importing and Cataloging Assemblies

16-14

Lesson 3: Implementing SQL CLR Integration

16-21

Lab 16: Implementing Managed Code in SQL Server

16-39

Implementing Managed Code in SQL Server 2012

Module Overview

MCT USE ONLY. STUDENT USE PROHIBITED

16-2

As a database professional, you are asked to create databases and related objects to meet business
needs. Most of the requirements can be met by using Transact-SQL. However, there are times when the
requirements go beyond the abilities of Transact-SQL. These requirements may include functionality
such as:

Complex/compound data types like currency values that include culture information, complex
numbers, dates that include calendar system, or storing entire arrays of values in a single column.

Accessing image files on the operating system and reading them or copying them into the database.

These requirements are examples of requirements that can be met using Microsoft SQL Server CLR
Integration. Integrated code is used to create user-defined functions, stored procedures, aggregates,
types, and triggers. These objects can be developed using any .NET language and can be highly
specialized. In this module, you will learn about using CLR integrated code to create user- defined
database objects that are managed by the .NET Framework.

Objectives
After completing this module, you will be able to:

Explain the importance of SQL Server CLR Integration

Import and configure assemblies

Implement objects that have been created within .NET assemblies

Lesson 1

Introduction to SQL CLR Integration

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-3

Amongst database professionals, there is a constant desire to extend the built-in functionality of SQL
Server. For example, you might wish to add a new aggregate to the existing list of aggregates supplied by
SQL Server. There is no right or wrong method to extend the product. Particular methods are more or less
suited to particular needs and situations. SQL Server CLR Integration is one method for extending SQL
Server. It is important to understand SQL Server CLR integration and its appropriate use cases.

Objectives
After completing this lesson, you will be able to:

Explain the ways that SQL Server can be extended

Describe the .NET Framework

Describe the .NET Common Language Runtime environment

Explain the need for managed code in SQL Server

Explain the situations where T-SQL use is inappropriate

Choose appropriate use cases for managed code in SQL Server

Implementing Managed Code in SQL Server 2012

Options for Extending SQL Server

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

16-4

Many SQL Server components have mechanisms that allow their functionality to be extended. Previous
mechanisms for extending the Database Engine such as the use of extended stored procedures are limited
in both effectiveness and safety. Managed code is a safer alternative.

Managed Code
Managed code is code written to operate within the .NET Framework. There seems to be a concern
amongst database administrators about running managed code within the Database Engine but it is
important to realize that even the most unsafe managed code that you write is always safer than any
extended stored procedure code.

Many applications can be created with the out of the box tools and functionality provided with SQL
Server. The ability to reuse previously developed functionality though, helps to produce higher quality
outcomes. It is then desirable to package that reusable functionality as an extension of the SQL Server
product.

Many SQL Server components are extensible. As an example, SQL Server Reporting Services allows for the
creation of rendering extensions, security extensions, data processing extensions, delivery extensions,
custom code, and external assemblies.

Database Engine Extensibility

Traditionally, extending the Database Engine has been achieved by creating extended stored procedures.
These are specially-crafted procedures that are written in C++ and are complex to code. Of more concern
though is that when they are operating, they are executing directly within the process space of the SQL
Server engine. That is not a safe place to be executing code as minor errors could cause failure or
instability of the Database Engine itself. The ability to create extended stored procedures is now
deprecated.
Question: Are there any aspects of the Database Engine that you would like to extend?

Introduction to the .NET Framework

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-5

The .NET Framework is the foundation for developing Microsoft Windows applications and services
including Microsoft SQL Server. The .NET Framework offers the developer tools that make application and
service development easier and provides a good basis for code to extend SQL Server.

Win32 and Win64 APIs

The Windows operating system evolved over a period of many years. The programming interfaces to the
operating system are commonly referred to as the Win32 and Win64 Application Programming Interfaces
(APIs). These interfaces also evolved over the same period. In general, they are complex and inconsistent
in the way they are designed, largely due to their evolution over time rather than being designed with a
single set of guidelines at one time.

.NET Framework

The .NET framework is a layer of software that sits above the Win32 and Win64 APIs and abstracts away
the underlying complexity. This framework is written in a consistent fashion to a tightly-written set of
design guidelines. Many people describe it as appearing to have been written by one brain. It is not
specific to any one programming language and also contains many thousands of prebuilt and pretested
objects. These objects are collectively referred to as the .NET Framework Class Libraries.
These capabilities make the .NET Framework a good base for building code to extend SQL Server.

Implementing Managed Code in SQL Server 2012

.NET Common Language Runtime

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

16-6

The .NET Common Language Runtime (CLR) is the layer in the .NET Framework that allows you to create
programs and procedures in any .NET language and deploy it for use. The resulting resources are referred
to as managed code. It is important to distinguish the CLR from the Common Language Specification
or CLS.

.NET Common Language Runtime

The CLR Integration feature within Microsoft SQL Server allows you to use .NET assemblies to customize
your SQL databases.
The .NET CLR offers:

Access to existing managed code.

Security features to ensure managed code will not compromise the server.

The ability to create new resources using .NET languages like Microsoft Visual C# and Microsoft
Visual Basic .NET.

Memory Management

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-7

A key problem that arose in development directly against the Win32 and Win64 APIs related to memory
management. In older Component Object Model (COM) programming that was used with these APIs,
releasing memory when it was no longer needed was based on reference counting. The idea was that the
following sequence of events would occur:

Object A creates Object B.

When Object B is created, it notes that it has one reference to itself.

Object C might then acquire a reference to Object B as well. Object B then notes that it has two
references to itself.

Object C releases its reference. Object B then notes that it has only a single reference to itself.

Object A then releases its reference as well. Object B then notes that it now has no references to itself
so it proceeds to destroy itself.

The problem with this scheme is that it is easy to create situations where memory is lost. For a simple
example, consider circular references. If two objects have references to each other but no other object has
any reference to either of them, they can both sit in memory forever as long as they have a reference to
each other. This then causes a leak (or loss) of the memory consumed by those objects. Over time,
creation of such situations could cause the loss of all available memory on the system.
This sort of memory management scheme would not be suitable within the Database Engine. The .NET
Framework includes a sophisticated memory management system known as Garbage Collection that is
designed to avoid any chance of such memory leaks. Instead of objects needing to count references, the
CLR periodically checks which objects are reachable and disposes of the other objects.

Type Safety

Another common problem with Win32 and Win64 code relates to what is known as type safety. When a
function or procedure is called, all that is known to the caller is the address in memory of the function.
The caller assembles a list of any required parameters, places them in an area called the stack, and jumps
to the memory address of the function. Problems arise when the design of the function and/or its
parameters change and the calling code is not updated. The function can then end up referring to
memory locations that do not exist.
The .NET CLR is designed to avoid such problems. As an example, as well as providing details of the
address of a function, it provides details of what is called the signature of a function. This specifies the
data types of each of the parameters and the order that they need to be in. The CLR will not allow a
function to be called with the wrong number or types of parameters. This is referred to as type safety.

Hosting the CLR

The CLR is also designed to be hostable. This means that it can itself be operated within other
programming environments. With SQL Server CLR integration, SQL Server becomes the host for the CLR.
From the point of view of the CLR, it is as though the CLR thinks that SQL Server is the operating system.
This allows SQL Server to exert great control over how the CLR operates, in terms of performance, security,
and stability.

CLS

The CLS is the Common Language Specification. It specifies the rules that languages must conform to, so
that interoperability between languages is possible. For example, even though it is possible to create a
method in C# called SayHello and another method called Sayhello, these methods could not be called
from another language that was not case sensitive. The CLS states that you should not create these two
methods, to avoid interoperability problems.

Implementing Managed Code in SQL Server 2012

Why Use Managed Code in SQL Server?

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

16-8

Managed code allows SQL Server to access thousands of available .NET libraries and assemblies created by
third parties as well as those you develop. A rich development environment (Microsoft Visual Studio) is
provided for building managed code. Many objects can be created in either T-SQL or managed code but
managed code also allows for the creation of new types of objects that cannot be created in T-SQL.

Why Use Managed Code in SQL Server?

In the last topic, the critical nature of type safety was discussed. For efficient development though, you
also need to achieve a high degree of code reuse. The .NET Framework offers a large set of libraries each
of which contains a large set of prewritten (and pretested) objects (typically referred to as classes) that can
easily be used directly in SQL Server via SQL Server CLR Integration. For example, the Regular Expression
(RegEx) library in the .NET Framework is a very powerful string manipulation library and can be utilized
within SQL Server by the use of SQL Server CLR Integration.
The inclusion of managed code in SQL Server also allows for much easier access to external resources and
in some cases provides higher performance.
While advances in error handling in T-SQL have been made in recent years, the error handling provided
by the T-SQL language still is well short of the type of error handling typically provided in higher-level
languages. Writing managed code allows advantage to be taken of these more extensive error handling
capabilities.

Alternative to T-SQL Objects


Many objects that can be created in T-SQL can also be created in managed code. This includes the
following set of objects:

Scalar user-defined functions

Table-valued user-defined functions

Stored procedures

DML triggers

DDL triggers

New Object Types


In managed code, you can also construct types of objects that cannot be constructed in T-SQL. These
include the following set of objects:

User-defined data types

User-defined aggregates

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-9

Implementing Managed Code in SQL Server 2012

T-SQL vs. Managed Code

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

16-10

There is no right or wrong answer for all situations when considering if an object should be created in TSQL or in managed code.

T-SQL

T-SQL is the primary method for manipulating data within databases. It is designed for direct data access
and offers high performance, particularly when working against very large sets of data. T-SQL is not,
though, a fully-fledged high-level programming language.

T-SQL has no object-oriented capabilities. For example, you cannot create a stored procedure that takes a
parameter of data type animal and pass a parameter of data type cat to it. T-SQL is not designed for tasks
such as intensive calculations or string handling and its objects are designed in a single flat namespace.
Almost all SQL Server system objects reside in a single sys schema. You cannot create a T-SQL schema
within another T-SQL schema. Managed code provides the ability to create a hierarchy of namespaces. TSQL does offer a useful set of built-in functions.

Managed Code

Managed code provides full object-oriented capabilities. It is important to realize though that this only
applies within the managed code itself. T-SQL code does not support the object-oriented capabilities.
Managed code works well in situations requiring intensive calculations (such as encryption) or string
handling.

General Rules
Two good general rules apply when making a choice between T-SQL and Managed Code:

The more data-oriented the need is, the more likely it is that T-SQL will be the better answer.

The more calculation, string-oriented, or external-access-oriented the need is, the more likely it is that
managed code will be the better answer.

Appropriate Database Object Use in Managed Code

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-11

In the last topic, you saw some general rules for choosing between T-SQL and managed code. You need
to consider how these rules would map to database objects in SQL Server. It is important to again
mention though that there is no right or wrong answer in all situations.

Scalar User-defined Functions (UDFs)


Scalar UDFs written in T-SQL are well-known for causing performance problems in SQL Server
environments. Managed code is often a good option for implementing scalar UDFs as long as the
function does not depend on data access.

Table-valued UDFs

The more data-related these functions are, the more they are likely to be best implemented in T-SQL. A
common use case for managed code in table-valued UDFs is for functions that need to access external
resources such as the filesystem, environment variables, registry, etc.

Stored Procedures

Stored procedures have traditionally been written in T-SQL. Most stored procedures should continue to
be written in T-SQL. There are very few good use cases for managed code in stored procedures. The
exceptions to this are stored procedures that need to access external resources or perform complex
calculations. There should be consideration, however, about whether code that performs these tasks
should be implemented within SQL Server at all.

DML Triggers

Almost all DML triggers are heavily-oriented towards data access and are written in T-SQL. There are very
few valid use cases for implementing DML triggers in managed code.

Implementing Managed Code in SQL Server 2012

DDL Triggers

MCT USE ONLY. STUDENT USE PROHIBITED

16-12

DDL triggers are again often data-oriented. Some DDL triggers though need to do extensive XML
processing, particularly based on the XML EVENTDATA structure passed to these triggers by SQL Server.
The more that extensive XML processing is required, the more likely the DDL trigger would be best
implemented in managed code. Managed code would also be a better option if the DDL trigger needed
to access external resources but this is rarely a good idea within any form of trigger.

User-defined Aggregates

T-SQL offers no concept of user-defined aggregates. These need to be implemented in managed code.

User-defined Data Types

T-SQL offers the ability to create alias data types but these are not really new data types. They are more
like subsets (or subclasses) of existing built-in data types. Managed code offers the ability to create
entirely new data types and determine not only what data needs to be stored but also the behavior of the
data type.

Demonstration 1A: Choosing Appropriate Use Cases for Managed Code


and T-SQL

Demonstration Steps

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-13

1.

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click SQL
Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ.ssmssln and click Open.

3.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file from
within Solution Explorer.

4.

Open the 11 Demonstration 1A Example 1.txt file.

5.

Open the 12 Demonstration 1A Example 2.txt file.

6.

Open the 13 Demonstration 1A Example 3.txt file.

7.

Open the 14 Demonstration 1A Example 4.txt file.

Question: Can you suggest ways that you could use T-SQL code that would be better as managed
code?

Question: Can you suggest ways that you could use managed code that would be better as T-SQL?

Implementing Managed Code in SQL Server 2012

Lesson 2

Importing and Cataloging Assemblies

MCT USE ONLY. STUDENT USE PROHIBITED

16-14

Assemblies are the unit of both deployment and security in the .NET Framework. Managed code in SQL
Server resides within assemblies. Before you can start to work with managed code in SQL Server, you need
to learn about assemblies and how they can be imported into SQL Server and secured.

Objectives
After completing this lesson, you will be able to:

Explain what an assembly is

Detail the permission sets that are available for securing assemblies

Import an assembly

What Is an Assembly?

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-15

Assemblies are the unit of both deployment and security in the .NET Framework. They contain the code
that will be executed, are self-describing, and may contain resources.

Structure of an Assembly
Prior to managed code, executable files (.exe files) and dynamic link libraries (.dll files) contained only
executable code. Executable code is produced by compilers by converting instructions in higher-level
languages into the binary codes required for execution by the computers processor.

Managed code assemblies have a specific structure. As well as executable code, they contain a manifest.
This manifest provides a list of contents of the assembly and of the programming interfaces provided by
the assembly. This allows other code to interrogate an assembly to determine both what it contains and
what it can do. As an example, SQL Server is able to gain a great deal of understanding of an assembly by
reading this manifest when cataloging an assembly.
Assemblies can contain other resources such as icons. These are also listed in the manifest.

Assemblies can be structured as either .exe files or as .dll files. The only difference between the two is that
.exe files also include an area called the portable execution header (PE header) that is used by the
operating system to find out where the executing code of an .exe file starts. SQL Server will only import
.dll files and will refuse to import .exe files.

Deployment and Security

Assemblies are the unit of managed code that is deployed and versioned. As they are created external to
SQL Server, it is possible to share assemblies between SQL Server and business applications.
Assemblies also form a boundary at which security is applied. In the next topic, you will see how this
security is configured.

Implementing Managed Code in SQL Server 2012

Assembly Permission Sets

Key Points
Using the CLR offers several levels of trust that can be set within policies for the machine and host on
which the assembly runs. There are three SQL Server permission sets that allow the administrator to
control the servers exposure to security and integrity risks: SAFE, EXTERNAL_ACCESS, and UNSAFE.

MCT USE ONLY. STUDENT USE PROHIBITED

16-16

Regardless of what the code in an assembly attempts to do, the permitted actions are determined by the
permission set on the assembly that contains the code.

SAFE

SAFE should be regarded by administrators as really meaning what the name says. It is a particularly
limited permission set but does allow access to the SQL Server database that it is cataloged in via a special
type of connection known as a context connection. Administrators should be comfortable with the
cataloging of SAFE assemblies. SAFE is the default permission set.

EXTERNAL_ACCESS

EXTERNAL_ACCESS is the permission set required before code in an assembly can access local and
network resources, environment variables, and the registry of the server. This permission set is still quite
safe and is typically used when any form of external access is required. Administrators should be fairly
comfortable with the cataloging of EXTERNAL_ACCESS assemblies, once a justification for the external
access requirements is made.

UNSAFE

UNSAFE is the unrestricted permission set. It should be rarely used for general development. UNSAFE is
required for code that calls external unmanaged code or code that holds state across function calls, etc.
Administrators should only allow the cataloging of UNSAFE assemblies in situations that have been very
carefully considered and justified.

Setup for EXTERNAL_ACCESS and UNSAFE

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-17

The EXTERNAL_ACCESS and UNSAFE permission sets require further configuration before they can be
used. A level of trust needs to be established. There are two ways to do this:

The database can be flagged as TRUSTWORTHY (via the ALTER DATABASE SET TRUSTWORTHY ON
statement). In general, this is not recommended without an understanding of what changes this
makes to the database security environment.

An asymmetric key is created from the assembly file cataloged in the master database, then a login
mapping to that key is created, and finally the login is granted the EXTERNAL ACCESS ASSEMBLY
permission on the assembly. While this is the recommended method of granting permission to use
EXTERNAL_ACCESS or UNSAFE permission sets, setting this up is an advanced topic beyond the scope
of this course.
Question: Which permission set should be rarely allowed?

Implementing Managed Code in SQL Server 2012

Importing an Assembly

Key Points
Before the code in an assembly can be used within SQL Server, the assembly must be imported and
cataloged within a database.

CREATE ASSEMBLY

MCT USE ONLY. STUDENT USE PROHIBITED

16-18

The CREATE ASSEMBLY statement is used to both import and catalog an assembly within the current
database. SQL Server assigns the assembly a permission set based on the WITH PERMISSON_SET clause in
the CREATE ASSEMBLY statement. If no permission set is explicitly requested, the assembly will be
cataloged as a SAFE assembly and the code within the assembly will only be able to execute tasks
permitted by the SAFE permission set.
Before any code in a user-created assembly can be executed, the clr enabled option must be set to 1
(enabled) at the instance level. An assembly and the objects within it can still be cataloged even if this
option is disabled. It only prevents code execution.

Once the assembly is cataloged in the database, the contents of the assembly are contained within the
database and the file that it was cataloged from is no longer needed by SQL Server. After it is cataloged,
an assembly will be loaded from within the database when required, not from the filesystem.

Assembly Path
There are three locations that an assembly can be imported from:

A .dll file on a local drive. The drive may not be a mapped drive.

A .dll file from a UNC path. (A UNC path is of the form \\SERVER\Share\PathToFile\File.dll.)

A binary string containing the contents of the .dll file.

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-19

It might at first seem odd to consider cataloging an assembly from a binary string but this is how
Visual Studio catalogs assemblies if you deploy an assembly directly from Visual Studio. Visual Studio
cannot make the assumption that you have access to the filesystem of the server. You might be working
with an instance of SQL Server or a database that is hosted by a hosting company and have no access to
the filesystem of the server at all.
Cataloging an assembly from a binary string allows you to stream an assembly to the server within the
CREATE ASSEMBLY statement. It is worth noting that if you later generate a script for the database, any
contained assemblies will also be scripted as binary strings.

Implementing Managed Code in SQL Server 2012

Demonstration 2A: Importing and Cataloging an Assembly

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

16-20

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

2.

Open the 21 Demonstration 2A.sql script file.

3.

Follow the instructions contained within the comments of the script file.

Question: Of the three trust levels, the UNSAFE level is the least protected. What situations can you
think of that would warrant the risk of using this trust level?
Question: Is it wise to set a company policy to never use this level? (Why or why not?)

Lesson 3

Implementing SQL CLR Integration

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-21

Once an assembly has been cataloged, there is a need to also catalog any objects within it. This will make
the objects visible within SQL Server so that they can be called from within T-SQL code.

Objectives
After completing this lesson, you will be able to:

Explain how appropriate attribute usage is important when creating assemblies

Implement scalar user-defined functions that have been written in managed code

Implement table-valued user-defined functions that have been written in managed code

Implement stored procedures that have been written in managed code

Implement stored procedures that have been written in managed code and that require access to
external resources

Implement triggers that have been written in managed code

Implement user-defined aggregates that have been written in managed code

Implement user-defined data types that have been written in managed code

Implementing Managed Code in SQL Server 2012

Attribute Usage

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

16-22

Attributes are metadata that is included within code and is used to describe that code. When
implementing managed code within SQL Server, attributes are used for deployment, performance, and
correctness reasons.

Attributes

If you have not written any managed code, the concept of attributes may be unfamiliar to you. Attributes
are metadata (or data about data) used to describe functions, methods, and classes. Attributes do not
form a part of the logic of the objects but describe aspects of them.
As an example, consider an attribute that records the name of the author of a method. This does not
change how the method operates but could be useful information for anyone using the method. The
.NET Framework also has a special set of logic called Reflection that allows one set of managed code to
interrogate details of another set of managed code. Attributes are returned as part of this process.
SQL Server accesses the attributes that you associate with your code via reflection.

Deployment

The first reason that attributes are helpful is for deployment. Adding a SqlFunction attribute to a managed
code method tells Visual Studio (or other code used for deployment) that the method should be
cataloged as a function within SQL Server. Adding an attribute to a method is also referred to as
adorning the method with the attribute.
If you do not add a SqlFunction attribute to a method, you can still manually catalog the method as a
function in SQL Server. The limitation is that automated deployment systems will not know to do so.

You might wonder why SQL Server doesnt just automatically catalog all methods as functions when
cataloging an assembly. Methods can be used for more than just functions and some methods are only
used within the assembly and are not intended to be used by code that utilizes the functionality provided
by the assembly.

Performance

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-23

The second reason why attributes are helpful relates to performance. Consider the DataAccess property of
the SqlFunction attribute as shown in the slide. This property tells SQL Server that no data context needs
to be provided to this method. It does not access data from the database. This makes the function quicker
to execute and reduces its memory requirements.
As another example of how an attribute can help with performance, consider an attribute that tells SQL
Server that a method call always returns NULL if the parameter passed to the method is NULL. In that
case, SQL Server knows it doesnt need to call the method at all if the value is NULL.

Correctness

The final reason for using attributes is for correctness. If a new Circle data type is created, it might provide
a method called Shrink. SQL Server needs to know that if this method is called, the internal state of the
user-defined data type will be changed when the method returns. This helps SQL Server know how the
method can be used. For example, SQL Server would then know that the method could be called in the
SET clause of an UPDATE statement. It would also prevent SQL Server from allowing the method to be
called in a SELECT list or WHERE clause in a SELECT statement.

Implementing Managed Code in SQL Server 2012

Scalar User-defined Functions

Key Points
Scalar user-defined functions are a common use case for managed code and often offer a higherperforming alternative to their equivalent T-SQL functions.

CREATE FUNCTION

MCT USE ONLY. STUDENT USE PROHIBITED

16-24

The CREATE FUNCTION statement is used to catalog a scalar user-defined function that has been written
in managed code. In the statement, you need to provide the details of the returned data type and a path
to the method within the assembly. Note that the name that a function is called within SQL Server does
not have to match the name that the method is called within the assembly. However, it is considered
good practice to have these matched with each other to avoid confusion.
The auto-deployment attribute related to this is the SqlFunction attribute.

EXTERNAL NAME

When cataloging the function, the EXTERNAL NAME clause is used to point to where the method exists
within the assembly. This normally consists of a three part name:

The first part of the name refers to the alias for the assembly that was used in the CREATE ASSEMBLY
statement.

The second part of the name must contain the namespace that contains the method. In the example
shown, the name UserDefinedFunctions is a class. However, the UserDefinedFunctions class itself
could be contained within another namespace. If that other namespace was called
CompanyFunctions, the second part of the name would need to be specified as
[CompanyFunctions.UserDefinedFunctions].

The third part of the name refers to the method within the class.

Note that even if the code has been built in a case-insensitive language like Visual Basic and the database
collation is set to case insensitive, the assembly name provided in the EXTERNAL NAME clause is case
sensitive.

Table-valued User-defined Functions

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-25

Table-valued functions (TVFs) are cataloged in a similar way to scalar functions but need to include the
definition of the returned table.

CREATE FUNCTION

The CREATE FUNCTION statement is also used to catalog TVFs written in managed code. The return data
type, however, must be TABLE. After the data type, you need to provide the definition of the schema of
the table. In the example shown in the slide, the table consists of two columns, both of integer data type.

Deployment Attribute

The definition of TVFs provides an example of why the properties of an attribute are useful. First, the
SqlFunction attribute indicates that the method should be cataloged as a function. The properties of the
attribute indicate:

That the function does not require access to database data.

The name of the FillRow method. (Do not be concerned with the FillRowMethodName method at this
point. While it must be present, it relates to the internal design of the function.)

The schema for the returned table. An automated deployment system (such as the one provided in
Visual Studio) needs to know the format of the returned table to be able to automatically catalog this
function in SQL Server.

Implementing Managed Code in SQL Server 2012

Parameter Naming

MCT USE ONLY. STUDENT USE PROHIBITED

16-26

The names chosen for the parameter in T-SQL do not need to match the names used in the managed
code. For example, the function in the slide example could have been cataloged in the following way:
CREATE FUNCTION dbo.RangeOfIntegers
(@StartValue int, @EndValue int)
RETURNS TABLE (PositionInList int, IntegerValue int)
AS EXTERNAL NAME
SQLCLR10776A.UserDefinedFunctions.RangeOfIntegers;

However, you should create T-SQL parameters with the same name as the parameters in the managed
code unless there is a compelling reason to make them different. An example of this would be a
parameter name that was used in managed code that was not a valid parameter name in T-SQL. Even in
this situation, a better option would be to change the parameter names in the managed code wherever
possible.
Question: How could a TVF be used with environment variables?

Demonstration 3A: Creating User-defined Functions

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-27

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

Open the 21 Demonstration 2A.sql script file and execute steps 1 to 3.

2.

Open the 31 Demonstration 3A.sql script file.

3.

Follow the instructions contained within the comments of the script file.

Implementing Managed Code in SQL Server 2012

Stored Procedures T-SQL Replacement

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

16-28

Stored procedures can also be written in managed code. Most stored procedures written at present tend
to be very data-access oriented. As such, replacing a T-SQL stored procedure with a managed code stored
procedure is unlikely to be useful.

CREATE PROCEDURE

The CREATE PROCEDURE statement is used to catalog a stored procedure written in managed code. The
relevant deployment attribute is the SqlProcedure attribute. This attribute tells Visual Studio (or other
deployment utility) that the method should be cataloged as a stored procedure.
Parameters that need to be passed to the stored procedure should be listed in the same way they are
listed for a T-SQL stored procedure definition.
The EXTERNAL NAME clause works identically to cataloging scalar user-defined functions.

SqlPipe

Stored procedures written in managed code support both input and output parameters as per their T-SQL
equivalent procedures.
Like stored procedures written in T-SQL, stored procedures written in managed code need a way to return
rows of data. The SqlPipe object is used within the stored procedure code to achieve this data. Rows of
data can be returned by this object.
If you call the Send method of the SqlPipe object and pass a string value to it, the outcome is the same as
if you had issued a PRINT statement in a T-SQL based stored procedure. You will see the values returned
in the Messages tab within SQL Server Management Studio.

You can see the SqlPipe object used in the following code:
public partial class StoredProcedures
{
[SqlProcedure]
public static void ProductsByColor(SqlString Color)
{
SqlConnection conn =
new SqlConnection("context connection=true");
SqlCommand command = conn.CreateCommand();
SqlPipe outputPipe = SqlContext.Pipe;
outputPipe.Send("Hello. It's now " +
DateTime.Now.ToLongTimeString() + " at the server.");
if (Color.IsNull)
{
command.CommandText =
"SELECT * FROM Production.Product "
+ "WHERE (Color IS NULL) ORDER BY ProductID";
}
else
{
command.CommandText =
"SELECT * FROM Production.Product "
+ "WHERE (Color = @Color) ORDER BY ProductID";
command.Parameters.Add(
new SqlParameter("@Color", Color.Value));
}
conn.Open();
outputPipe.Send(command.ExecuteReader());
conn.Close();
}
};

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-29

Implementing Managed Code in SQL Server 2012

Stored Procedures External Access

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

16-30

In the previous topic, you learned how simple it is to replace T-SQL stored procedures that perform data
access with stored procedures written in managed code. You also learned how unlikely it is for stored
procedure written in managed code to be useful. This is because the appropriate use cases for stored
procedures written in managed code are more likely to involve access to external resources.

EXTERNAL_ACCESS Permission Set

A stored procedure that accepts data to be written to an operating system file would be a more useful
example of implementing stored procedures in managed code. The stored procedure would create the
file and write the data to it.
Access to the filesystem requires EXTERNAL_ACCESS permission when the assembly that contains the
method is cataloged.

Avoid the Need for Many Extended Stored Procedures

The use of managed code in stored procedures also removes the need for the use of many extended
stored procedures. xp_cmdshell is an example of an extended stored procedure that is disabled by default
in SQL Server, yet many applications require it to be enabled. This lets the applications perform operations
at the filesystem level. Enabling xp_cmdshell is undesirable from a security perspective and managed code
provides alternate ways to implement this required functionality in a much safer form.
Question: What would be a good use case for stored procedures in managed code?

Triggers

Key Points
Both DML and DDL triggers can be implemented from within managed code.

CREATE TRIGGER

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-31

The CREATE TRIGGER statement is used to catalog methods in managed code assemblies as triggers. The
relevant deployment attribute is SqlTrigger. The SqlTrigger attribute properties that are most useful are:

Name this indicates the name that the trigger will be called when deployed

Target the name of the object that the trigger will be attached to

Event the event (or events) that the trigger will fire on

Access to Modifications

As with triggers written in T-SQL, triggers written in managed code are able to access the details of the
changes being made or the commands that have been executed.

Within DML triggers, access is provided to the inserted and deleted virtual tables in exactly the same way
as in DML triggers written in T-SQL.
Similarly, within DDL triggers access is provided to the XML EVENTDATA structure.

SqlTriggerContext

A DML trigger can be associated with multiple events on an object. Within the code of a DML trigger, you
may need to know which event has caused the trigger to fire. The SqlTriggerContext class can be used to
build logic based on the event that caused the trigger to fire.

Implementing Managed Code in SQL Server 2012

Demonstration 3B: Creating Stored Procedures and Triggers

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

16-32

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

Open the 21 Demonstration 2A.sql script file and execute steps 1 to 3.

2.

Open the 32 Demonstration 3B.sql script file.

3.

Follow the instructions contained within the comments of the script file.

User-defined Aggregates

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-33

User-defined aggregates (UDAGGs) are an entirely new type of object for SQL Server and cannot be
created in T-SQL. The ability to create aggregates allows you to provide additional aggregates that are
not provided by the built-in set of aggregates. For example, you might decide that you need a MEDIAN
aggregate but SQL Server does not supply one. Another good use case for creating aggregates occurs
when migrating code from another database engine that offers different aggregates or aggregates other
than those provided by SQL Server. Aggregates could also be created to operate on data types that are
not supported by built-in aggregates, including user-defined data types.

CREATE AGGREGATE

The CREATE AGGREGATE statement is used to catalog UDAGGs written in managed code. The relevant
deployment attribute is SqlUserDefinedAggregate. Note that the path to a struct or class will be a two
part name as shown in the EXTERNAL NAME clause on the slide.

Serializable

SQL Server needs to be able to store interim results while calculating the value of an aggregate. In
managed code, the ability to save an object as a stream of data is called serializing the object. UDAGGs
need to be Serializable. In managed code, they can be implemented as either classes or structs (data
structures). Most UDAGGs would be implemented as structs rather than as classes, as structs are easier to
implement.

The property Format.Native shown in the slide example is indicating that the struct will be serialized using
the standard serialization mechanisms built-in to the .NET Framework. The built-in serialization can only
be used with simple data types. For more complex data types, user-defined serialization needs to be
added.

Implementing Managed Code in SQL Server 2012

Attribute Properties
A few more useful attribute properties are shown in the example.

MCT USE ONLY. STUDENT USE PROHIBITED

16-34

IsInvariantToDuplicates this tells SQL Server that the result of the aggregate is the same even if it
doesnt see the values from every row. It only needs to see unique values. To visualize this, consider
which rows the built-in MAX or MIN aggregates need to process and how this compares to which
rows the built-in COUNT aggregate needs to see.

IsInvariantToNulls this tells SQL Server that the result of the aggregate is unaffected by seeing rows
that do not have a value in the relevant column.

IsNullIfEmpty this tells SQL Server that if no rows at all need to be processed that the aggregate
does not need to be called at all as the result will be NULL anyway.

Name this tells Visual Studio (or the deployment utility) what name the aggregate should have
when cataloged.

Note that this is not a complete list of all the possible properties, just the most useful ones.

Question: Can you think of another common mathematical aggregate that would be useful in SQL
Server?

User-defined Data Types

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-35

The ability to create user-defined data types (UDDTs) in managed code allows you to extend the data
type system available from within SQL Server. There is no T-SQL equivalent method of doing this. Userdefined data types allow for the determination not only what data is stored, but also how the data type
behaves.

CREATE TYPE

The CREATE TYPE statement is used to catalog UDDTs. The data type will be defined as a class in a
managed code assembly. Similar to user-defined aggregates, data types need to be Serializable as SQL
Server needs to be able to store them. The deployment attribute is SqlUserDefinedType.

The geometry, geography, and hierarchyid system data types are in fact system CLR data types. Their
operation is unrelated to the clr enabled configuration setting at the SQL Server instance level. The clr
enabled option only applies to user-created managed code.

Accessing Properties and Methods


The properties of an instance of a managed code data type are accessed via:
InstanceOfTheType.Property eg: @Shape.STArea

The methods of an instance of a managed code data type are accessed via:
InstanceOfTyeType.Method() eg: @Shape.STDistance(@OtherShape)

Managed code data types might also include functionality that is useful without creating an object of the
data type first. This allows you to expose functions from within a data type somewhat like a code library.
The methods of the managed code data type itself are accessed via:
TypeName::Method() eg: GEOMETRY::STGeomFromText(POINT (12 15))

Implementing Managed Code in SQL Server 2012

Considerations for User-defined Data Types

Key Points
UDDTs are not directly comparable but their properties are.

Comparing Managed Code Data Types


With the built-in set of system data types, a variety of operations are defined for each data type. For
example, you can compare two integers or strings.

MCT USE ONLY. STUDENT USE PROHIBITED

16-36

User-defined data types cannot be compared using operations such as > (greater than) or < (less than).
This means they cannot be sorted, used in a SELECT DISTINCT clause, or directly indexed.

Note: The one exception to this is that binary comparisons are permitted when the IsByteOrdered
property of the SqlUserDefinedDataType attribute is set to true. Even in this situation, only a simple
binary comparison is performed.

The individual properties of a data type are comparable. For example, two geometry data types cannot be
compared via code as shown below:
IF (@Shape1 < @Shape2) BEGIN

However, the properties of the two data types can be compared as shown below:
IF (@Shape1.X < @Shape2.X) BEGIN

Indexing User-defined Data Type Properties

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-37

While you cannot create indexes on user-defined data types, some system managed code data types have
special indexes. For example, the geometry and geography data types can be indexed by a special type of
index known as a spatial index.

For UDDTs, there is no method for creating new types of index to support them. What you can do is
create a persisted calculated column in the same table and use it to promote the properties of the UDDT
into standard relational columns. These columns can then be indexed.

Operator Overloading

In object-oriented programming, it is possible to define or change the operators that operate on the
object. User-defined data types do not offer this capability. For example, a customized meaning for a >
(greater than) operator cannot be defined.

Implementing Managed Code in SQL Server 2012

Demonstration 3C: Creating Aggregates and User-defined Data Types

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

16-38

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

Open the 21 Demonstration 2A.sql script file and execute steps 1 to 3.

2.

Open the 33 Demonstration 3C.sql script file.

3.

Follow the instructions contained within the comments of the script file.

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-39

Lab 16: Implementing Managed Code in SQL Server

Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ.ssmssln.

7.

From the View menu, click Solution Explorer. In Solution Explorer, double-click the query
00-Setup.sql. When the query window opens, click Execute on the toolbar.

Lab Scenario

You are concerned that one of your company developers has decided to implement almost all of her logic
in SQL CLR assemblies. You will determine if this is appropriate. Also in this lab, you will implement and
test a supplied .NET assembly. You will also investigate all installed assemblies on the system.

Implementing Managed Code in SQL Server 2012

Supporting Documentation
The following list details the proposed functionality being considered for managed code.

Proposed SQLCLR Functionality


Table-valued function that returns a list of files in a particular folder.
Function that formats phone numbers as strings.
Trigger that records balance movements with a value of more than 1000.
Stored procedure that writes an XML file for a given XML parameter.
Function that counts rows in a table.
A new Customer data type.

Exercise 1: Assess Proposed CLR Code


Scenario

MCT USE ONLY. STUDENT USE PROHIBITED

16-40

You need to assess a list of proposed functions and determine which functions should or should not be
implemented via SQL CLR logic.
The main tasks for this exercise are as follows:
1.

Review the supporting documentation.

2.

For each object listed, determine if implementing it in managed code is appropriate or not.

f Task 1: Review the supporting documentation

Review the proposed list of managed code objects.

f Task 2: For each object listed, determine if implementing it in managed code is


appropriate or not

Work through the list of proposed objects and for each object, decide if it should or should not be
implemented in managed code and why.

Results: After this exercise, you should have created a list of which objects should and should not be
implemented in managed code and the reasons for your decision.

Exercise 2: Implement a CLR Assembly


Scenario
You have been provided with an existing .NET assembly. You will implement it within SQL Server.
The main tasks for this exercise are as follows:
1.

Ensure the database is configured appropriately to support an EXTERNAL_ACCESS assembly.

2.

Catalog the assembly and the functions contained within it.

3.

Test the functions contained within the assembly.

f Task 1: Ensure the database is configured appropriately to support an


EXTERNAL_ACCESS assembly
1.

Ensure that SQL CLR integration is enabled for the SQL Server instance.

2.

Flag the MarketDev database as trustworthy.

f Task 2: Catalog the assembly and the functions contained within it

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-41

1.

Use CREATE ASSEMBLY to catalog the supplied sample assembly as alias SQLCLRDemo. The path to
the assembly is D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ\SQLCLRDemo.DLL.

2.

Query the sys.assemblies and sys.assembly_files system views to confirm the details of how the
assembly has been cataloged.

3.

Use the CREATE FUNCTION statement to catalog the function dbo.IsValidEmailAddress. It takes a
parameter named @email of type nvarchar(4000) and returns a bit. It is found in the assembly at
path: SQLCLRDemo.[SQLCLRDemo.CLRDemoClass].IsValidEmailAddress.

4.

Use the CREATE FUNCTION statement to catalog the function dbo.FormatAustralianPhoneNumber. It


takes a single parameter @PhoneNumber of type nvarchar(4000). It returns nvarchar(4000). It is
found in the assembly at path:
SQLCLRDemo.[SQLCLRDemo.CLRDemoClass].FormatAustralianPhoneNumber.

5.

Use the CREATE FUNCTION statement to catalog the function dbo.FolderList. It takes two parameter
@RequiredPath of type nvarchar(4000) and @FileMask of type nvarchar(4000). It returns a table of
filenames, with one column called FileName of type nvarchar(4000). It is found in the assembly at
path: SQLCLRDemo.[SQLCLRDemo.CLRDemoClass].FolderList.

f Task 3: Test the functions contained within the assembly

Execute the following T-SQL statements to test the functions that have been cataloged:
SELECT dbo.IsValidEmailAddress('test@somewhere.com');
GO
SELECT dbo.IsValidEmailAddress('test.somewhere.com');
GO
SELECT dbo.FormatAustralianPhoneNumber('0419201410');
SELECT dbo.FormatAustralianPhoneNumber('9 87 2 41 23');
SELECT dbo.FormatAustralianPhoneNumber('039 87 2 41 23');
GO
SELECT * FROM dbo.FolderList(
'D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ','*.txt');
GO

Results: After this exercise, you should have three functions working as expected.

Implementing Managed Code in SQL Server 2012

MCT USE ONLY. STUDENT USE PROHIBITED

16-42

Challenge Exercise 3: Implement a CLR User-defined Aggregate and CLR


User-defined Data Type (Only if time permits)
Scenario

The sample assembly also includes a user-defined aggregate and a user-defined data type. You will now
catalog and test these objects.
The main tasks for this exercise are as follows:
1.

Catalog and test the user-defined aggregate.

2.

Catalog and test the user-defined data type.

f Task 1: Catalog and test the user-defined aggregate


1.

Use the CREATE AGGREGATE statement to catalog the user-defined aggregate dbo.AggString.

2.

dbo.AggString takes a single nvarchar(4000) parameter and returns type nvarchar(4000). It is found in
the assembly at path: SQLCLRDemo.[SQLCLRDemo.AggString].

3.

Execute the following T-SQL statements to test the operation of the aggregate.
SELECT dbo.AggString(DISTINCT ProductNumber)
FROM Marketing.Product
WHERE Color = 'Black';
GO

f Task 2: Catalog and test the user-defined data type


1.

Use the CREATE TYPE statement to catalog the user-defined data type dbo.zVarChar. It is found in the
assembly at path: SQLCLRDemo.[SQLCLRDemo.zVarChar].

2.

Execute T-SQL statements similar to the following to test the operation of the data type. (The exact
statements required would depend upon your table design).
CREATE TABLE dbo.TestTable
( RecID int IDENTITY(1,1),
TextValue zVarChar
);
INSERT INTO dbo.TestTable VALUES('Some
GO

compressible

SELECT TextValue.ToString(),
TextValue.CompressedLength(),
TextValue.CompressionPercentage()
FROM dbo.TestTable;
GO
SELECT AVG(TextValue.CompressionPercentage()) FROM dbo.TestTable;
GO
DROP TABLE dbo.TestTable;
GO

data');

3.

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

16-43

Execute the following T-SQL statements to see how to call a method on the class itself, rather than on
an instance of the class. (First, show how to call the method on an instance. Then, show how to call
directly on the class so that you can see the difference).
DECLARE @TestString zVarChar;
SET @TestString = 'Some
compressible
PRINT @TestString.CompressedValue();
GO
PRINT zVarChar::Compress(
'Some
GO

compressible

value';

value');

Results: After this exercise, you should have cataloged and tested both the user-defined aggregate and
the user-defined data type.

Implementing Managed Code in SQL Server 2012

Module Review and Takeaways

Review Questions
1.

Which types of database objects can be implemented using managed code?

2.

What purpose do attributes have in CLR managed code?

Best Practices

MCT USE ONLY. STUDENT USE PROHIBITED

16-44

1.

The biggest mistake made when deciding between T-SQL and Managed Code is to assume that either
one is the correct answer for every situation. Each has benefits and limitations and should be used for
the appropriate tasks.

2.

Developers should avoid implementing using SQL CLR to implement code that would be better
placed on another application tier (such as on a client system).

3.

DBAs should avoid refusing to allow SQL CLR code without consideration. As you have seen in this
module, there is code that should be implemented in Managed Code rather than in T-SQL.

4.

DBAs should set boundaries for developers:

No row-based code that should be set-based T-SQL operations.

Limited use of EXTERNAL_ACCESS permissions and only after justification.

Rare use of UNSAFE permissions and only after very serious justifications and testing.

MCT USE ONLY. STUDENT USE PROHIBITED


17-1

Module 17
Storing XML Data in SQL Server 2012
Contents:
Lesson 1: Introduction to XML and XML Schemas

17-3

Lesson 2: Storing XML Data and Schemas in SQL Server

17-15

Lesson 3: Implementing XML Indexes

17-26

Lab 17: Storing XML Data in SQL Server

17-31

Storing XML Data in SQL Server 2012

Module Overview

MCT USE ONLY. STUDENT USE PROHIBITED

17-2

Extensible Markup Language (XML) provides rules for encoding documents in a machine-readable form.
It has become a very widely adopted standard for representing data structures rather than sending
unstructured documents. Microsoft SQL Server servers often need to use XML to interchange data
with other systems and many SQL Server tools provide an XML based interface.

SQL Server has extensive handling of XML both for storage and for querying. This module introduces XML
and shows how XML data can be stored within SQL Server. The next module will show how XML data can
be queried, including by queries written in a language called XQuery.

Objectives
After completing this module, you will be able to:

Describe XML and XML schemas

Store XML data and associated XML schemas in SQL Server

Implement the XML data type within SQL Server

Lesson 1

Introduction to XML and XML Schemas

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

Before covering how to work with XML in SQL Server, it is important to gain an understanding of XML
itself and how it is used external to SQL Server. Some core XML-related terminology needs to be
understood along with concepts of how the structure of XML can be validated and enforced using
schemas. One common problem with the use of XML in SQL Server is a tendency to over-use it. It is
important to gain an understanding of the appropriate uses for XML when working with SQL Server.

Objectives
After completing this lesson, you will be able to:

Describe why XML matters in relation to SQL Server

Explain core XML concepts

Explain the difference between documents and fragments

Describe the role of XML namespaces

Describe the role of XML schemas

Determine appropriate use cases for XML data storage in SQL Server

17-3

Storing XML Data in SQL Server 2012

Discussion: XML Usage in SQL Server

Discussion

MCT USE ONLY. STUDENT USE PROHIBITED

17-4

The use of XML within SQL Server is new to many database administrators and database developers. XML
has often been seen more as an application developer topic.
Question: Why do you imagine that two modules of this course have been dedicated to XML within
SQL Server?
XML is very useful for data interchange and for storing data with an arbitrary or complex structure. It is
common for data arriving into SQL Server to already be in an XML format. It is also common to wish to
provide XML-based output from SQL Server.
Many aspects and components of SQL Server are now XML-based, including:

SQL Server can store XML data and XML schemas and can validate XML data against an XML schema.

DDL triggers return details of the event that caused the trigger to fire via an XML data structure called
EVENTDATA.

Extended events is a high-performance tracing capability that returns its trace data in XML format.

Logon triggers return details through an EVENTDATA structure similar to the one provided by DDL
triggers.

Service Broker is a transaction based queuing system that is contained within a database. Most data
that travels on Service Brokers queues is XML based.

Event notifications are used to send details of DDL events via a Service Broker queue. These
notifications are XML based.

This means that even database administrators will benefit from a level of understanding of how to work
with XML in SQL Server, if they wish to progress to more advanced usage of the product.

Core XML Concepts

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-5

XML is a plain-text, Unicode-based meta-language (a language used to describe language). It can be


used to hold both structured and semi-structured data and isnt tied to any particular vendor, language
or operating system. It provides access to a wide range of technologies for manipulating, structuring,
transforming and querying data.

Data Interchange

XML came to prominence as a format for interchanging data between systems. It follows the same basic
structure rules as other markup languages (such as HTML) and is used as a self-describing language.
Consider the following XML document, which is the same as on the slide:
<?xml version="1.0" encoding="iso-8859-1" ?>
<?xml-stylesheet href="orders.xsl"?>
<order id="ord123456">
<customer id="cust0921">
<first-name>Dare</first-name>
<last-name>Obasanjo</last-name>
<address>
<street>One Microsoft Way</street>
<city>Redmond</city>
<state>WA</state>
<zip>98052</zip>
</address>
</customer>
</order>

Question: With no knowledge apart from the document above, what would you imagine it contains
details of?

Storing XML Data in SQL Server 2012

MCT USE ONLY. STUDENT USE PROHIBITED

17-6

Without any context and information, you can determine that this document holds the details about an
order, the customer who placed the order along with the customers name and address details. This
explains why XML is defined as a self-describing language. In formal terminology, this is described as
deriving a schema from a document.

XML Specifics

The two first lines in the example document that start with ?xml are referred to as processing
instructions. These instructions are not a part of the data but determine the details of encoding. The first
instruction in the example shows that version 1.0 of the XML specification is being used along with a
specific encoding of iso-8859-1. The second instruction indicates the use of the extensible style sheet
(XSL) orders.xsl to format the document for display, if displaying the document is necessary.

The third line of the example is the order element. Note that the document data starts with an opening
order element and finishes with a closing order element shown as </order>. The order element also has
an associated attribute named id.
It is important to realize that elements in XML (as in most other markup languages) are case-sensitive.

Attribute-centric vs. Element-centric XML


There are two basic ways to encode data in XML. The following example shows element-centric XML:
<Customer>
<Name>Tailspin Toys</Name>
<Rating>12</ Rating >
</Customer>

The following example shows the equivalent data in attribute-centric XML:


<Customer Name="Tailspin Toys" Rating="12">
</Customer>

Note that if all data for an element is contained in attributes, then a shortcut form of element is available.
As an example, the two XML elements below are equivalent:
<Customer Name="Tailspin Toys" Rating="12"></Customer>
<Customer Name="Tailspin Toys" Rating="12" />

Question: Do you use XML for exchanging data between your organization and another
organization?

Fragments vs. Documents

Key Points
Well-formed XML has only one top-level element and element tags are correctly nested within each
other. Text with multiple top-level elements is considered a fragment, not a document.

Documents vs. Fragments


Consider the following XML document:
<order id="ord123456">
<customer id="cust0921" />
</order>

This provides the details for a single order and would be considered to be an XML document. Now,
consider the following:
<order id="ord123456">
<customer id="cust0921" />
</order>
<order id="ord123457">
<customer id="cust0925" />
</order>

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-7

This text contains the details of multiple orders. While it is perfectly reasonable XML, it is considered to be
a fragment of XML rather than a document.

Storing XML Data in SQL Server 2012

To be called a document, the XML needs to have a single root element as shown in the following:
<orders>
<order id="ord123456">
<customer id="cust0921" />
</order>
<order id="ord123457">
<customer id="cust0925" />
</order>
</orders>

Question: How could the XML fragment shown in the slide be converted to an XML document?

MCT USE ONLY. STUDENT USE PROHIBITED

17-8

XML Namespaces

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-9

An XML namespace is a collection of names that can be used as element or attribute names. It is used to
avoid conflicts with other names. Imagine an XML instance that contains references to both a product and
an order. Both of these elements could have a child element called id. Any reference to the id element
could easily be ambiguous. Namespaces are used to remove that ambiguity.

XML Namespaces

An XML namespace is defined using the special attribute xmlns and the value of the attribute must be a
valid Universal Resource Identifier (URI). As an example, an XML namespace attribute could be:
xmlns="http://schemas.microsoft.com/sqlserver/profiles/gml"

Note that specifying an address in a namespace does not necessarily suggest that the URI provided could
be used to retrieve the details in any particular format. Many URIs used in namespaces only link to an
address where a human-readable description of the namespace is found. Many other URIs do not lead to
any real resources at all. The URI is simply used as a unique identifier for the namespace to reduce the
possibility of duplicate entries.

Prefixes

When declaring a namespace, an alias for the namespace is assigned. In XML terminology, this alias is
called a prefix because of the way it is used within the remainder of the XML. You can see this in the
snippet below from the example shown in the slide:
xmlns="urn:AW_NS" xmlns:o="urn:AW_OrderNS"

Storing XML Data in SQL Server 2012

MCT USE ONLY. STUDENT USE PROHIBITED

17-10

Two namespaces have been declared. The second namespace has been assigned the prefix o. The prefix
is then used later to identify which namespace any element name is part of as shown below:
<o:Order SalesOrderID="43860" Status="5"
OrderDate="2001-08-01T00:00:00">
<o:OrderDetail ProductID="761" Quantity="2"/>
<o:OrderDetail ProductID="770" Quantity="1"/>
</o:Order>

In this snippet, the Order and OrderDetail elements are identified as being part of the urn:AW_OrderNS
namespace by being prefixed by o.
Question: Why do you imagine that aliases are typically used with namespaces?

XML Schemas

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-11

XML schemas are used to provide rules that determine the specific elements, attributes, and layout that
should be permitted within an XML document.

XML Schemas

The Worldwide Web Consortium (W3C) defined XML schemas as a more capable replacement for earlier
objects called Document Type Definitions (DTDs).
An XML schema defines:

elements that can or must appear in a document

attributes that can or must appear in a document

which elements are child elements

the order of child elements

the number of child elements

whether an element is empty or can include text

data types for elements and attributes

default and fixed values for elements and attributes

XML schemas are often also referred to as XML Schema Definitions (XSDs). XSD is also the default file
extension used by most products when storing XML schemas in operating system files.

Storing XML Data in SQL Server 2012

Appropriate Usage of XML Data Storage in SQL Server

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

17-12

Given how widely XML has come to be used in application development in higher application tiers, there
is a tendency towards the overuse of XML within the database. It is important to consider when it is and
when it is not appropriate to use XML within SQL Server.

XML vs. Objects

Higher level programming languages that are used for constructing application programs often represent
entities such as customers and orders as objects. Many developers see SQL Server as a simple repository
for objects, that is, an object-persistence layer. Consider the following table definition:
CREATE TABLE dbo.Object
( ObjectID uniqueidentifier PRIMARY KEY,
PersistedData xml
);

While there is no suggestion that this would make for a good database design, note that this table design
could be used to store all objects from an application: customers, orders, payments, etc. all in a single
table. Compare this to how tables have been traditionally designed in relational databases.
Question: What is the basic problem with using this table design as your entire database design?

SQL Server provides the developer with a wide range of choices with a simple XML design at one end of
the spectrum and fully normalized relational tables at the other end. It is important to understand that
there is no generic right and wrong answer for where in this range of options that a table should be
designed.

Appropriate Use Cases


There are a number of reasons for storing XML data within SQL Server.

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-13

You may be dealing with data that is already in XML, such as an order being received electronically
from a customer. You may wish to share, query and modify the XML data in an efficient and
transacted way.

You may need to achieve a level of interoperability between your relational and XML data. Imagine
the need to join a customer table with a list of customer IDs that are being sent to you as XML.

You may need to use XML formats to achieve cross-domain applications and need to have maximum
portability for your data. Other systems that you are communicating with may be based on entirely
different technologies and not represent data in the same way as your server.

You may not know the structure of your data in advance. It is common to have a mixture of
structured and semi-structured data. A table might hold some standard relational columns but also
hold some less structured data in XML columns.

You may have very sparse data. Imagine a table with thousands of columns where only a few columns
or a few rows ever tend to have any data in them. (Sparse column support in SQL Server provides
another mechanism for dealing with this situation but it also uses XML in the form of XML column
sets. Sparse columns are an advanced topic beyond the scope of this current course.)

You may need to have order within your data. For example, order detail lines might need to be
retained in a specific order. Relational tables and views have no implicit order. XML documents can
exhibit a predictable order.

You may wish to have SQL Server validate that your XML data meets a particular XML schema before
processing it.

You may wish to store transferred XML data for historical reasons.

You may wish to create indexes on your XML data to make it faster to query.

Storing XML Data in SQL Server 2012

Demonstration 1A: Using XML and XML Schemas

Demonstration Steps

MCT USE ONLY. STUDENT USE PROHIBITED

17-14

1.

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click SQL
Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to D:\10776A_Labs\10776A_17_PRJ\10776A_17_PRJ.ssmssln and click Open.

3.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file from
within Solution Explorer.

4.

Open the 11 Demonstration 1A.sql script file.

5.

Follow the instructions contained within the comments of the script file.
Question: What would the likely problem be if NULL elements are simply omitted?

Lesson 2

Storing XML Data and Schemas in SQL Server

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-15

After coming to an understanding of XML, schemas and the surrounding terminology, you can now turn
your attention to how XML data and schemas can be stored within SQL Server. This is the first step in
learning how to process XML effectively within SQL Server.
You need to see how the XML data type is used, how to define schema collections that contain XML
schemas, how to declare both typed and untyped variables and database columns and how to specify
how well-formed the XML data needs to be before it can be stored.

Objectives
After completing this lesson, you will be able to:

Use the XML data type

Create XML schema collections

Declare variables and database columns as either typed or untyped XML

Choose whether XML fragments can be stored rather than entire XML documents

Storing XML Data in SQL Server 2012

XML Data Type

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

17-16

SQL Server 2005 introduced a native data type for storing XML data in SQL Server. It can be used for
variables, parameters and columns in databases. SQL Server also exposes a number of methods that can
be used for querying or modifying the XML data.

xml Data Type


xml is a built-in data type for SQL Server. It is an intrinsic data type, which means that it is not
implemented separately via managed code. The xml Data Type is limited to a maximum size of 2GB.

Variables, parameters and database columns can be declared as xml data type. You can see a variable
declared using xml data type in the following code declaration:
DECLARE @Settings xml;

Once you have declared a variable of xml data type, by default any well-formed XML can be stored in it.
Look at the assignments in the following code block:
SET @Settings = '<Customer Name="Terry"></Customer>";
SET @Settings = '<Customer Name="Terry"><Customer>';

The first assignment would be successful and the second assignment would fail as the value being
assigned is not well-formed XML.

Canonical Form

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-17

It is important to realize that SQL Server stores XML data in an internal format that makes it easier for it to
process the XML data when required. It does not store the XML in the same format (including whitespace)
as the data was received in. For example, look at the following code block:
DECLARE @Settings xml;
SET @Settings = N'<Customer Name="Terry"></Customer>';
SELECT @Settings;

When executed, the result returned is as follows:

Note that the output returned is logically equivalent to the input but the output is not in exactly the same
format as the input. It is referred to as having been returned in a canonical or logically-equivalent form.

Storing XML Data in SQL Server 2012

XML Schema Collections

Key Points
While the xml data type will only store well-formed XML, it is possible to further constrain the stored
values by associating the data type with an XML schema collection.

XML Schema Collections

MCT USE ONLY. STUDENT USE PROHIBITED

17-18

In the first lesson, you learned how XML schemas can be used to constrain what can be stored in an XML
document. SQL Server does not store XML schemas as database objects. SQL Server has an XML SCHEMA
COLLECTION object that holds a collection of XML schemas.

When you associate an XML SCHEMA COLLECTION with an XML variable, parameter, or database column,
then the XML to be stored in that location needs to conform to at least one of the schemas that is
contained in the schema collection.

XML Schemas

While they are somewhat legible to humans, XML schemas are designed to be processed by computer
systems. Even simple schemas tend to have quite a level of complexity. Fortunately, you do not need to be
able to read (or worse write!) such schemas. They are generally created by tools and utilities. SQL Server
can also create XML schemas. You will see an example of this in a later lesson.
An example of an XML schema is shown on the following page:

<xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1"
xmlns:schema="urn:schemas-microsoft-com:sql:SqlRowSet1"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:sqltypes=
"http://schemas.microsoft.com/sqlserver/2004/sqltypes"
elementFormDefault="qualified">
<xsd:import namespace=
"http://schemas.microsoft.com/sqlserver/2004/sqltypes"
schemaLocation="http://schemas.microsoft.com/
sqlserver/2004/sqltypes/sqltypes.xsd" />
<xsd:element name="Production.Product">
<xsd:complexType>
<xsd:attribute name="ProductID" type="sqltypes:int"
use="required" />
<xsd:attribute name="Name" use="required">
<xsd:simpleType sqltypes:sqlTypeAlias=
"[AdventureWorks].[dbo].[Name]">
<xsd:restriction base="sqltypes:nvarchar"
sqltypes:localeId="1033" sqltypes:sqlCompareOptions=
"IgnoreCase IgnoreKanaType IgnoreWidth">
<xsd:maxLength value="50" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="Size">
<xsd:simpleType>
<xsd:restriction base="sqltypes:nvarchar"
sqltypes:localeId="1033" sqltypes:sqlCompareOptions=
"IgnoreCase IgnoreKanaType IgnoreWidth">
<xsd:maxLength value="5" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="Color">
<xsd:simpleType>
<xsd:restriction base="sqltypes:nvarchar"
sqltypes:localeId="1033" sqltypes:sqlCompareOptions=
"IgnoreCase IgnoreKanaType IgnoreWidth">
<xsd:maxLength value="15" />
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Creating an XML Schema Collection

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-19

An XML schema collection holds one or more schemas. The data being validated must match at least one
of the schemas within the collection.
You create an XML schema collection with the CREATE XML SCHEMA COLLECTION syntax shown in the
following code snippet:
CREATE XML SCHEMA COLLECTION SettingsSchemaCollection
AS
N'<?xml version="1.0" ?>
<xsd:schema
...
</xsd:schema>';

Storing XML Data in SQL Server 2012

Altering Schema Collections

MCT USE ONLY. STUDENT USE PROHIBITED

17-20

Only a limited ability to modify a schema collection is provided in T-SQL. New schema components can
be added to an existing schema collection by using the T-SQL ALTER SCHEMA COLLECTION statement.

System Views
The details of the existing XML schema collections can be seen by querying the
sys.xml_schema_collections system view.

The details of the namespaces that are referenced by XML schema collections can be seen by querying
the sys.xml_schema_namespaces system view.
Like XML, XML schema collections are not stored in the format that you enter them. They are stripped
into an internal format. You can get an idea of how they are stored by querying the
sys.xml_schema_components system view as shown in the following code:
SELECT cp.*
FROM sys.xml_schema_components cp
JOIN sys.xml_schema_collections c
ON cp.xml_collection_id = c.xml_collection_id
WHERE c.name = 'SettingsSchemaCollection';

Untyped vs. Typed XML

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-21

When storing XML data, you can choose to allow any XML to be stored or you can choose to constrain
the available values by associating the XML location with an XML schema collection.

Untyped XML

You may choose to store any well-formed XML. One reason is that you might not have a schema for the
XML data. Another reason is that you might want to avoid the processing overhead involved in validating
the XML against the XML schema collection. For complex schemas, validating the XML can involve
substantial work. The following example shows a table being created with an untyped XML column:
CREATE TABLE App.Settings
( SessionID int PRIMARY KEY,
WindowSettings xml
);

Any well-formed XML could be stored in the WindowSettings column up to the maximum size of a SQL
Server XML object which is currently 2GB.

Typed XML

You may wish to have SQL Server validate your data against a schema. You might want to take advantage
of storage and query optimizations based on the type information or want to take advantage of this type
information during the compilation of your queries. The following example shows the same table being
created with a typed XML column:
CREATE TABLE App.Settings
( SessionID int PRIMARY KEY,
WindowSettings xml (SettingsSchemaCollection)
);

Storing XML Data in SQL Server 2012

MCT USE ONLY. STUDENT USE PROHIBITED

17-22

In this case, a schema collection called SettingsSchemaCollection has been defined and SQL Server will not
allow data to be stored in the WindowSettings column if it does not meet the requirements of at least one
of the XML schemas in the SettingsSchemaCollection.
Question: What types of errors could occur if the XML data you were querying did not follow the
schema you were expecting?

CONTENT vs. DOCUMENT

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-23

While specifying typed XML, you can also specify whether entire XML documents must be provided or if
XML fragments can be stored.

CONTENT
In the example used in the last topic, a table was defined by this code:
CREATE TABLE App.Settings
( SessionID int PRIMARY KEY,
WindowSettings xml (SettingsSchemaCollection)
);

This is equivalent to defining the table by the following code as the keyword CONTENT is the default
value for typed XML declarations:
CREATE TABLE App.Settings
( SessionID int PRIMARY KEY,
WindowSettings xml (CONTENT SettingsSchemaCollection)
);

Note the addition of the CONTENT keyword. When CONTENT is specified, XML fragments and entire
well-formed XML documents can be stored in the typed XML location.

Storing XML Data in SQL Server 2012

DOCUMENT

MCT USE ONLY. STUDENT USE PROHIBITED

17-24

The alternative to the default value of CONTENT is to specify the keyword DOCUMENT as shown in the
following code:
CREATE TABLE App.Settings
( SessionID int PRIMARY KEY,
WindowSettings xml (DOCUMENT SettingsSchemaCollection)
);

In this case, XML fragments would not be able to be stored in the WindowSettings column. Only wellformed XML documents could be stored. For example, a column that is intended to store a customer
order can then be presumed to actually hold a customer order and not some other type of XML
document.

Demonstration 2A: Working with Typed vs. Untyped XML

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-25

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_17_PRJ\10776A_17_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

2.

Open the 21 Demonstration 2A.sql script file.

3.

Follow the instructions contained within the comments of the script file.
Question: What is the difference between an XML SCHEMA COLLECTION and an XML schema?

Storing XML Data in SQL Server 2012

Lesson 3

Implementing XML Indexes

MCT USE ONLY. STUDENT USE PROHIBITED

17-26

The final core aspect related to storing XML data in SQL Server is the creation of indexes on the XML
columns. These are critical for achieving high performance of XML based queries. There are four types of
XML index: a primary index and three types of secondary index. It is important to know how each can be
used to achieve the maximum performance gain for your queries.

Objectives
After completing this lesson, you will be able to:

Describe the need for XML indexes

Explain how each of the four types of XML index can be used

What are XML Indexes?

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-27

Indexes are used in SQL Server to improve the performance of queries. XML indexes are used to improve
the performance of XQuery-based queries. (XQuery will be discussed in the next module.)

XML Indexes

Many systems query XML data directly as text. This can be very slow, particularly if the XML data is large.
You saw earlier how XML data is not directly stored in a text format in SQL Server. For ease of querying, it
is broken into a form of object tree that makes it easier to navigate in memory.
Rather than having to create these object trees as required for queries, which is also a relatively slow
process, XML indexes can be defined. An XML index is somewhat like a copy of an XML object tree that is
saved into the database for rapid reuse.
It is important to note that XML indexes can be quite large compared to the underlying XML data. While
relational indexes are often much smaller than the tables they are built on, it is not uncommon to see
XML indexes that are larger than the underlying data.

You should also consider alternatives to XML indexes. Promoting a value that is stored within the XML to
a persisted calculated column would allow a standard relational index to be used to quickly locate the
value.

Storing XML Data in SQL Server 2012

Types of XML Indexes

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

17-28

SQL Server supports four types of XML index: a primary XML index and up to three secondary XML types.

Primary XML Index

The primary XML index basically provides a persisted object tree in an internal format. The tree has been
formed from the structure of the XML, is used to speed up access to elements and attributes within the
XML and avoids the need to read the entire XML document for each and every query.
Before a primary XML index can be created on a table, the table must have a clustered primary key.
Based on the App.Settings table that was used as an example earlier, you could create a primary XML
index by executing the following code:
CREATE PRIMARY XML INDEX PXML_Settings_WindowSettings
ON App.Settings (WindowSettings);

Secondary XML Indexes

Most of the querying benefit comes from primary XML indexes, however, SQL Server also allows for the
creation of three types of secondary XML index. These secondary indexes are each designed to speed up a
particular type of query.
Secondary XML indexes are used to speed up particular types of queries. There are three forms of query
that they help with: PATH, VALUE and PROPERTY. A PATH index helps decide if a particular path to an
element or attribute is valid. It is typically used with the exist() XQuery method. (XQuery is discussed in the
next module). A VALUE index helps obtaining the value of an element or attribute. A PROPERTY index is
used when retrieving multiple values via PATH expressions. You will see the use of these queries in the
next module.

A secondary XML index can only be created once a primary XML index has been established. When
creating the secondary XML index, you need to reference the primary XML index:
CREATE XML INDEX IXML_Settings_WindowSettings_Path
ON App.Settings (WindowSettings)
USING XML INDEX PXML_Settings_WindowSettings FOR PATH;

XML Tooling Support


Note that primary XML indexes can be created in SQL Server Management Studio or via T-SQL
commands. In SQL Server 2012, secondary XML indexes can also be created using SQL Server
Management Studio but in earlier versions, they could only be created using T-SQL commands.
Question: Why does SQL Server support multiple types of secondary XML index?

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-29

Storing XML Data in SQL Server 2012

Demonstration 3A: Implementing XML Indexes

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

17-30

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_17_PRJ\10776A_17_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

2.

Open the 31 Demonstration 3A.sql script file.

3.

Follow the instructions contained within the comments of the script file.

Question: How does SQL Server enforce the requirement that a primary XML index must be created
before a secondary XML index can be created?

Lab 17: Storing XML Data in SQL Server

Lab Setup

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-31

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In the Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_17_PRJ\10776A_17_PRJ.ssmssln.

7.

From the View menu, click Solution Explorer. In Solution Explorer, double-click the query
00-Setup.sql. When the query window opens, click Execute on the toolbar.

Lab Scenario

A new developer in your organization has discovered that SQL Server can store XML directly. He is keen to
use this mechanism extensively. In this lab, you will decide on appropriate usage of XML within the
documented application.
You also have an upcoming project that will require the use of XML data within SQL Server. No members
of your current team have experience working with XML data in SQL Server. You need to learn how to
process XML data within SQL Server and you have been provided with some sample queries to assist with
this learning.

Storing XML Data in SQL Server 2012

Supporting Documentation
Use Cases
Use Cases Requirements
Existing XML data that is stored but not processed
Storing attributes for a customer
Relational data that is being passed through a system but not processed within it
Storing attributes that are nested (that is attributes stored within attributes)

Exercise 1: Appropriate Usage of XML Data Storage in SQL Server


Scenario
In this exercise, you will need to assess the list of use cases provided by your new developer and
determine which are appropriate for XML storage in SQL Server and which are not.
The main tasks for this exercise are as follows:
1.

Review the list of use cases.

2.

Determine whether each is appropriate for XML storage.

f Task 1: Review the list of use cases

Review the list of use cases in the supporting documentation.

f Task 2: Determine whether each is appropriate for XML

Determine whether the use cases are suitable for XML storage.

MCT USE ONLY. STUDENT USE PROHIBITED

17-32

Results: After this exercise, you have seen how to analyze requirements and determine on appropriate
use cases for XML storage.

Exercise 2: Investigate the Storage of XML Data in Variables


Scenario

Before you can begin to work with XML data in your organization, you need to explore how XML data is
stored in variables. You have been provided with a set of sample XML queries to assist with this. In this
exercise, you will review the effect of executing these queries.
The main tasks for this exercise are as follows:

Review and execute the queries, and review the results.

f Task 1: Review and execute the queries, and review the results

Review the queries, execute the queries and determine how the output results relate to the queries.
Do this one query at a time for scripts 17.1 to 17.9.

Results: After this exercise, you have seen how XML data is stored in variables.

Exercise 3: Investigate the Use of XML Schema Collections


Scenario

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

17-33

For some of the XML processing that you will need to perform in your upcoming project, you will need to
validate XML data using XML schemas. In SQL Server, XML schemas are stored in XML schema collections.
You need to investigate how these schemas are used. You have been provided with a set of sample
queries to assist with this. In this exercise, you will review the effect of executing these queries.
The main tasks for this exercise are as follows:

Review and execute the queries, and review the results.

f Task 1: Review and execute the queries, and review the results

Review the queries, execute the queries and note the output. Do this one query at a time for scripts
17.10 and 17.11.

Results: After this exercise, you have seen how to create XML schema collections.

Challenge Exercise 4: Investigate the Creation of Database Columns Based


on XML (Only if time permits)
Scenario

The final aspect of XML data storage in SQL Server that you will need to understand for your upcoming
project is how XML data is stored in tables. You need to investigate how columns of XML data type are
used. You have been provided with a set of sample queries to assist with this. In this exercise, you will
review the effect of executing these queries.
The main tasks for this exercise are as follows:
1.

Review and execute the queries, and review the results.

f Task 1: Review and execute the queries, and review the results

Review the queries, execute the queries and note the results of the queries. Do this one query at a
time for scripts 17.12 to 17.20.

Results: After this exercise, you have seen how to create database columns based on the XML data type.

Storing XML Data in SQL Server 2012

Module Review and Takeaways

Review Questions
1.

What is XML?

2.

How are NULL elements represented in an XML document?

3.

What is the difference between an element and an attribute?

Best Practices

MCT USE ONLY. STUDENT USE PROHIBITED

17-34

1.

Use appropriate data types for your database columns. Do not store all your data in XML columns.

2.

Use XML schemas only when required. Validating data against schemas incurs substantial processing
overhead.

3.

Ensure you have at least basic XML proficiency when working with SQL Server, even if you will be
working primarily in database administration.

4.

Index XML data stored in database columns. Use the appropriate type of index for the types of
queries expected.

MCT USE ONLY. STUDENT USE PROHIBITED


18-1

Module 18
Querying XML Data in SQL Server 2012
Contents:
Lesson 1: Using the T-SQL FOR XML Statement

18-3

Lesson 2: Getting Started with XQuery

18-15

Lesson 3: Shredding XML

18-26

Lab 18: Querying XML Data in SQL Server

18-35

Querying XML Data in SQL Server 2012

Module Overview

MCT USE ONLY. STUDENT USE PROHIBITED

18-2

In the previous module, you saw how Microsoft SQL Server can be used to store XML based data and
how you can index XML database columns to increase the performance of queries. You now need to see
how to query the XML data. The ability to query XML data directly avoids the need to shred it to a
relational format before executing SQL queries. To effectively process XML, it is important to be able to
query XML data in several ways: returning existing relational data as XML, querying data that is already
XML, and shredding XML data into a relational format.

Objectives
After completing this module, you will be able to:

Use the T-SQL FOR XML Statement

Work with basic XQuery queries

Shred XML to a relational form

Lesson 1

Using the T-SQL FOR XML Statement

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-3

There is a common requirement to return data that is stored in relational database columns as XML
documents. Typically this requirement relates to the need to exchange data with other systems, including
those from other organizations. When the FOR XML clause is added to a T-SQL SELECT statement, it
causes the output to be returned as XML instead of as a relational rowset. SQL Server provides a number
of modes for the FOR XML clause to allow the production of many styles of XML document.

Objectives
After completing this lesson, you will be able to:

Explain the role of the FOR XML clause

Use RAW Mode Queries

Use AUTO Mode Queries

Use EXPLICIT Mode Queries

Use PATH Mode Queries

Retrieve Nested XML

Querying XML Data in SQL Server 2012

Introducing the FOR XML clause

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

18-4

The FOR XML clause is used to extend the T-SQL SELECT statement syntax and causes the statement to
return XML instead of rows and columns. It can be configured to return the attributes, elements, and/or
schema that are required for client applications.

FOR XML
The FOR XML clause works in one of four modes:

RAW mode generates a single <row> element per row in the rowset that is returned by the SELECT
statement.

AUTO mode generates nesting in the resulting XML based on the way the SELECT statement is
specified. You have minimal control over the shape of the XML generated. If you need to produce
nested XML, AUTO mode is a better choice than RAW mode.

EXPLICIT mode allows more control over the shape of the XML. It can be used when other modes do
not provide enough flexibility but does so at the cost of greater complexity. You can mix attributes
and elements at will in deciding the shape of the XML.

PATH mode together with the nested FOR XML query capability provides much of the flexibility of
the EXPLICIT mode in a simpler manner.
Question: Why would sending XML data to a supplier be more useful than sending text files with
fixed width columns?

Using RAW Mode Queries

Key Points
RAW mode is the simplest mode to work with in the FOR XML clause. It returns a simple XML
representation of the rowset and can optionally specify a row element name and a root element.

FOR XML RAW


Look at the following simple T-SQL query:
SELECT FirstName, LastName, PersonType
FROM Person.Person
ORDER BY FirstName, LastName;

When executed, it returns the following results:

Now, look at the modified statement after adding the FOR XML clause:
SELECT FirstName, LastName, PersonType
FROM Person.Person
ORDER BY FirstName, LastName
FOR XML RAW;

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-5

Querying XML Data in SQL Server 2012

When executed, this returns the following:

MCT USE ONLY. STUDENT USE PROHIBITED

18-6

Note that one XML element is returned for each row from the rowset, the element has a generic name of
row and all columns are returned as attributes. The returned order is based on the ORDER BY clause.
In the example on the slide, you can see how to override the generic element name. In that example, the
elements have been named Order instead.

In addition, notice that the results have been returned as an XML fragment rather than an XML document.
This is because there is no root element. Also, in the example on the slide, you can see how to
automatically add a root element called Orders.

Element-centric XML

You will notice that in the previous examples that the columns from the rowset have been returned as
attributes. This is referred to as attribute-centric XML.

You can modify this behavior to produce element-centric XML by adding the keyword ELEMENTS to the
FOR XML clause. You can see this in the following query:
SELECT FirstName, LastName, PersonType
FROM Person.Person
ORDER BY FirstName, LastName
FOR XML RAW, ELEMENTS;

When executed, this query returns the following output:

Note that each column has been returned as a sub-element of the row element.

Using AUTO Mode Queries

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-7

With AUTO mode, by default, each row in the result set is represented as an XML element named after the
table (or alias) it was selected from. AUTO mode generates nesting in the resulting XML based on the way
the SELECT statement is specified. You have minimal control over the shape of the XML generated. AUTO
mode queries are more capable of dealing with nested XML.

FOR XML AUTO

AUTO mode queries are useful if you want to generate simple hierarchies but they provide limited control
of the resultant XML. If you need more control over the resultant XML than is provided by AUTO mode
queries, you will need to consider using PATH or EXPLICIT modes instead.
Look at the following query (which is a modified version of the query that you saw in the last topic):
SELECT FirstName, LastName, PersonType
FROM Person.Person
ORDER BY FirstName, LastName
FOR XML AUTO;

Each table in the FROM clause, from which at least one column is listed in the SELECT clause, is
represented as an XML element. The columns listed in the SELECT clause are mapped to attributes or
subelements, if the optional ELEMENTS option is specified in the FOR XML clause. You can see the output
of this query below:

Querying XML Data in SQL Server 2012

MCT USE ONLY. STUDENT USE PROHIBITED

18-8

Note how the name of the table is directly used as the element name. For this reason, it is common to
provide an alias for the table as shown in the following code:
SELECT FirstName, LastName, PersonType
FROM Person.Person AS Person
ORDER BY FirstName, LastName
FOR XML AUTO;

When executed, it provides the following output:

Note that in the example in the slide, the nesting of the resultant XML is based upon the ORDER BY
clause, and not based on any form of grouping statement.

NULL Columns
Look at the following query:
SELECT ProductID, Name, Color
FROM Production.Product AS Product
ORDER BY ProductID
FOR XML AUTO;

When executed, it produces the following output:

Note that a number of products do not have any color. In the resultant XML, NULL values are not
returned as zero length strings, by default they are omitted from the results. While in general this is
appropriate, it can cause a specific problem when an XML schema is being derived from an XML
document. For example, if someone sent you an XML document with product details, if none of the
products happened to have a color, you would assume that there was no color column.

XSINIL

To assist in situations where a schema needs to be derived from a document that contains nullable
columns, SQL Server provides an additional option XSINIL that adds an element to the output to indicate
that an element exists but that it is currently NULL. Look at the following code:
SELECT ProductID, Name, Color
FROM Production.Product AS Product
ORDER BY ProductID
FOR XML AUTO, ELEMENTS XSINIL;

When executed, it returns the following results:

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-9

Note the difference between the rows that have no color and the rows that do have a color. XSINIL can
also be used in other modes such as PATH and RAW.

Querying XML Data in SQL Server 2012

Using EXPLICIT Mode Queries

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

18-10

EXPLICIT mode gives you the greatest control over the resulting XML but at the price of query complexity.
Many common queries that required EXPLICIT mode in SQL Server 2000 can be implemented using PATH
mode (which was introduced in SQL Server 2005 and will be explained later).

FOR XML EXPLICIT

EXPLICIT mode queries define XML fragments as a universal table, which consists of a column for each
piece of data that you require, and two additional columns. The additional columns are used to define the
metadata for the XML fragment. The Tag column uniquely identifies the XML tag that will be used to
represent each row in the results, and the Parent column is used to control the nesting of elements. Each
row of data in the universal table represents an element in the resulting XML document.

The power of EXPLICIT mode is to mix attributes and elements at will, create wrappers and nested
complex properties, create space-separated values (for example, OrderID attribute may have a list of order
ID values), and mixed contents.
PATH mode, together with the nesting of FOR XML queries and the TYPE clause, gives enough power to
replace most of the EXPLICIT mode queries in a simpler, more maintainable way.
EXPLICIT mode is rarely needed now and is complicated to write queries for.

Using PATH Mode Queries

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-11

PATH mode provides a simpler way to mix elements and attributes. It can be used in many situations as
an easier way to write query than those using EXPLICIT mode.

FOR XML PATH


PATH mode is a simpler way to introduce additional nesting for representing complex properties.

In PATH mode, column names or column aliases are treated as XML Path Language (XPath) expressions.
(More detail on XPath will be provided later in this module.) These expressions indicate how the values are
being mapped to XML. Each XPath expression is a relative XPath that provides the item type, such as the
attribute, element, and scalar value, and the name and hierarchy of the node that will be generated
relative to the row element.
The slide provides an example of an XML PATH query. Note that the path to e.BusinessEntityID is shown
as @EmpID. Values starting with an @ symbol in XPath refer to attributes. You can see in the output
that the e.BusinessEntityID value has been returned as the EmpID attribute of the row element.
The next two columns listed in the example on the slide detail the path to the values. For example, the
p.FirstName path is shown as EmpName/First. This indicates that the p.FirstName value should be
output as an element named First that is a sub-element of an element named EmpName, which is
itself returned as a sub-element of the row element.
You can use FOR XML EXPLICIT mode queries to construct such XML from a rowset, but PATH mode
provides a simpler alternative to the potentially time consuming EXPLICIT mode queries.

PATH mode, together with the ability to write nested FOR XML queries and the TYPE directive to return
xml data type instances, allows you to write queries with less complexity and gives enough power to
replace most of the EXPLICIT mode queries in a simpler, more maintainable way.
Question: What does an @ symbol relate to in an XQuery?

Querying XML Data in SQL Server 2012

Retrieving Nested XML

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

18-12

The TYPE keyword can be used to return FOR XML sub-queries as xml data type rather than as nvarchar
data type.

TYPE Keyword

In the previous topics in this lesson, you have seen how FOR XML AUTO queries can return attribute or
element centric XML. If this data is returned from a subquery, it needs to be returned as a specific data
type.

The FOR XML clause was introduced in SQL Server 2000. That version of SQL Server did not have an xml
data type. For that reason, sub-queries with FOR XML clauses had no way to return xml. FOR XML
subqueries in SQL Server 2000 returned nvarchar data type instead.

SQL Server 2005 introduced the xml data type but for backward compatibility, the data type for return
values from FOR XML subqueries was not changed to xml. However, a new keyword TYPE was introduced
that changes the return data type of FOR XML sub-queries to xml.
For example, look at the following query:
SELECT Customer.CustomerID, Customer.TerritoryID,
(SELECT SalesOrderID, [Status]
FROM Sales.SalesOrderHeader AS soh
WHERE Customer.CustomerID = soh.CustomerID
FOR XML AUTO) as Orders
FROM Sales.Customer as Customer
WHERE EXISTS(SELECT 1 FROM Sales.SalesOrderHeader AS soh
WHERE soh.CustomerID = Customer.CustomerID)
ORDER BY Customer.CustomerID;

When executed in SQL Server Management Studio, this query returns:

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-13

Note that the Orders column is not hyperlinked. XML data type columns are hyperlinked in SQL Server
Management Studio. Now look at the following modified query:
SELECT Customer.CustomerID, Customer.TerritoryID,
(SELECT SalesOrderID, [Status]
FROM Sales.SalesOrderHeader AS soh
WHERE Customer.CustomerID = soh.CustomerID
FOR XML AUTO, TYPE) as Orders
FROM Sales.Customer as Customer
WHERE EXISTS(SELECT 1 FROM Sales.SalesOrderHeader AS soh
WHERE soh.CustomerID = Customer.CustomerID)
ORDER BY Customer.CustomerID;

When executed in SQL Server Management Studio, this returns the following results:

Note that the Orders column now is hyperlinked like other xml data type columns. The example on the
slide is a common use case where elements and attributes are mixed to define hierarchies in FOR XML.
When you click on one of the orders, you can see it in the XML editor in SQL Server Management Studio:

Question: Why is hyperlinking of XML columns in SSMS useful?

Querying XML Data in SQL Server 2012

Demonstration 1A: Using FOR XML Queries

Demonstration Steps

MCT USE ONLY. STUDENT USE PROHIBITED

18-14

1.

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click SQL
Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to D:\10776A_Labs\10776A_18_PRJ\10776A_18_PRJ.ssmssln and click Open.

3.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file from
within Solution Explorer.

4.

Open the 11 Demonstration 1A.sql script file.

5.

Follow the instructions contained within the comments of the script file.
Question: When should you use EXPLICIT mode in an XQuery?

Lesson 2

Getting Started with XQuery

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-15

In Lesson 1, you learned how to query relational data and return it as XML. Sometimes, however, the data
is already in XML and you may need to query it directly. You might want to extract part of the XML into
another XML document; you might want to retrieve the value of an element or attribute; you might want
to check if an element or attribute exists and finally you might want to directly modify the XML. These
tasks are provided for by XQuery methods.

Objectives
After completing this lesson, you will be able to:

Explain the role of XQuery

Use the Query Method

Use the Value Method

Use the Exist Method

Use the Modify Method

Querying XML Data in SQL Server 2012

What Is XQuery?

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

18-16

XQuery is a query language that is designed to query XML documents. It also includes elements of other
programming languages such as looping constructs.

XQuery
XQuery was developed by a working group within the Worldwide Web Consortium (W3C). It was
developed in conjunction with other work in the W3C, in particular the definition of Extensible Style
Sheets (XSLT). XSLT makes use of a subset of XQuery known as XPath.
XPath is the syntax used to provide an address for specific attributes and elements within an XML
document. You saw basic examples of this when looking at FOR XML PATH mode queries in the
last lesson.
Look at the following XPath expression:
/InvoiceList/Invoice[@InvoiceNo=1000]

This specifies a need to traverse the InvoiceList node (that is the root element as the expression starts with
a slash /), then to the Invoice sub-elements (note that there may be more than one of these) and then to
access the InvoiceNo attribute. All invoices with invoice number 1000 are then returned.
Note that while you may realize that there is unlikely to be more than one invoice with the number 1000,
nothing about XML syntax (without a schema) enforces this. One thing that can be hard to get used to
with the XPath syntax is that you constantly need to specify that you want the first entry of a particular
type, even though logically you may think that it should be obvious that there would only be one. You
indicate the first entry in a list by the expression [1].
In XPath, attributes are indicated by the @ prefix. The content of the element itself is referred to by the
token text().

FLWOR Expressions

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-17

As well as basic path traversal, XPath supports an iterative expression language known as FLWOR and
commonly pronounced as though it was spelled flower. FLWOR stands for for, let, where, order, and
return which are the basic operations in a FLWOR query. An example of a FLWOR expression is shown in
the following XQuery query() method:
SELECT @xmlDoc.query('<OrderedItems>
{
for $i in /InvoiceList/Invoice/Items/Item
return $i
}
</OrderedItems>');

This query outputs OrderedItems as an element and then within that element, it locates all items on all
invoices contained in the XML document and outputs them as sub-elements of the OrderedItems
element. An example of the output from this query is shown below:

Note that becoming proficient at XQuery is an advanced topic beyond the scope of this course. The aim
of this lesson is to make you basically aware of what is possible when using XQuery methods. The
available XQuery methods are shown in the following table:

Method

Function

query()

Returns selected XML data

value()

Retrieves a specific value from an element or attribute

exist()

Checks for the existence of an element or attribute

modify()

Performs insert, replace or delete operations

nodes()

Shreds XML data to relational format

The nodes() method will be covered in the next lesson that discusses shredding XML to relational data.
Question: Why do you think it is important to learn XPath query language?

Querying XML Data in SQL Server 2012

query() Method

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

18-18

The query() method is used to extract XML from an existing XML document. The output XML can be a
subset of the original XML document or entirely new XML can be generated based on the values
contained in the original XML document.

query() Method

The query() method is used to return untyped XML. It takes an XQuery expression that evaluates to a list
of XML nodes and allows the users to create output XML based in some way on the fragments it extracts
from the input XML.
An XQuery expression in SQL Server consists of two sections a prolog and a body. The prolog can
contain a namespace declaration. You will see how to do this later in this module.
The body of an XQuery expression contains query expressions that define the result of the query. It is
important to understand that both the input and output of a query() method are XML.
Note that if NULL is passed to a query() method, the result returned by the method is also NULL.

Example of query() Method


Look at the query shown in the slide:
SELECT XmlEvent.query(
'<EventSPIDs>
{
for $e in /EVENT_INSTANCE
return <SPID>
{number($e/SPID[1])}
</SPID>
}
</EventSPIDs>')
FROM dbo.DatabaseLog;

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-19

This query tells SQL Server to return one xml value for each row in the dbo.DatabaseLog table. The xml
value returned for each row will have a root element called EventSPIDs. For each EVENT_INSTANCE node
that is contained in the XmlEvent column within each row, a sub-element named SPID should be returned.
The contents of that node will be the value of the first SPID sub-element of the EVENT_INSTANCE node
returned as a number.
Sample output from the query is shown below:

You will see how this works in the following demonstration.

Querying XML Data in SQL Server 2012

value() Method

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

18-20

The value() method is useful for extracting scalar values from XML documents as a relational value. This
method takes an XQuery expression that identifies a single node and the desired SQL type to be returned.
The value of the XML node is returned cast to the specified SQL type.

value() Method

The purpose of the value() method shown in the slide example is to navigate to the ProductModelID
attributes of the ProductDescription element. Note the use of the expression [1] to specify that the first of
these is retrieved and returned as an integer with the alias Result.
Do not be too concerned with the namespace declaration in the example shown in the slide. It is only
specified as the examples in the AdventureWorks database require this.

Example Output
You can see the output from this query below:

Note that as with the query() method, if NULL is passed to the value() method, NULL will be returned.

exist() Method

Key Points
Use the exist() method to check for the existence of a specified value.

exist() Method

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-21

The exist() method allows the user to perform checks on XML documents to determine if the result of an
XQuery expression is empty or nonempty. The result of this method is:

1 if the XQuery expression returns a nonempty result

0 if the result is empty, and

NULL if the XML instance itself is NULL

For better performance, use the exist() method on the XML data type, whenever possible, instead of the
value() method. The exist() method is most helpful when used in a SQL WHERE clause and utilizes XML
indexes more effectively than the value() method.

Example Output

Querying XML Data in SQL Server 2012

MCT USE ONLY. STUDENT USE PROHIBITED

18-22

This is a good example of the power of XML methods when used by database administrators. A database
log has been used to hold details of all schema changes made to a database. The example query asks SQL
Server to return the rows where the type of object affected was a table. You will notice in the output that
all events listed are table-related.
Question: Why would the exist() method outperform the value() method?

modify() Method

Key Points
Data manipulation operations can be performed on an XML instance using the modify() method. The
modify() method changes the contents of an XML document.

modify() Method

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-23

You can use the modify() method to alter the content of an xml type variable or column. This method
takes an XML data manipulation language (DML) statement to insert, update, or delete nodes from the
XML data. The modify() method of the xml data type can only be used in the SET clause of an UPDATE
statement. One or more nodes can be inserted, deleted, and updated using the insert, delete, and
replace value of keywords, respectively.
Note that unlike the previous methods, an error is returned if NULL is passed to the modify() method.

Supported Actions
The modify() method supports these keywords:

Insert adds child nodes to an XML document

Replace updates a node in an XML document

Delete removes a node from the XML document

Querying XML Data in SQL Server 2012

Slide Example

MCT USE ONLY. STUDENT USE PROHIBITED

18-24

In the insert example shown in the slide, a new SalesPerson node with the text() of Bill is inserted into the
first position of the first invoice in the list of invoices.

In the replace example shown in the slide, the SalesPersons name is replaced by Ted.

In the delete example shown in the slide, the SalesPerson sub-element would be removed from the
InvoiceList/Invoice path.

Demonstration 2A: Using XQuery Methods in a DDL Trigger

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-25

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_18_PRJ\10776A_18_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

2.

Open the 21 Demonstration 2A.sql script file.

3.

Follow the instructions contained within the comments of the script file.

Question: Suggest an example of where the ability to create triggers on DDL statements would be
useful.

Querying XML Data in SQL Server 2012

Lesson 3

Shredding XML

MCT USE ONLY. STUDENT USE PROHIBITED

18-26

Another common need that can arise when working with XML data in SQL Server is to be able to extract
relational data from within an XML document.

For example, you might receive a purchase order from a customer in XML format. You need to parse the
XML to retrieve the details of the items that you need to supply. The extraction of relational data from
within XML documents is referred to as shredding the XML documents.
There are two basic ways to do this. SQL Server 2000 supported the creation of an in-memory tree that
could then be queried with an OPENXML function.

While that is still supported, SQL Server 2005 introduced the XQuery nodes() method which in many cases
will be an easier way to shred XML data.
Besides covering these in this module, you will also see how T-SQL provides a way of simplifying how
namespaces are referred to in queries.

Objectives
After completing this lesson, you will be able to:

Describe how to shred XML data

Use system stored procedures for creating and managing in-memory node trees extracted from
XML documents

Use the OPENXML function

Work with XML namespaces

Use the nodes() method

Overview of Shredding XML Data

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-27

The first of the methods for shredding XML data that was mentioned was to query an in-memory tree
that represents the XML. The system stored procedure sp_xml_preparedocument can be used to create an
in-memory node tree from an XML document that will make querying the XML data faster. This allows
you to obtain relational data from within the XML document.

Shredding XML
The process for shredding XML is:
1.

An XML document is received from a client application.

2.

By calling sp_xml_preparedocument, an in-memory node tree gets created, based on the input XML.

3.

The OPENXML table-valued function is then used to query the in-memory node tree and extra
relational data.

4.

The relational data that has been extracted is normally combined with other relational data as part of
standard T-SQL queries.

5.

Calling sp_xml_removedocument removes the node tree from memory.

The steps in this process will be explored in the following topics.

Querying XML Data in SQL Server 2012

Stored Procedures for Managing In-Memory Node Trees

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

18-28

Before you can use the OPENXML functionality to navigate over ML documents, you need to create an inmemory node tree. This is done via the sp_xml_preparedocument system stored procedure.

sp_xml_preparedocument

sp_xml_preparedocument is a system stored procedure that takes XML either as the untyped xml data
type or as xml stored in the nvarchar data type, creates an in-memory node tree from the XML (to make it
easier to navigate), and returns a handle to that node tree.
sp_xml_preparedocument reads the XML text provided as input, parses the text by using the Microsoft
XML Core Services (MSXML) parser (Msxmlsql.dll), and provides the parsed document in a state ready for
consumption. This parsed document is a tree representation of the various nodes in the XML document:
elements, attributes, text, comments, and so on.
Before calling sp_xml_preparedocument, you need to declare an integer variable to be passed as an
output parameter to the procedure call. When the call returns, the variable will then be holding a handle
to the node-tree.
It is important to realize that the node tree must stay available and unmoved in visible memory because
the handle is basically a pointer that needs to remain valid. This means that on 32 bit systems, the node
tree will not be able to be stored in AWE memory.

sp_xml_removedocument

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-29

sp_xml_removedocument is a system stored procedure that frees the memory occupied by a node tree
and invalidates the handle.
In SQL Server 2000, sp_xml_preparedocument created a node tree that was session-scoped, that is, the
node tree remained in memory until the session ended or until sp_xml_removedocument was called.
A common coding error was to forget to call sp_xml_removedocument. Allowing too many node trees
to remain in memory was known to cause severe lack of available low-address memory on 32-bit
systems. Because of this, a change was made in SQL Server 2005 that made the node trees created by
sp_xml_preparedocument to become batch-scoped rather than session-scoped. Even though the
tree will be removed at the end of the batch, it is considered good practice to explicitly call
sp_xml_removedocument to minimize the use of low-address memory as much as possible.
Note that 64-bit systems generally do not have the same memory limitations anyway.

Querying XML Data in SQL Server 2012

OPENXML

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

18-30

OPENXML provides a rowset over in-memory XML documents, which is similar to a table or a view.
OPENXML allows access to the XML data as though it is a relational rowset. It does this by providing a
rowset view of the internal representation of an XML document.

OPENXML

After you have created an in-memory node tree of an XML document using sp_xml_preparedocument,
you can use OPENXML to write queries against the document. For example, you might need to extract a
list of products that you need to supply to a customer from an XML-based order that the customer sent
to you. OPENXML provides a rowset view of the document, based on the parameters passed to it.

The parameters passed to OPENXML are the XML document handle, a rowpattern, which is an XPath
expression that maps the nodes of XML data to rows, and an indication of whether or not to use attributes
by default rather than elements. Associated with the OPENXML clause is a WITH clause that provides a
mapping between the rowset columns and the XML nodes.
The ColPattern shown is an optional, generic XPath pattern that describes how the XML nodes should be
mapped to the columns. If ColPattern is not specified, the default mapping (attribute-centric or elementcentric mapping as specified by flags) takes place.

Working with XML Namespaces

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-31

In the previous module, you saw how an XML namespace is a collection of names that can be used as
element or attribute names in an XML document. The namespace qualifies names uniquely to avoid
naming conflicts with other elements with the same name.

Working with XML Namespaces

When calling sp_xml_preparedocument, if you are working with XML that contains embedded
namespaces, you can specify an optional parameter that specifies the namespaces used. You can then use
the namespace prefixes in the XPath expressions used within OPENXML.
In the example on the slide, note how the alias o has been assigned to the XML namespace
urn:AW_OrderNS. That alias is then used throughout the document when an element that is defined in
that namespace is used.

Querying XML Data in SQL Server 2012

nodes() Method

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

18-32

The nodes() method provides a much easier way to shred XML into relational data than OPENXML and its
associated system stored procedures.

nodes() Method

The nodes() method is an XQuery method and is useful when you want to shred an xml data type instance
into relational data. It is a table-valued function that allows you to identify nodes that will be mapped into
a new row.
Every xml data type instance has an implicitly provided context node. For the XML instance stored in a
column or a variable, this is the document node. The document node is the implicit node at the top of
every xml data type instance.

The result of the nodes() method is a rowset that contains logical copies of the original XML instances. In
these logical copies, the context node of every row instance is set to one of the nodes identified with the
query expression. This enables subsequent queries to navigate relative to these context nodes.

It is important to be careful about the query plans generated when using the nodes() method. In
particular, no cardinality estimates are available when using this method. This has the potential to lead to
poor query plans. In some cases, the cardinality is simply estimated to be a fixed value of 10,000 rows. This
might cause an inappropriate query plan to be generated if your XML document contained only a handful
of nodes.

CROSS APPLY and Table-valued Function


The nodes() method is a table-valued function that is normally called via the CROSS APPLY or OUTER
APPLY operations.
APPLY operations cause table-valued functions to be called for each row in the left-hand table of the
query. Look at the query from the example in the slide:
SELECT EventDetail.value('PostTime[1]','datetime2') AS PostTime,
EventDetail.value('SPID[1]', 'int') AS SPID,
EventDetail.value('ObjectType[1]','sysname') AS ObjectType,
EventDetail.value('ObjectName[1]','sysname') AS ObjectName
FROM dbo.DatabaseLog AS dl
CROSS APPLY dl.XmlEvent.nodes('/EVENT_INSTANCE')
AS EventInfo(EventDetail)
ORDER BY PostTime;

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-33

In this query, for every row in the dbo.DatabaseLog table, the nodes() method is called on the XmlEvent
column from the dbo.DatabaseLog table. When table-valued functions are used in queries like this, an
alias must be provided for both the derived table and the columns it contains. In this case, the alias
provided to the derived table is EventInfo and the alias provided to the extracted column is EventDetail.

One output row is being returned for each node at the level of the XPath expression /EVENT_INSTANCE.
From the returned XML column (EventDetail), a series of columns is generated by calling the value()
method. Note that it is called four times for each output row in this example. Also note that the path to
the value to be returned and the data type of that value are being specified along with output column
aliases.
When executed, the above query returns the following output:

Question: When would it make sense to use OPENXML rather than the nodes() method for shredding
XML data to relational format?

Querying XML Data in SQL Server 2012

Demonstration 3A: Shredding XML

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

18-34

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_18_PRJ\10776A_18_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

2.

Open the 31 Demonstration 3A.sql script file.

3.

Follow the instructions contained within the comments of the script file.

Lab 18: Querying XML Data in SQL Server

Lab Setup

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-35

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_18_PRJ\10776A_18_PRJ.ssmssln.

7.

From the View menu, click Solution Explorer. In Solution Explorer, double-click the query
00-Setup.sql. When the query window opens, click Execute on the toolbar.

Lab Scenario

In this lab, you will investigate several ways in which XML data can be used in SQL Server. You will query
relational data and return it as XML and also process existing XML data using T-SQL.

If you have time, your manager has an additional task for you. A new web service is being added to the
marketing system. You need to create a stored procedure that will query data from a table and return it as
an XML value.

Querying XML Data in SQL Server 2012

Supporting Documentation
Stored Procedure Specifications

MCT USE ONLY. STUDENT USE PROHIBITED

18-36

Stored Procedure

WebStock.GetAvailableModelsAsXML

Input Parameters:

None

Output Parameters:

None

Returned Rows:

One XML document with attribute-centric XML.


Root element is AvailableModels.
Row element is AvailableModel.
Row contains ProductID, ProductName, ListPrice, Color and SellStartDate
(from Marketing.Product) and ProductModelID and ProductModel (from
Marketing.ProductModel) for rows where there is a SellStartDate but not
yet a SellEndDate.

Output Order:

Rows within the XML should be in order of SellStartDate ascending and


then ProductName ascending. That is, sort by SellStartDate first and then
ProductName within SellStartDate.

Stored Procedure: Marketing.UpdateSalesTerritoriesByXML


Stored Procedure

Marketing.UpdateSalesTerritoriesByXML

Input Parameters:

@SalespersonMods xml

Output Parameters:

None

Returned Rows:

None

Actions:

Update the SalesTerritoryID column in the Marketing.Salesperson table


based upon the SalesTerritoryID values extracted from the input parameter.

Incoming XML Object Format:


<SalespersonMods>
<SalespersonMod SalespersonID="274">
<Mods>
<Mod SalesTerritoryID="3"/>
</Mods>
</SalespersonMod>
<SalespersonMod SalespersonID="278">
<Mods>
<Mod SalesTerritoryID="4"/>
</Mods>
</SalespersonMod>
</SalespersonMods>

Exercise 1: Learn to Query SQL Server Data As XML


Scenario

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-37

In this exercise, you have decided to learn to query SQL Server data to return XML. You will review and
execute scripts that demonstrate the most important FOR XML querying techniques.
The main tasks for this exercise are as follows:

Review and execute the query and review the results.

f Task 1: Review and execute the query and review the results

Review the query, execute the query and review the results for scripts 18.1 to 18.9.

Results: After this exercise, you have executed queries that return SQL Server relational data as XML.

Exercise 2: Write a stored procedure returning XML


Scenario

In this exercise, a new web service is being added to the marketing system. You need to create a stored
procedure that will query data from a table and return it as an XML value.
The main tasks for this exercise are as follows:
1.

Review the requirements.

2.

Create the stored procedure.

3.

Test the stored procedure.

f Task 1: Review the requirements

Review the supporting documentation for details of the required stored procedure
WebStock.GetAvailableModelsAsXML.

f Task 2: Create the stored procedure

Create and implement the stored procedure based on the specifications provided.

f Task 3: Test the stored procedure

Test the stored procedure by executing the following code:


EXEC WebStock.GetAvailableModelsAsXML;

Results: After this exercise, you should have created and tested the required stored procedure that
returns XML.

Querying XML Data in SQL Server 2012

MCT USE ONLY. STUDENT USE PROHIBITED

18-38

Challenge Exercise 3: Write a Stored Procedure That Updates Using XML


(Only if time permits)
Scenario

In this exercise, the reassignment of salesperson territories is done by an external management system.
When it completes reassignments, it sends an XML document containing the changes. You need to
construct a stored procedure Marketing.UpdateSalesTerritoriesByXML that can process this XML and use it
to update the SalesTerritoryID column in the Marketing.Salesperson table.
The main tasks for this exercise are as follows:
1.

Review the requirements.

2.

Create the stored procedure.

3.

Test the stored procedure and ensure the updates have been applied.

f Task 1: Review the requirements

Review the supporting documentation for details of the required stored procedure
Marketing.UpdateSalesTerritoriesByXML.

f Task 2: Create the stored procedure

Create and implement the stored procedure based on the specifications given in the supporting
documentation for the lab.

f Task 3: Test the stored procedure

Test the stored procedure by executing the following code:


DECLARE @SalespersonMods xml;
SET @SalespersonMods = '<SalespersonMods>
<SalespersonMod SalespersonID="274">
<Mods>
<Mod SalesTerritoryID="3"/>
</Mods>
</SalespersonMod>
<SalespersonMod SalespersonID="278">
<Mods>
<Mod SalesTerritoryID="4"/>
</Mods>
</SalespersonMod>
</SalespersonMods>';
EXEC Marketing.UpdateSalesTerritoriesByXML @SalespersonMods;
GO
SELECT * FROM Marketing.Salesperson;
GO

Results: After this exercise, you should have created and tested the required stored procedure that
updates XML.

Module Review and Takeaways

Review Questions
1.

What are AUTO mode queries?

2.

What are PATH mode queries?

3.

What does the nodes() method do?

4.

What are RAW mode queries?

Best Practices

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

18-39

1.

Convert existing code that uses the nvarchar data type for XML parameters to use the XML data type.

2.

Provide meaningful row names when using RAW mode by using the optional name parameter to the
RAW clause.

3.

Check the query plans for queries using the nodes() method to ensure that the lack of cardinality
estimates is not producing a poor execution plan.

MCT USE ONLY. STUDENT USE PROHIBITED

MCT USE ONLY. STUDENT USE PROHIBITED


19-1

Module 19
Working with SQL Server 2012 Spatial Data
Contents:
Lesson 1: Introduction to Spatial Data

19-3

Lesson 2: Working with SQL Server Spatial Data Types

19-12

Lesson 3: Using Spatial Data in Applications

19-25

Lab 19: Working with SQL Server Spatial Data

19-35

Working with SQL Server 2012 Spatial Data

Module Overview

MCT USE ONLY. STUDENT USE PROHIBITED

19-2

Business applications routinely deal with addresses and locations, yet they rarely provide effective ways to
process distances and proximity. Spatial data in Microsoft SQL Server allows for effective storage and
processing of locations, addresses, and shapes. This capability can help business make better decisions
and can also be used to help visualize results, which often makes results easier to interpret.

Objectives
After completing this module, you will be able to:

Describe the importance of spatial data and the industry standards related to it

Explain how to store spatial data in SQL Server

Perform calculations on and query SQL Server spatial data

Lesson 1

Introduction to Spatial Data

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-3

Before starting to work with spatial data, it is important to gain an understanding of where it is typically
used in applications, along with an understanding of the different types of spatial data. Most business
applications need to work with addresses or locations. SQL Server can process both planar and geodetic
data. It is important to understand the difference between these two types of data as well as how the SQL
Server data types relate to the relevant industry standards and measurement systems.

Objectives
After completing this lesson, you will be able to:

Explain how spatial data is useful in a wide variety of business applications

Describe the different types of spatial data

Describe the difference between planar and geodetic data types

Explain the relationship between the SQL Server spatial data support and the industry standards

Work with spatial reference identifiers to provide measurement systems

Working with SQL Server 2012 Spatial Data

Target Applications

Key Points
There is a perception that spatial data is not useful in mainstream applications. This is an invalid
perception as almost every business application can benefit from the use of spatial data.

Business Applications

MCT USE ONLY. STUDENT USE PROHIBITED

19-4

While mapping provides an interesting visualization in some cases, business applications can make good
use of spatial data for much more routine tasks.
Almost all business applications involve storing addresses or locations. Customers or clients have street
addresses, mailing addresses, and delivery addresses. The same is true for stores, offices, suppliers, and
many other business-related entities.

Business Intelligence Applications

A particularly strong use of spatial data comes when it is combined with Business Intelligence applications.
These applications often deal with results that are best visualized rather than being presented as tables of
numbers. Spatial capabilities make it possible to provide very rich forms of visualization.

Common Business Questions

Consider a pet accessories supply company. They have stores all over the country. They know where their stores
are and they know where their customers live. The owner has a feeling though that the companys customers
are not buying from their nearest store but the owner also has no firm facts to base this feeling on.
It could be true that customers really do purchase from their local store. The owner may have just come
across a small sample of data and been misled by it.

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-5

It could also be true that customers dont purchase from their local stores. If so, it might be interesting to
know what they purchase when they travel to another store. Perhaps the local store doesnt hold a wide
enough variety of stock. Perhaps instead, the customers purchase everything they need from a more
remote store as they dont like the staff at the local store. A situation might also exist where two stores
are cannibalizing each others business. The data might also be used to find new locations for stores.
It is important to realize though that these sorts of questions are normal business questions, not
specialized mapping questions. This is the sort of problem you can solve quite easily if you are able to
process spatial data in a database.

Working with SQL Server 2012 Spatial Data

Types of Spatial Data

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

19-6

In the spatial data community, several types of spatial data are used. SQL Server works with vector-based
2D data but has some storage options for 3D values.

Vector vs Raster Data

Spatial data could be stored as either a series of line segments that together form an overall shape (vector
storage) or as a series of dots or pixels that are formed by dividing a shape into smaller pieces (raster
storage). Vector storage is the method that SQL Server spatial data is based upon. One key advantage of
vector-based storage is the way that it can scale. Imagine storing the details of a line. You could divide the
line into a series of dots that make up the line. However, if you then zoom in into an image of the line, the
individual dots would then become visible, along with the gaps between the dots. This is how rasterbased storage works. Alternately, if the line was stored as the coordinates of the start and end points of
the line, no matter how much you zoom in or out, the line would still look complete. This is because it
would effectively be redrawn at each level of magnification. This is how vector-based storage works.

2D, 3D, 4D

You are likely familiar with seeing drawings or maps on paper that are two dimensional in nature. A third
dimension would represent the elevation of a point on the map. 4D systems usually incorporate changes
in a shape over time.
SQL Server spatial data is currently based on the 2D technology. In some of the objects and properties it
provides, SQL Server spatial data supports storing and retrieving of 3D and 4D values but it is important
to realize that the third and fourth dimensions ignored during calculations. This means that if you
calculate the distance between say a point and a building, the calculated distance is the same regardless
of which floor or level in the building the point is located.

Question: Which existing SQL Server data type could be used to store (but not directly process) raster
data?

Planar vs. Geodetic

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-7

Planar systems represent the Earth as a flat surface. Geodetic systems represent the Earth more like its
actual shape.

Planar Systems

Prior to the advent of computer systems, it was very difficult to perform calculations on round models of
the Earth. For convenience, mapping tended to be two dimensional in nature. Most people are familiar
with traditional flat maps of the world.

As soon as larger distances are involved, however, flat maps provide a significant distortion, particularly as
you move from the center of the map. When most of the standard maps from atlases were first drawn,
they also were oriented around where the people drawing them lived. That way, the least distortion
occurred where the people that were using the maps were based.
As an example, in the flat map shown, it isnt obvious how Africas area (about 30 million square
kilometers) compares to North Americas area (about 24 million square kilometers). Also, compare
Antarcticas size on the map and note that it is really only about 13 million square kilometers in size.

Geodetic Systems
Geodetic systems represent the Earth in a round shape. Some systems use simple spheres but it is
important to realize that the Earth is not actually spherical.

SQL Server spatial data offers a number of systems for representing the shape of the Earth. Most systems
model the Earth as an ellipsoid rather than as a sphere.
Question: What is the difference between an ellipsoid and a sphere?

Working with SQL Server 2012 Spatial Data

OGC Object Hierarchy

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

19-8

The Open Geospatial Consortium (OGC) is the industry body that provides specifications for how spatial
data processing should occur in SQL-based systems.

SQL Specification

One of the two data types that SQL Server provides is the geometry data type. It conforms to the
OGC Simple Features for SQL Specification 1.0 and is used for planar spatial data. Besides defining how
the data should be stored, the specification details common properties and methods to be applied to
the data.

The OGC defines a series of data types that form an object tree. In the chart shown in the slide, the
objects that are supported and can be created in SQL Server spatial data are shown in blue (or the darker
color). Other objects in the OGC Geometry hierarchy are shown in yellow (or the lighter color). Curved Arc
support was added in SQL Server 2012. It is likely that future versions of SQL Server will expand this
coverage of OGC objects.

Extensions

SQL Server also extends the standards in a number of ways. A round-Earth data type called geography has
also been provided, along with a number of additional useful properties and methods.
Methods and properties that are related to the OGC standard have been defined with an ST prefix (such as
STDistance) and those without an ST prefix are Microsoft extensions to the standard (such as MakeValid).

Spatial Reference Identifiers

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-9

Many systems of measurement have existed over time. SQL Server supports many of these measurement
systems directly. When you specify a spatial data type in SQL Server, you also specify the measurement
system to be used. You specify this by associating a spatial reference ID with the data. A spatial reference
ID of zero indicates the lack of a measurement system. This is commonly used where there is no need for
a specific measurement system.

Spatial Reference Systems


Any model of the Earth is an approximation, some models closer to reality than others. SQL Server
supports many different Earth models via a series of Spatial Reference Identifiers (SRIDs).

Each SRID defines the shape of the Earth model, the authority responsible for maintaining it, the unit of
measure used, and a multiplier that determines how the unit of measure could be converted to meters.

SRID 4326

The World Geodetic System (WGS) is commonly used in cartography, geodetics, and navigation. The latest
standard is WGS 84 and is best known to most people via the Global Positioning System (GPS). GPS is
often used in navigation systems and uses WGS 84 as its coordinate system.
WGS 84 support is provided by SRID 4326 in SQL Server spatial data. If you query the list of SRIDs in SQL
Server, the entry for SRID 4326 has the following name (formally called the Well-known Text or WKT that
is associated with the ID):
GEOGCS["WGS 84", DATUM["World Geodetic System 1984", ELLIPSOID["WGS 84", 6378137,
298.257223563]], PRIMEM["Greenwich", 0], UNIT["Degree", 0.0174532925199433]]

Working with SQL Server 2012 Spatial Data

MCT USE ONLY. STUDENT USE PROHIBITED

19-10

This specifies how WGS 84 models the Earth as an ellipsoid (you can imagine it as a squashed ellipsoid),
with its major radius of 6378137 meters at the equator, a flattening of 1 / 298.257223563 (or about 21
kilometers) at the poles, a prime meridian (that is, a starting point for measurement) at Greenwich, and a
measurement based on degrees. The starting point at Greenwich is specifically based at the Royal
Observatory. The units are shown as degrees and the size of a degree is specified in the final value in the
definition.
Most geographic data today would be represented by SRID 4326.
Question: Do you currently use GPS data in any existing applications within your organization?

Demonstration 1A: Viewing Available Spatial Reference Systems

Demonstration Steps

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-11

1.

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click SQL
Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to D:\10776A_Labs\10776A_19_PRJ\10776A_19_PRJ.ssmssln and click Open.

3.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file from
within Solution Explorer.

4.

Open the 11 Demonstration 1A.sql script file.

5.

Follow the instructions contained within the comments of the script file.

Working with SQL Server 2012 Spatial Data

Lesson 2

Working with SQL Server Spatial Data Types

MCT USE ONLY. STUDENT USE PROHIBITED

19-12

SQL Server supports two spatial data types, geometry and geography, which have been created as system
CLR data types. It is important to know how to use each of these data types and how to interchange data
using industry standard formats.

Objectives
After completing this lesson, you will be able to:

Describe the support provided by SQL Server spatial data

Explain how system CLR types differ from user CLR types

Use the geometry data type

Use the geography data type

Work with standard spatial data formats

Use OGC methods and properties on spatial data

Use Microsoft extensions to the OGC standard when working with spatial data

SQL Server Spatial Data

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-13

SQL Server supplies rich support for spatial data. It provides two data types: the geometry data type that is
suited to flat Earth (planar) models, and the geography data type that is suited for round Earth (geodetic)
models.

geometry Data Type

The geometry data type is the SQL Server implementation of the OGC Geometry data type. It supports the
majority of the methods and properties of the OGC type plus extensions to the OGC type. The geometry
type is used when modeling flat Earth models such as two dimensional diagrams. The geometry data type
offers an X- and Y-based coordinate system.

geography Data Type

The geography data type is a Microsoft extension to the OGC standards that is suitable when working
with round Earth models such as GPS data. The geography data type works with a Long and Lat
(longitude and latitude) coordinate system.

Note that although latitude and longitude is a commonly-used phrase in the general community, the
geographical community uses the terminology in the reverse order. When specifying inputs for
geographic data in SQL Server, the longitude value precedes the latitude value.

Additional Support

The Bing Maps software development kit (SDK) has been updated to work closely with SQL Server spatial
data.

SQL Server Reporting Services 2012 includes a map control that can be used to render spatial data and a
wizard to help to configure the map control. The map control is available for reports built using BIDS and
for reports built using Report Builder.

Working with SQL Server 2012 Spatial Data

MCT USE ONLY. STUDENT USE PROHIBITED

19-14

An application that stores or retrieves spatial data from a SQL Server database needs to be able to
work with that data as a spatial data type. To make this possible, a separate installer file (MSI) has been
provided as part of the SQL Server 2012 Feature Pack to allow client applications to also make use of the
SQL Server spatial data types. The installer is called the Microsoft System CLR Types for SQL Server 2012.
This file can be installed on client systems and allows an application on the client to rehydrate a
geography object that has been read from a SQL Server database into a SqlGeography object within
.NET managed code.

ST Prefix

For the properties and methods that are implementations of the OGC standards, an ST prefix has been
added to the names of the properties and methods. For example, the X and Y coordinates of a geometry
object are provided by STX and STY properties and the Distance calculation is provided by the STDistance
method.
For Microsoft extensions to the OGC standards, no prefix has been added to the name of the methods or
properties. As an example, there is a MakeValid method. Care must also be taken when referring to
properties and methods as they are case sensitive, even on servers that are configured for case
insensitivity.
Question: You may have used a web service to calculate the coordinates of an address. What is this
process commonly called?

System vs. User SQL CLR Types

Key Points
The geometry and geography data types have been implemented as CLR types using managed code.
They are defined as system CLR types and work even when CLR integration is not enabled at the SQL
Server instance level.

System CLR Types

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-15

SQL Server 2005 introduced the concept of a CLR user-defined data type, for data types implemented in
managed code. CLR data types in SQL Server 2005 were limited in size. They needed to be capable of
being serialized into 8 KB of storage (they needed to fit in one SQL Server data page).
SQL Server 2008 introduced the concept of a system CLR data type, separate from the user-defined
data types but also implemented in managed code. SQL Server 2008 also replaced the 8 KB limit on
serialization with a 2 GB limit. This increased limit makes it possible to create complex data types using
managed code.

In SQL Server 2012, there are three system CLR data types that take advantage of this large data type
support: geometry, geography, and hierarchyid. Unlike user-defined CLR data types, these system data
types operate even when the clr enabled setting for the server instance is disabled.

You can see the currently installed assemblies and whether or not they are user-defined by executing the
following query:
SELECT name,
assembly_id,
permission_set_desc,
is_user_defined
FROM sys.assemblies;

Working with SQL Server 2012 Spatial Data

Accessing Properties and Methods

MCT USE ONLY. STUDENT USE PROHIBITED

19-16

You can access a property of an instance of a spatial data type by referring to it as Instance.Property. As
an example of this, look at the following code that is accessing the STX property of a variable called
@Location:
SELECT @Location.STX;

You can access a method of an instance of a spatial data type by referring to it as Instance.Method(). As
an example of this, look at the following code that is calling the MakeValid method of a variable called
@InputLocation:
SELECT @Location = @InputLocation.MakeValid();

It is also possible to call methods that are defined on the data types (geometry and geography) rather
than on instances (that is, columns or variables) of those types. This is an important distinction. As an
example of this, look at the following code that is calling the GeomFromText method of the geometry
data type:
SELECT @Location = geometry::STGeomFromText('POINT (12 15)',0);

Note that you are not calling the method on a column or variable of type geometry but on the geometry
type itself. In .NET terminology, this would be referred to as calling a public static method on the
geometry class.
Note also that the methods and properties of the spatial data types are case-sensitive, even on servers
configured with case-insensitive default collations.

geometry Data Type

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-17

The geometry data type is used for flat Earth (that is, planar) data storage and calculations. It provides
comprehensive coverage of the OGC standard.

geometry Data Type

The geometry data type is based on an X and Y coordinate system. It is a 2D data type. In the definition of
the type, there is provision for Z (elevation) and M (measure) as well as the X and Y coordinates. The
geometry data type allows the Z and M values to be entered and retrieved but it ignores these values
when performing calculations. You can see input and output of X, Y, Z, and M in the following code:
DECLARE @Location geometry;
SELECT @Location = geometry::STGeomFromText('POINT (12 15 2 9)',0);
SELECT @Location.STAsText();
SELECT @Location.AsTextZM();

When executed, the above code returns the following results:

Comprehensive coverage of the OGC Geometry data type is provided by the SQL Server geometry data
type. The X and Y coordinates are represented by STX and STY properties.

Working with SQL Server 2012 Spatial Data

SRID and geometry

MCT USE ONLY. STUDENT USE PROHIBITED

19-18

When working with geometric data, the measurement system is not directly relevant. For example, the
area of a shape which is 3 x 2 is still 6 regardless of whether 3 and 2 are in meters or in inches. For this
reason, there is no need to specify an SRID when working with geometric data. When inputting data, the
SRID value is typically left as zero.

Spatial Results Viewer

Whenever a SQL Server result set is being displayed in SQL Server Management Studio and the results
include columns of geometry or geography data type, a special spatial results viewer tab will also be
provided, to allow you to visualize the spatial results.

geography Data Type

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-19

The geography data type is used for round Earth values, typically involving actual positions or locations
on the Earth. It is an extension to the OGC standard.

geography Data Type

The geography data type is based on a latitude and longitude coordinate system. The latitude and
longitude values are represented by the Lat and Long properties. Unlike the geometry data type where
the X and Y coordinates can be any valid number, the Lat and Long properties must relate to valid
latitudes and longitudes for the selected spatial reference system. 4326 (or WGS 84) is the most
commonly used spatial reference system when working with the geography data type. The geography
data type can also store but not process Z and M values.

Result Size Limitations

In the earlier SQL Server 2008 implementation of the geography data type, any resulting geography value
needed to be contained within a single hemisphere. This did not mean any specific hemisphere such as
the northern or southern hemispheres but just that no two points could be more than half the Earth apart
if they were contained in the same instance of the geography data type. This limitation was removed in
SQL Server 2012.

Point Order in Polygons

When defining the shape of a polygon via a series of points, the order in which the points are provided
matters. Imagine the set of points that define a postal code region. The same set of points actually defines
two regions: all the points inside the postal code region and all the points outside the postal code region.
To enclose points, they are listed in anti-clockwise order. As you draw a shape, all the points to the left of
the line that you draw will be enclosed by the shape. The points on the line are also included.

Working with SQL Server 2012 Spatial Data

MCT USE ONLY. STUDENT USE PROHIBITED

19-20

If you draw a postal code region in a clockwise direction, you are defining all points outside the region. In
earlier versions of SQL Server, because results were not permitted to span more than a single hemisphere,
an error would have been returned. This restriction was removed in SQL Server 2012.

Spatial Results Viewer

As discussed in the previous topic, a spatial results viewer will be provided whenever a result set is
displayed in SQL Server Management Studio and the results include either geometry or geography data.
For geography, the viewer is quite configurable. You can set which column to display, the geographic
projection to use for display (for example, Mercator, Bonne, etc.), and you can choose to display another
column as a label over the relevant displayed region.

The spatial results viewer in SQL Server Management Studio in SQL Server 2012 is limited to displaying the
first 5000 objects from the result set.

Spatial Data Formats

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-21

The internal binary format of any CLR data type is not directly used for input and output of the data type
in most cases. String-based representations of the data need to be accommodated.

CLR data types (including the geometry and geography system CLR data types) are stored in a binary
format determined by the designer of the data type. While it is possible to both input and output
instances of the data type using a binary string, this is not typically very helpful as you would need to have
a detailed understanding of the internal binary format.

Spatial Data Formats

The OGC and other organizations that work with spatial data define a number of formats that can be used
for interchanging spatial data. Some of the formats that SQL Server supports are:

Well-known Text (WKT) is the most common string format and is quite human-readable.

Well-known Binary (WKB) is a more compact binary representation that is useful for computer to
computer interchange.

Geography Markup Language (GML) is the XML-based representation for spatial data.

All CLR data types must implement two string-related methods. The Parse method is used to convert a
string to the data type and the ToString method is used to convert the data type back to a string. Both of
these methods are implemented in the spatial types and both assume WKT format.
There are a number of variations of these methods used for input and output. As an example, the
STAsText method provides a specific WKT format as output and the AsTextZM method is a Microsoft
extension that provides the Z and M values as well as the 2D coordinates.
Question: Why is there a need to represent spatial data types as strings within SQL Server?

Working with SQL Server 2012 Spatial Data

OGC Methods and Properties

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

19-22

A wide variety of OGC methods and properties has been provided in SQL Server spatial data, along with a
number of OGC defined collections. Several of the common methods and properties are described here
but many more exist.

Common Methods

The STDistance method returns the distance between two spatial objects. Note that this does not only
apply to points. The distance between two polygons can also be calculated. It is returned as the minimum
distance between any two points on the polygons.
The STIntersects method returns 1 when two objects intersect and 0 otherwise.
The STArea method returns the total surface area of a geometry instance.

The STLength method returns the total length of the objects in a geometry instance. For example, with a
polygon, STLength returns the total length of all line segments that make up the polygon.
The STUnion method returns a new object that is formed by uniting all points from two objects.
The STBuffer method returns an object whose points are within a certain distance of an instance of a
geometry object.

Common Collection Properties

Support has also been provided in SQL Server for a number of collections defined in the OGC
specifications. A geometry can be held in a GeometryCollection object and can contain a number of other
nested geometry objects. Properties such as STNumGeometries and STGeometryN provide access to the
members of these collections.

Microsoft Extensions

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-23

Besides the OGC properties and methods, Microsoft has provided a number of useful extensions to the
standards. Several of these extensions are described in this topic but many more exist.

Common Extensions

While the coverage provided by the OGC specifications is good, Microsoft has enhanced the data types by
adding properties and methods that extend the standards. Note that the extended methods and
properties do not have the ST prefix.
The MakeValid method takes an arbitrary shape and returns another shape that is valid for storage in a
geometry data type. SQL Server produces only valid geometry instances, but allows for the storage and
retrieval of invalid instances. A valid instance representing the same point set of any invalid instance can
then be retrieved using the MakeValid method.
The Reduce method is used to reduce the complexity of an object while attempting to maintain the
overall shape of the object.
The IsNull method returns 1 if an instance of a spatial type is NULL; otherwise it returns 0.

The AsGML method returns the object encoded as Geographic Markup Language (GML). An example of
GML is shown here:
<Point xmlns="http://www.opengis.net/gml">
<pos>12 15</pos>
</Point>

GML is excellent for information interchange but you can see that the representation of objects in XML
can quickly become very large.

The BufferWithTolerance method returns a buffer around an object but uses a tolerance value to allow for
minor rounding errors.

Working with SQL Server 2012 Spatial Data

Demonstration 2A: Working with Spatial Data Types

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

19-24

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_19_PRJ\10776A_19_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

2.

Open the 21 Demonstration 2A.sql script file.

3.

Follow the instructions contained within the comments of the script file.

Lesson 3

Using Spatial Data in Applications

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-25

Once you have gained an understanding of how spatial data is stored and accessed in SQL Server, it is
important to gain an understanding of implementation issues that need to be addressed when building
applications that use SQL Server spatial data. In particular, spatial indexes can be created to improve the
performance of the applications and an understanding of how spatial indexes work and which methods
they are useful for is necessary for building performant applications.

Objectives
After completing this lesson, you will be able to:

Describe the need for spatial indexes

Explain the basic tessellation process used within SQL Server spatial indexes

Describe the different types of spatial indexes

Implement spatial indexes

Explain which geometry methods can benefit from spatial indexes

Explain which geography methods can benefit from spatial indexes

Describe options for extending SQL Server spatial data support

Working with SQL Server 2012 Spatial Data

Performance Issues in Spatial Queries

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

19-26

Spatial queries can often involve a very large number of data points. Executing methods like STIntersect
for a large number of points is slow. Spatial indexes help avoid unnecessary calculations. Otherwise,
complex geometric calculations are involved.

Discussion
Imagine trying to locate the streets that intersect your suburb or region.
Question: What is the challenge in locating the intersecting streets?
Question: Which streets would you need to check?
Question: How could you minimize this problem?

Tessellation Process

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-27

Spatial indexes help avoid unnecessary calculations by breaking larger problems down into problems that
need to be solved and those that dont need to be solved.

Tessellation

In the example from the discussion where you were considering finding streets that intersect your suburb
or region, the biggest problem is that checking every street in the state or worse, in the country, would
take a very long time. The irony of this is that almost all of these calculations would return an outcome
that showed no intersection.
To avoid making unnecessary calculations, SQL Server breaks the problem-space into relevant areas by
using a four-level grid. Each grid level is made up of a number of cells.

The basic idea is that if your suburb is located within a region of cells, any streets that do not extend into
those cells do not need to be checked at all. Grid levels are used to quickly isolate large areas that do not
need to be checked.

As a real-world human-based example, if you are checking for streets that are part of Vienna, you do not
need to check for streets that are contained entirely within Paris. Moreover, you do not need to check any
street that is contained entirely within France. You can quickly eliminate those as not being of interest to
you.

Working with SQL Server 2012 Spatial Data

Spatial Indexes

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

19-28

Spatial indexes are unlike standard relational indexes. Instead of locating specific rows to be returned,
queries that use spatial indexes operate in a two-phase manner. In the first phase, possible candidates are
found. In the second phase, the returned list of candidates is individually checked.

Spatial Indexes

When you traverse a clustered or non-clustered index on a SQL Server table, you apply the predicates in
the WHERE clause to filter the specific rows. After applying the predicate, you are left with precisely the
rows you require. Spatial indexes work differently than this. Instead of precisely locating the specific rows,
spatial indexes are used to locate rows that could potentially be of interest.

You saw in the last topic how tessellation can be applied to minimize the number of calculations that
need to be performed. Spatial indexes use this tessellation process to quickly reduce the overall number
of rows to a list of candidate rows that might potentially be of interest. In the street-based example that
was mentioned previously, if Vienna was contained inside a grid cell and a street entered that cell, you still
do not know if the street actually intersects the boundaries of Vienna. However, you know that you need
to check whether or not it does, as it is possible that it might.

Primary and Secondary Filters

Spatial indexes are used as a primary filter on the data. All possible candidate rows are returned by the
filter. There may still be false positives after the filter is applied. The secondary filter is used to locate the
precise rows of interest. It executes the methods in the WHERE clause of the query on the filtered set of
candidate rows. This greatly reduces the number of calculations that SQL Server needs to make as long as
the spatial index has been effective.
To allow a check on the effectiveness of the primary filter, SQL Server provides a Filter method that only
applies to the primary filter. The number of rows returned by that method can then be compared to the
total number of rows to see how effective the spatial index has been. This will be shown in an upcoming
demonstration.

Implementing Spatial Indexes

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-29

Spatial indexes are created using the CREATE SPATIAL INDEX statement. Indexes on geometry data type
should specify a BOUNDING_BOX.

CREATE SPATIAL INDEX

Spatial indexes are created in a similar way as non-clustered indexes. You execute the CREATE SPATIAL
INDEX statement, providing a name for the index, the table that the index is to be created on, and the
spatial data column that needs to be indexed. The table must have a clustered primary key before a
spatial index can be built on it.

Index Bounds

Unlike more traditional types of index, a spatial index is most useful when it knows the overall area that is
covered by the spatial data. Spatial indexes created on the geography data type do not need to specify a
bounding box as the data type is naturally limited by the Earth itself.

Spatial indexes on the geometry data type specify a BOUNDING_BOX. This provides the coordinates of a
rectangle that would contain all possible points or shapes of interest to the index. The geometry data type
has no natural boundaries and specifying a bounding box allows SQL Server to produce a more useful
index. If values arise outside the bounding box coordinates, the rows they are contained in would need to
be returned by the primary filter.

Working with SQL Server 2012 Spatial Data

Grid Density

MCT USE ONLY. STUDENT USE PROHIBITED

19-30

SQL Server also allows you to specify grid densities when creating spatial indexes. You specify a value for
the number of cells per grid for each grid level in the index.

A value of LOW indicates 16 cells per grid or a 4 x 4 cell grid.

A value of MEDIUM indicates 64 cells per grid or an 8 x 8 cell grid.

A value of HIGH indicates 256 cells per grid or a 16 x 16 cell grid.

Spatial indexes are also different from other types of index as it might make sense to create multiple
spatial indexes on the same table and column. Indexes with one set of grid densities might be more useful
than a similar index with a different set of grid densities for locating data in a specific query.
To make spatial indexes easier to configure, SQL Server 2012 introduced automatic grid density and level
selections: GEOMETRY_AUTO_GRID and GEOGRAPHY_AUTO_GRID. The automated grid configuration
defaults to an eight level grid.

Limitations
Spatial indexes do not support the use of ONLINE build operations, as are available for other types of
index in the Enterprise Edition of SQL Server.

geometry Methods Supported by Spatial Indexes

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-31

Not all geometry methods and not all predicate forms can benefit from the presence of spatial indexes.
The table in the slide shows the specific predicates that can potentially make use of a spatial index as a
primary filter. Unless the predicate in your query is in one of these forms, spatial indexes that you create
will be ignored.

Working with SQL Server 2012 Spatial Data

geography Methods Supported by Spatial Indexes

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

19-32

Similarly to the geometry type, not all geography methods and not all predicate forms can benefit from
the presence of spatial indexes. The table in the slide shows the specific predicates that can potentially
make use of a spatial index as a primary filter. Unless the predicate in your query is in one of these forms,
spatial indexes that you create will be ignored.

Extending SQL Server Spatial

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-33

A very active community that contributes user-created extensions to SQL Server spatial data exists on the
CodePlex site.

In the slide, the functions, types, and aggregates that were present in the sqlspatial.codeplex.com project
at the time of writing these notes are listed. As the project continues to evolve, the capabilities provided in
the project will change.
You may be able to use some of these extensions directly. They may also be useful as starting points when
creating your own extensions to the SQL Server spatial data.
Note also that several additional built-in aggregates were added to SQL Server spatial in SQL Server 2012.

Working with SQL Server 2012 Spatial Data

Demonstration 3A: Using Spatial Data in Applications

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

19-34

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_19_PRJ\10776A_19_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

2.

Open the 31 Demonstration 3A.sql script file.

3.

Follow the instructions contained within the comments of the script file.

Lab 19: Working with SQL Server Spatial Data

Lab Setup

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

19-35

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In the Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_19_PRJ\10776A_19_PRJ.ssmssln.

7.

From the View menu, click Solution Explorer. In Solution Explorer, double-click the query
00-Setup.sql. When the query window opens, click Execute on the toolbar.

Lab Scenario

Your organization has only recently begun to acquire spatial data within its databases. The new
Marketing database was initially designed prior to the company beginning to implement spatial
data. One of the developers has provided a table of the locations where prospects live. It is called
Marketing.ProspectLocation. A second developer has added columns to it for Latitude and Longitude
and geocoded the addresses. You will make some changes to the system to help support the need for
spatial data.

Working with SQL Server 2012 Spatial Data

Supporting Documentation
Stored Procedure Specifications
Stored Procedure Name

Marketing.GetNearbyProspects

Input Parameters

@ProspectID int
@DistanceInKms int

Output Parameters

None

Output Rowset Columns

Distance (in Kms)


ProspectID
LastName
FirstName
WorkPhoneNumber
CellPhoneNumber
AddressLine1
AddressLine2
City
Longitude
Latitude

Output Order

Distance

Exercise 1: Familiarity with Geometry Data Type


Scenario

MCT USE ONLY. STUDENT USE PROHIBITED

19-36

In this lab, you have decided to learn to write queries using the geometry data type in SQL Server. You will
review and execute scripts that demonstrate querying techniques.
The main tasks for this exercise are as follows:

Review and execute the sample script.

f Task 1: Review and execute the sample script

Review the query, execute the query, and review the results for scripts 19.1 to 19.9.
Results: After this exercise, you should have seen how to work with the geometry data type.

Exercise 2: Adding Spatial Data to an Existing Table


Scenario

In this lab, you need to modify an existing table Marketing.ProspectLocation to replace the existing
Latitude and Longitude columns with a new Location column of type geography. You need to migrate the
data to the new Location column before you delete the existing Latitude and Longitude columns.
The main tasks for this exercise are as follows:
1.

Add a Location column.

2.

Write code to assign values to this column.

3.

Drop the existing Latitude and Longitude columns.

f Task 1: Add a Location column

Add a Location column to the Marketing.ProspectLocation table.

f Task 2: Write code to assign values to this column

19-37

Write code to assign values to this column based on the existing Latitude and Longitude columns.

f Task 3: Drop the existing Latitude and Longitude columns

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

Once you are sure the new column has correct data, drop the existing Latitude and Longitude
columns.

Results: After this exercise, you should have replaced the existing Longitude and Latitude columns with
a new Location column.

Challenge Exercise 3: Business Application of Spatial Data (Only if time


permits)
Scenario

Salespeople are keen to visit with prospects at their own locations, rather than just on the phone. To
minimize effort, they are keen when visiting a prospect to also see other prospects in the same area. You
will write a stored procedure that provides details of other prospects in the area. To ensure it performs
quickly, you will create a spatial index on the table.
The main tasks for this exercise are as follows:
1.

Review the requirements.

2.

Create a spatial index.

3.

Design and implement the Stored Procedure.

4.

Test the procedure.

f Task 1: Review the requirements

Review the supporting documentation for details of the required stored procedure.

f Task 2: Create a spatial index

Create a spatial index on the Marketing.ProspectLocation table.

f Task 3: Design and implement the Stored Procedure

Design and implement the stored procedure based on the specifications given in the supporting
documentation for the lab.

f Task 4: Test the procedure

Test the stored procedure by executing the following code:


EXEC Marketing.GetNearbyProspects 2,50;

Results: After this exercise, you should have created and tested the required stored procedure.

Working with SQL Server 2012 Spatial Data

Module Review and Takeaways

Review Questions
1.

What is the main difference between the geometry and geography data types?

2.

Why does the order of points matter when defining a polygon?

Best Practices

MCT USE ONLY. STUDENT USE PROHIBITED

19-38

1.

Set the SRID for geometry objects to 0 to ensure that operations on multiple geometry objects can
always be performed.

2.

Use a CHECK CONSTRAINT to ensure that the SRID values for a column are consistent across all rows.

3.

Before creating spatial indexes, make sure that the queries that need to be executed against the data
use predicate forms that are supported by the types of index you are creating.

MCT USE ONLY. STUDENT USE PROHIBITED


19-1

Module 20
Working with Full-Text Indexes and Queries
Contents:
Lesson 1: Introduction to Full-Text Indexing

20-3

Lesson 2: Implementing Full-Text Indexes in SQL Server

20-9

Lesson 3: Working with Full-Text Queries

20-20

Lab 20: Working with Full-Text Indexes and Queries

20-32

Working with Full-Text Indexes and Queries

Module Overview

MCT USE ONLY. STUDENT USE PROHIBITED

20-2

Existing business applications and the database applications that are associated with them tend to offer
very limited options when users need to search for information.

Full-text indexing in Microsoft SQL Server allows you to create very flexible searching options, much
more powerful than what is possible with traditional T-SQL language statements alone.

Objectives
After completing this module, you will be able to:

Describe why user interfaces in existing applications are not sufficient for end user search needs

Implement full-text indexes in SQL Server

Query SQL Server using full-text queries

Lesson 1

Introduction to Full-Text Indexing

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-3

Most developers feel that their applications provide sufficiently usable search options for end users. This is
usually not the case. There is a substantial mismatch between what is being provided to end users and
what they really want to use.
In this lesson, you will explore this mismatch and see how traditional searching approaches using T-SQL
statements such as LIKE are not capable of providing appropriate levels of search capability. You will also
investigate the types of queries that users really wish to execute.

Objectives
After completing this lesson, you will be able to:

Explain why applications need to offer much more flexible user-interaction options

Explain why the T-SQL LIKE statement, while powerful, does not offer the level of flexibility that users
really want

Execute queries that use SQL Server full-text predicates or table-valued functions

Working with Full-Text Indexes and Queries

Discussion: The Need for More Flexible User Interaction

Key Points
Spend some time looking at the two simple search screens on the slide. Ask yourself which of these
screens you would prefer to interact with.
Question: Which type of interface would you prefer?
Most people prefer the first search page shown. Why do you think so?

MCT USE ONLY. STUDENT USE PROHIBITED

20-4

IT Professionals tend to like everything they deal with to be exact, precise, and well-structured. Ironically,
this is the exact opposite of what most end users prefer. They prefer much more flexible and fuzzy
interfaces.

Consider the early online mapping engines. When you interacted with those online mapping engines, you
needed to enter values into multiple input locations for street number, street name, suburb, state, country,
etc. in contrast to the current online mapping engines. For users in some countries, it may not have been
obvious how to answer those questions. For example, concepts such as states, or postal (or zip) codes
differ widely between countries. Most online mapping engines today have a single input box where you
type what you are looking for. The system then works out whats needed.
Question: Why aren't interfaces to business systems built like that?

Why LIKE Isnt Enough

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-5

The LIKE operator in T-SQL is a very powerful operator that works by finding string values within strings. It
applies a pattern-based search. While this can be useful, there is a big difference between searching for
substrings and searching for words.

T-SQL LIKE Operator

The most common use for the LIKE operator in T-SQL is to search for substrings within strings. In the
example shown on the slide, the search term '%Lee%' is being used. This looks for rows where the
FirstName column contains the string 'Lee'. This is not the same as searching for the word Lee. The search
also returns rows where the name is Ashlee, Carolee, Colleen, Kathleen, Keylee, Shirleen, Waleed, and
Wathalee. Similarly, if a search was made using the term '%Pen%', as well as values containing the word
Pen, you would get values such as pencil, pendulum, penitentiary, open, and so on.
Question: What is really needed to find the word Pen?
Question: What would you need to know to be able to find words rather than substrings?

Performance

Another consideration is performance. An index on a column is ignored when you are searching for values
using a pattern with a leading % wildcard.
Question: Is LIKE case sensitive?

Working with Full-Text Indexes and Queries

Fuzziness in Queries

Key Points
While being able to find words within columns that you search is useful, effective search needs much
more capability than that. End users are looking for much more fuzzy concepts when searching.

Query Fuzziness

MCT USE ONLY. STUDENT USE PROHIBITED

20-6

IT professionals tend to like to work in an exact and precise way with highly defined structure. Ironically,
end users want something quite different. They prefer interfaces that are much more flexible and fuzzy.
They often don't know exactly what they are looking for but they know something about it, such as what
it was about or some of the key words related to it.

Near

Most users have had the experience of searching for two words and the returned results include
documents that contained both words but they were so far apart in the document that the fact that they
were both contained in the document was almost meaningless.
Ideally, you need a concept of nearness. Often the nearer together two words are, the more relevant the
result is. But how do you measure how far apart words are?
Full-text indexes in SQL Server understand nearness and also understand more subtle concepts, such as
sentence and paragraph breaks. SQL Server knows that two words that are side-by-side in a single
sentence are logically closer than two words with a sentence break between them.

Thesaurus

It is common for different people in an organization to call the same object by different names. One
person might say Client while another says Customer. Full-text search in SQL Server is capable of being
taught that these terms are equivalent. This can be particularly important with companies that are
involved in mergers.

Inflections

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-7

To a human, the words drive, drove, and driving are really just expressing the same concept. If a
human was searching for one of these words, they would expect the system to return results containing
any of them. This is much harder for computer systems. SQL Server understands inflectional forms of
words.

FreeText

End users often do not know what they are looking for but know some information about what the result
is related to. For example, if the user wants to find data related to Attempts to Improve Solar Energy
Efficiency, how would you search for that using T-SQL?
Full-text indexing and querying in SQL Server allows you to search for results based on semantics (or
meaning). This is extremely powerful and directly appeals to end users. Consider how popular existing
Internet-based search engines such as Bing are. Users really like this form of search yet very few business
applications today provide them with the ability to do searches like this.

Working with Full-Text Indexes and Queries

Demonstration 1A: Using Full-Text Queries

Demonstration Steps

MCT USE ONLY. STUDENT USE PROHIBITED

20-8

1.

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click SQL
Server Management Studio. In the Connect to Server window, type Proseware in the Server
name text box and click Connect. From the File menu, click Open, click Project/Solution, navigate
to D:\10776A_Labs\10776A_20_PRJ\10776A_20_PRJ.ssmssln and click Open.

3.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file from
within Solution Explorer.

4.

Open the 11 Demonstration 1A.sql script file.

5.

Follow the instructions contained within the comments of the script file.

Lesson 2

Implementing Full-Text Indexes in SQL Server

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-9

In the previous lesson, you saw how traditional search options based on T-SQL operators such as
LIKE provided only limited searching ability. In this lesson, you will learn about the advanced search
capabilities and how to implement full-text indexes. Before learning the details of how full-text indexes
are implemented, you will also need to gain an understanding of the components of a full-text searching
system.

Objectives
After completing this lesson, you will be able to:

Describe existing search-related options and their features

Discuss specific capabilities of full-text indexes implemented in SQL Server

Describe the core components that make up a full-text search system

Detail the available languages supported by SQL Server full-text indexing

Implement a full-text index in SQL Server

Working with Full-Text Indexes and Queries

Discussion: Search-related Options

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

20-10

There are many existing forms of search available on the Microsoft Windows operating system platform.
Question: Which forms of search are you already familiar with?
Question: Describe your experiences (both good and bad) with any form of search.

Include your experience with earlier versions of full-text search in SQL Server if you have used it. Full-text
search in SQL Server was completely redesigned in SQL Server 2008. In prior versions, SQL Server
interfaced with the search capabilities at the operating system level. This led to both management and
performance-related challenges.
Starting with SQL Server 2008, full-text search has been implemented directly inside the SQL Server
engine. This has brought significant benefits in terms of both management and performance. The only
components that now live outside the SQL Server engine are the components that can be created or
customized by end users and developers. This separation is maintained to ensure stability and security of
the SQL Server engine.

Full-Text Search in SQL Server

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-11

Full-text indexing in SQL Server allows for the creation of word-based indexes built on character-based
data stored in SQL Server tables.

Data Types

Full-text indexes in SQL Server are created on character-based data. A wide variety of character data types
is supported. Single-byte character columns, char and varchar, are supported as well as their double-byte
character equivalents, nchar and nvarchar.
While creating full-text indexes on text, ntext, and image data types is possible, it is not recommended as
these data types have been deprecated and will be removed in a later version of SQL Server. If possible,
first convert any text, ntext, and image columns as follows:

Old Data Type

New Data Type

text

varchar(max)

ntext

nvarchar(max)

image

varbinary(max)

Next, build indexes on those data types.


XML data can also be indexed via full-text indexes. XML indexes allow fast access for certain types of
XQuery based queries. Full-text indexes on XML allow indexing all the text within the XML.

Working with Full-Text Indexes and Queries

MCT USE ONLY. STUDENT USE PROHIBITED

20-12

SQL Server knows how to extract character data from a large number of document types that can be
stored in varbinary(max) columns. To be able to index these columns, another column in the table must
hold the file extension (for example, .TXT) of the document that is being held in the varbinary(max)
column.

Full-Text Indexes

Full-text indexes are stored in the database, as other indexes are. Each index is associated with a table that
contains the character data being indexed and full-text indexing supports multiple concurrent languages
in different columns.
Once full-text indexes are created, special query predicates and table-valued functions can be used to
provide powerful querying abilities. The table-valued functions are also able to rank results, where
appropriate, to provide a measure of how relevant the returned results are.

Full-text indexes can be created on textual data in a variety of languages. A significant number of
languages are supported by default and it is possible to extend the product by the construction of code
to support additional languages. The predicates that are supported by full-text indexes allow users to
query using simple words and phrases that are much more familiar to the users.

Core Components of Full-Text Search

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-13

The full-text indexing system is made up of a number of components that are described using
specific terminology. It is important to understand the role of each component and to understand the
terminology used. iFilters, Word Breakers and Stemmers operate sequentially to extract the words that
need to be placed in the index.

iFilter

The first step in indexing character data is to obtain a stream of characters. If the data is being held
in a character-based column in a table, this is straightforward. However, if the data is being held in a
document stored in a varbinary(max) column, SQL Server needs to know how to extract a stream of
characters from that type of document. The filters that perform this action are called iFilters. A significant
number of iFilters are provided with SQL Server. You can see the list of installed filters by querying the
sys.fulltext_document_types view.

SQL Server also supports the concept of adding additional iFilters. You can create user-created iFilters for
special types of documents or you can obtain iFilters from companies that create special document types.
A common example of this would be to install an iFilter to work with PDF documents.
Note that if you need to support Microsoft Office 2007 documents, you can download the required
iFilters from the following location:
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=20109

Word Breaker

Once a stream of characters has been obtained, the next step is to break that stream into a series of
words. This involves an understanding of the punctuation and whitespace rules in each language. The
output is then a stream of words, rather than a stream of characters.

Working with Full-Text Indexes and Queries

Stemmer

MCT USE ONLY. STUDENT USE PROHIBITED

20-14

When a stream of words has been obtained, processing needs to be done to standardize the format of the
words. Inflectional forms of the words need to be resolved and verbs conjugated (that is, drive, drove,
driving).
Together, word breaking and stemming are often called tokenizing.

Noise Word (Stop Word) Removal

It is not always desirable to index all the words in a document. Every language has a series of words such
as and or the that do not add value to the index. You can see the list of standard noise words for the
English language (which has Windows locale identifier 1033) by executing the query:
SELECT * FROM sys.fulltext_system_stopwords WHERE language_id = 1033;

Note that in recent versions of SQL Server, the term noise word has been replaced by the term stop
word. SQL Server also allows you to create stoplists that contain your own stopwords that you do not
want to index. A good example of this would be your company name. It is likely that it appears in every
document so it would likely add little value to the index.

Question: Which among the core components of full-text search are likely to be language-specific?

Indexing, Querying, Scoring

Finally, you then have a list of words to be indexed. SQL Server uses them to construct the full-text index.
It is important to understand that, unlike a standard clustered or nonclustered index, full-text indexes are
not updated during data modification statements. However, if auto-population is enabled, the full-text
indexes are updated shortly after the data modification has been performed.

Querying involves using the full-text predicates and table-valued functions to return results. Scoring (also
known as ranking) involves providing a measure of relevance of each row in the result set.

iFilter, Word Breaker, Stemmer, and Stopword Example

As a high-level example of the actions performed by the iFilter, Word Breaker, Stemmer and Stopword
components, consider the following document:
<Document>
<Author>Proseware, Inc.</Author>
<Address>19 Proseware Drive, CONTOSO</Address>
<Title>Driving Us Broke And Driving Us Crazy</Title>
</Document>

An iFilter could be created to extract text from this type of document. The iFilter will output a stream of
text. The exact text that is output will depend upon the design of the iFilter but the following output
could be obtained:
author - proseware, inc..address - 19 proseware drive, contoso.title - driving us broke
and driving us crazy.

Note that this particular iFilter replaces XML element tags with the name of the tag and a dash separator.
It also adds a period at the end of each tag, to assist with keeping terms that are related within the same
sentence in the output.

After a Word Breaker has been applied, the following output could be obtained:
author proseware inc. address 19 proseware drive contoso. title driving us broke and
driving us crazy.

After a Stopword list is applied, the following output could be obtained:


author proseware inc. address proseware drive contoso. title driving broke driving
crazy.

After a Stemmer has been applied, the following output could be obtained:

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-15

author proseware inc. address proseware drive contoso. title drive broke driving crazy.

At this point, the words are ready to be inserted into a full-text index. The list of words is different to the
original text but is sufficient for indexing the text.

Working with Full-Text Indexes and Queries

Language Support and Supported Word Breakers

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

20-16

A large number of languages are supported by the built-in functionality in SQL Server full-text indexing.

Language Support

You can see from the list of languages presented in the slide how many languages are directly supported
by full-text indexing in SQL Server.
A full list of the supported languages can be seen by executing the following query:
SELECT * FROM sys.fulltext_languages;

The query returns the Windows locale identifier in a column called lcid and the name of the language.
Question: Which of the languages in the list do you need to support in your applications or
databases?

Implementing Full-Text Indexes

Key Points
Creating and querying full-text indexes is not difficult but involves a number of steps.

Table with Character-based Data

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-17

Full-text indexes are created on tables in a way that is similar to how non-clustered indexes are created.
The table must contain some character-based data from the list of supported data types mentioned
earlier.

Primary Key or Unique Constraint/Index

Full-text indexes can only be created on tables with either primary keys (best option) or another form of
uniqueness for the rows. This can be either a unique constraint (most likely) or a unique index.

Full-Text Catalog

Full-text indexes are created within full-text catalogs. This concept is mainly retained for backward
compatibility with the earlier versions of SQL Server, when the full-text indexes were created in the
operating system rather than directly in the database. While some maintenance operations can still be
carried out at the catalog level but in general, most databases do not need more than one full-text
catalog.

While the CREATE FULLTEXT CATALOG statement includes an option to specify a filegroup, this setting is
now ignored as is the optional PATH parameter.
One full-text catalog can be identified as the DEFAULT catalog and will be used for all full-text indexes
that are created without specifying a catalog.

Working with Full-Text Indexes and Queries

Full-Text Index

MCT USE ONLY. STUDENT USE PROHIBITED

20-18

The full-text index is created using the CREATE FULLTEXT INDEX command. You need to specify a name
for the index, the table that it is based upon, and the name of the column that contains the character
data.

If the character data is contained within a document in a column of type varbinary(max), you also need to
specify the name of another column that will hold the name of the type of document. For example, this
column could hold values such as '.TXT' and '.DOC'.
You also need to specify which index can be used to uniquely identify rows within the table. This will
almost always be the primary key of the table. One full-text index is allowed per table or indexed view,
and each full-text index applies to a single table or indexed view.

Populate the Index

Once the index is created, you can manually request SQL Server to start populating it, based on the
existing data or you can set the index to be auto-populated. Auto-population is the most common option
used. SQL Server tracks the rows that have been changed and updates the index as close to the end of the
data modification as possible.

Query the Index


Querying the full-text indexes relates to using the full-text predicates or the full-text table-valued
functions in T-SQL queries. In the next lesson, you will see how to use full-text querying capability to
return results from the full-text indexes.

Demonstration 2A: Implementing Full-Text Indexes

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-19

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_20_PRJ\10776A_20_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

2.

Open the 21 Demonstration 2A.sql script file.

3.

Follow the instructions contained within the comments of the script file.

Working with Full-Text Indexes and Queries

Lesson 3

Working with Full-Text Queries

MCT USE ONLY. STUDENT USE PROHIBITED

20-20

The real power of using full-text indexes comes after the index has been built and populated and you
begin to execute queries that make use of the index. There are two basic ways to execute queries using
full-text indexes. Full-text predicates can be added to WHERE clauses. Alternatively, tables of full-text
results can be returned and queried or joined to other tables. It is also important to be able to exercise
control over which words are included in the index and to use the thesaurus to provide synonyms or
replacements for common search terms.

Objectives
After completing this lesson, you will be able to:

Write queries using the CONTAINS predicate

Write queries using the FREETEXT predicate

Writing queries using customizable nearness

Return CONTAINS and FREETEXT results as tables along with ranking of relevance

Create stopwords and stoplists

Add details of synonyms and replacements to a full-text thesaurus

Manage and debug full-text indexes

CONTAINS Queries

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-21

Queries using the CONTAINS predicate search for words or combinations of words. They can also perform
proximity searches and advanced searches using inflectional forms of words or synonyms and
replacements from a thesaurus.

CONTAINS

The CONTAINS predicate is used to search for words. The most basic form of usage is shown in the first
example on the slide. The query returns the MessageID and Description columns from the dbo.Messages
table for each row where the Description column contains the word 'filing'.

Boolean Logic

Boolean logic can also be applied to search for combinations of words. In the second example shown in
the slide, the query is changed so that only rows where the Description column contains the word 'file' but
does not also contain the word 'boundary' are returned.
The permitted Boolean operators are:

AND

AND NOT

OR

Working with Full-Text Indexes and Queries

Proximity
The CONTAINS predicate can also search for combinations of words using the NEAR operator. This is
shown in the following WHERE clause:
WHERE CONTAINS(Description, 'NEAR(drive,car),2,FALSE')

MCT USE ONLY. STUDENT USE PROHIBITED

20-22

This predicate will filter rows where the Description column contains the word drive within two words of
the word car in either direction. It is important to realize however, that NEAR is a relative term. NEAR is
described further later in this lesson.

Inflectional

Earlier in the module, you learned how end users think of words like drive, drove, and driving as being
different versions of the same word. In a CONTAINS query, you can include a search for these by using the
FORMSOF function. This is shown in the following WHERE clause:
WHERE CONTAINS(Description, 'FORMSOF(INFLECTIONAL, drive)')

This predicate will filter rows where the Description column contains a word that is an inflectional form of
the word drive, such as driving or drove.

Thesaurus

It is also useful to be able to search for words that are treated as having the same meaning within the
organization. This is shown in the following WHERE clause:
WHERE CONTAINS(Description, 'FORMSOF(THESAURUS,client)')

This predicate will filter rows where the Description column contains words that have been configured as
synonyms for the word client. Later in the lesson, you will see how to configure a thesaurus to make
these queries possible.

Specified Relevance

The CONTAINS predicate can also search for terms based upon the weighting that you assign to each
term. This is shown in the following WHERE clause:
WHERE CONTAINS(Description, 'ISABOUT (certificate weight (.8), symmetric weight (.4),
key weight (.2) )')

In this WHERE clause, the developer is telling SQL Server how important each search term is by assigning
a weight to each term. In this example, the word 'certificate' is ranked as 0.8, the word 'symmetric' is
ranked as 0.4 and the word 'key' is ranked as 0.2. The more important a search term is, the higher the
weight that should be assigned to it. Weights are values between 0 and 1.0.
The ISABOUT function is another example where it is important to rank the relevance of the results
returned. Ranking will be demonstrated later in this lesson.

FREETEXT Queries

Key Points
FREETEXT queries are the most powerful form of full-text query. They depend upon SQL Server
understanding the meaning of the term or phrase being searched for.

FREETEXT

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-23

The FREETEXT predicate allows for the creation of the most powerful forms of full-text query. Users can
enter the details of the meaning of what they are looking for and SQL Server finds matches that are
relevant.

In the example shown in the slide, the MessageID and Description columns from the dbo.Messages table
are being returned for any rows where the Description column contains a value that is relevant to the
phrase 'statement was terminated'.

Weighting

SQL Server determines the relevance of the column data by internally assigning a weight to each of the
search terms and then matching using the weights. This is very similar to the way the ISABOUT option
works in the CONTAINS predicate but with FREETEXT, SQL Server automatically assigns the search term
weightings based on an internal algorithm. This provides a result that makes it appear that SQL Server has
understood the meaning of the search phrase.

Working with Full-Text Indexes and Queries

Customizable Nearness

Key Points
You can locate words that are near other words. The proximity of words can be important when
determining the relevance of a match.

NEAR
Previous versions of SQL Server used a fixed concept of nearness. You could search for 'user NEAR
certificate'. This fixed form is now deprecated.

MCT USE ONLY. STUDENT USE PROHIBITED

20-24

In the example on the slide, a match will occur when the words user and certificate are not separated by
more than two non-search terms and if they are found in the same order as they are listed. (The
parameter shown as TRUE is optional and indicates whether or not specific ordering is required for a
match.)

Table Functions and Ranking Results

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-25

Many of the CONTAINS and FREETEXT predicate options will return a large number of returned rows.
Some will be more relevant than others. SQL Server can also provide a ranking on the relevance of the
results when table-valued functions are used instead of WHERE clause predicates.

CONTAINSTABLE

CONTAINSTABLE is a table-valued function that returns the same rows that would have been filtered by a
CONTAINS predicate but additionally provides a measure of relevance of each returned result via a RANK
column that is returned in the table. It also provides a KEY column that determines the returned row so
that it can be joined to the underlying table if necessary.

FREETEXTTABLE

FREETEXTTABLE is the table-valued function equivalent of the FREETEXT predicate. It also returns the
same rows that would have been filtered by a FREETEXT predicate along with RANK and KEY columns.

In the example shown in the slide, the MessageID and Description columns are being returned from the
dbo.Messages table for rows where the Description column is relevant to the search phrase 'statement
was terminated'. Note, however, that the rows that are filtered have been returned as a table which has
then been joined back to the source dbo.Messages table.

The table returned by the FREETEXTTABLE function includes a column named KEY that is then used to
locate the row in the original table. In this example, it would be used to find entries in the dbo.Messages
table based upon the value in the MessageID column.

Working with Full-Text Indexes and Queries

RANK
Besides returning the relevant rows, CONTAINSTABLE and FREETEXTTABLE also return a measure of
relevance of each returned row called RANK.

MCT USE ONLY. STUDENT USE PROHIBITED

20-26

In the example shown in the slide, the query returns an output where the resultant rows are arranged by
the RANK descending. This will return the most relevant rows first. You will see an example of this in the
next demonstration.
Note that the value returned by the RANK column is only meaningful within the table of results that are
returned during that particular execution of the query. The value cannot be used to compare with other
CONTAINSTABLE or FREETEXTTABLE function return values.
Question: Would a search engine be more likely to use the table forms of these or the predicate
forms?

Thesaurus

Key Points
The full-text thesaurus allows specifying synonyms or replacements for search terms. It is set up in a
language-specific XML file for each SQL Server instance.

Thesaurus

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-27

The full-text thesaurus does not live within the database. It is an instance-level XML file that contains
details of synonyms and replacement searches. There is one XML file per language per SQL Server
instance. It is stored in the FTDATA folder that is located within the default database file folder for the
instance.

In the example shown in the slide, an expansion has been set up. Expansions are used for synonyms. In
this example, SQL Server has been told that the words 'user', 'operator', and 'developer' can be used
interchangeably. If a search is performed for any of these three words, a search is automatically performed
for all three of the words.
A replacement has also been created in the example shown in the slide. A replacement is used to provide
alternate search terms. In the example shown, if the user searches for either the word 'NT5' or the word
'W2K', then a search for the word 'Windows 2000' would be carried out instead. Note that no search for
'NT5' or 'W2K' would actually be carried out. Only the replacement term would be searched for.

Working with Full-Text Indexes and Queries

Diacritic Sensitivity

MCT USE ONLY. STUDENT USE PROHIBITED

20-28

You may have noticed another option in the example thesaurus file in the slide. Diacritics are marks that
are used as indicators in certain languages. Mostly, these indicators relate to phonetics and alter how a
word is pronounced. Diacritics are uncommon in the English language today apart from when words have
been adopted from another language. Many other languages use accents, breves, diaeresis, cedillas, etc.
For example, the Greek language includes diacritics for breathings, stress and iota subscripts. Indic
diacritics include the anusvara, chandrabindu, nukta, and virama.
With the default value of zero for diacritics_sensitive, these are ignored. When the value is set to one, the
search becomes sensitive to diacritics.

Stopwords and Stoplists

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-29

Not all words in any language are useful in a word index. SQL Server automatically excludes common
noise words from full-text indexes. These are referred to as stopwords. You can also configure other words
to be ignored by full-text indexing. Stopwords are created within stoplists.

Noise Words (Stopwords)

Using English as an example, words such as 'and' or 'the' are not useful in an index. Similarly, in Simplified
Chinese (language_id 2052), words such as '' (which means 'I') or '' (which means 'not') are not useful
in an index. Each language, therefore, has a set of words that should be ignored when creating an index.
You can see the list of system-supplied stopwords for all languages by executing the following query:
SELECT * FROM sys.fulltext_system_stopwords;

Stoplists and Stopwords


In addition to the system-supplied stopwords, SQL Server allows you to define your own sets of
stopwords. These are grouped together in stoplists.

In the slide, a full-text stoplist called CompanyNames is being created and then the word 'Microsoft' is
being added for language_id 1033 (English).

The word 'Microsoft' would then no longer be indexed in full-text indexes within this database. You can
see the list of stoplists that have been created by executing the following query:
SELECT * FROM sys.fulltext_stoplists;

You can see the user-defined stopwords by executing the following query:
SELECT * FROM sys.fulltext_stopwords;

Working with Full-Text Indexes and Queries

SQL Server Management of Full-Text

Key Points

MCT USE ONLY. STUDENT USE PROHIBITED

20-30

Full-text indexes are stored within the database that holds the tables they are based on. Like other
indexes, they can benefit from periodic reorganization to remove fragmentation. Several useful DMVs
have been provided for working with full-text indexing.

Fragmentation

Full-text indexes can become fragmented like other types of index. This can cause a loss of performance.
The ALTER INDEX REORGANIZE command can be used to reorganize a full-text index to reduce
fragmentation and increase performance. Changes to full-text indexes are not merged into the main
index files immediately. Changes are first written to separate locations within the index. To perform a
master merge on all full-text indexes in a full-text catalog, the ALTER FULLTEXT CATALOG REORGANIZE
command can be used. A fully merged index will have higher performance.

Dynamic Management Views and Functions

Several dynamic management objects have been added to SQL Server to make it easier to troubleshoot
full-text indexing. The views and functions are all prefixed with a sys.dm_fts* prefix.

One particularly useful function is sys.dm_fts_parser. You can pass a string, a language id, and optionally a
stoplist to this function. It will then display how it parses the string into words and how it categorizes
those words into useful words or noise words. This can be very helpful when trying to work out how a
character value is being indexed.
Full-text indexing supports many document types without the need to install additional components. You
can see the list of supported document types by executing the query:
SELECT * FROM sys.fulltext_document_types;

This query returns a row for each supported document type.

Demonstration 3A: Working with Full-Text Queries

Demonstration Steps
1.

If Demonstration 1A was not performed:

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-31

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, click
SQL Server Management Studio. In the Connect to Server window, type Proseware in the
Server name text box and click Connect. From the File menu, click Open, click
Project/Solution, navigate to D:\10776A_Labs\10776A_20_PRJ\10776A_20_PRJ.ssmssln and
click Open.

From the View menu, click Solution Explorer. Open and execute the 00 Setup.sql script file
from within Solution Explorer.

2.

Open the 31 Demonstration 3A.sql script file.

3.

Follow the instructions contained within the comments of the script file.

Working with Full-Text Indexes and Queries

MCT USE ONLY. STUDENT USE PROHIBITED

20-32

Lab 20: Working with Full-Text Indexes and Queries

Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines using the instructions at D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In the Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_20_PRJ\10776A_20_PRJ.ssmssln.

7.

From the View menu, click Solution Explorer. In Solution Explorer, double-click the query
00-Setup.sql. When the query window opens, click Execute on the toolbar.

Lab Scenario

Users have been complaining about the limited querying ability provided in the marketing system. You
are intending to use full-text indexing to address these complaints.

You will implement a full-text index on the Marketing.ProductDescription table to improve this situation.
You will implement a stoplist to avoid excessive unnecessary index size.

If you have time, your manager would like you to help provide a more natural interface for your users.
This will involve creating a new stored procedure.

Supporting Documentation
Stored Procedure Specifications
Name

Marketing.GetRelevantDescriptions

Input Parameters

@PhraseToSearch nvarchar(1000)

Output Parameters

Nil

Columns Returned

ProductDescriptionID, Description, Ranking

Provide relevant results by using the FREETEXTTABLE function and limit your results to rows with
LanguageID = 'en' (for English).

Exercise 1: Implement a Full-Text Index


Scenario

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-33

In this exercise, you will implement a full-text index on the Marketing.ProductDescription table to improve
the searching abilities.
The main tasks for this exercise are as follows:
1.

Create a full-text catalog.

2.

Create a full-text index on the Description column of the Marketing.ProductDescription table.

3.

Enable automatic change tracking for the index.

4.

Check to see that the population of the index is complete.

5.

Write a simple CONTAINS query.

6.

Write a CONTAINS query that searches for two words.

7.

Write a CONTAINS query that uses INFLECTIONAL forms.

f Task 1: Create a full-text catalog

Execute T-SQL statements to create a new full-text catalog called DefaultFullTextCatalog.

Ensure that the catalog is set as the default full-text catalog.

f Task 2: Create a full-text index on the Description column of the


Marketing.ProductDescription table

Execute T-SQL statements to create a full-text index on the Description column of the
Marketing.ProductDescription table.

Specify the primary key of the table as the key index.

Initially configure change tracking and population off.

f Task 3: Enable automatic change tracking for the index

Execute T-SQL statements to enable automatic change tracking for the table.

Working with Full-Text Indexes and Queries

f Task 4: Check to see that the population of the index is complete

Query the sys.fulltext_indexes system view to determine if the population is complete.

Look in particular at the has_crawl_completed column.

f Task 5: Write a simple CONTAINS query

Write a query to search for ProductDescriptionID and Description where the Description column
contains the word bottle.

f Task 6: Write a CONTAINS query that searches for two words

Write a query to search for ProductDescriptionID and Description where the Description column
contains the words, elastic and lycra.

f Task 7: Write a CONTAINS query that uses INFLECTIONAL forms

Write a query to search for ProductDescriptionID and Description where the Description column
contains inflectional forms of the word wash.

Results: After this exercise, you should have created and tested a full-text index.

Exercise 2: Implement a Stoplist


Scenario
In this exercise, you will implement a stoplist to avoid excessive unnecessary index size.
The main tasks for this exercise are as follows:
1.

Review the existing system stopwords.

2.

Create a full-text stoplist.

3.

Add words to the stoplist.

4.

View the stoplist.

f Task 1: Review the existing system stopwords

Review the existing system stopwords by querying the view sys.fulltext_system_stopwords for
language_id 1033 (English).

f Task 2: Create a full-text stoplist

Create a full-text stoplist called CommonWords.

f Task 3: Add words to the stoplist

Add the words, Bike and AdventureWorks to the stoplist for the English language.

f Task 4: View the stoplist

View the new stoplist by querying the sys.fulltext_stoplists and sys.fulltext_stopwords views.

MCT USE ONLY. STUDENT USE PROHIBITED

20-34

Results: After this exercise, you should have created a new stoplist that will help avoid excessive index
size growth.

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-35

Challenge Exercise 3: Create a stored procedure to implement a full-text


search (Only if time permits)
Scenario

In this exercise, you want to provide a more natural interface for your users. In this exercise, you will create
a stored procedure that implements a more advanced type of search.
The main tasks for this exercise are as follows:
1.

Review the supporting documentation.

2.

Design, implement, and test the stored procedure.

f Task 1: Review the supporting documentation

The supporting documentation includes specifications for the design of a new stored procedure.

f Task 2: Design, implement, and test the stored procedure

Design and implement the stored procedure. The procedure Marketing.GetRelevantDescriptions takes
an input search term and returns the specified columns from the Marketing.ProductDescription table.
Rows are returned where they are considered relevant, based on the FREETEXTTABLE function and the
English language.

Test the stored procedure using the search phrases 'strong frame' and 'serious competition'.

Results: After this exercise, you should have created a new stored procedure and tested its behavior.

Working with Full-Text Indexes and Queries

Module Review and Takeaways

Review Questions
1.

What is the function of a stopword?

2.

What are iFilters used for?

3.

What is the difference between FREETEXT and FREETEXTTABLE?

4.

How do you configure a thesaurus for use with full-text indexing?

Best Practices

MCT USE ONLY. STUDENT USE PROHIBITED

20-36

1.

Create a stoplist for your company. Add to the stoplist, any words that are used in almost all your
company documents.

2.

Use auto-population of indexes except in rare cases with specific issues. (These situations would
typically involve data that is updated at a high rate and where the index does not need to be kept
completely up to date.)

3.

Try to encourage developers in your organization to offer much more flexible user interfaces to your
end users, based on full-text indexes in SQL Server.

Course Review and Evaluation

Your evaluation of this course will help Microsoft understand the quality of your learning experience.
Please work with your training provider to access the course evaluation form.

MCT USE ONLY. STUDENT USE PROHIBITED

10776A: Developing Microsoft SQL Server 2012 Databases

20-37

Microsoft will keep your answers to this survey private and confidential and will use your responses to
improve your future learning experience. Your open and honest feedback is valuable and appreciated.

MCT USE ONLY. STUDENT USE PROHIBITED

MCT USE ONLY. STUDENT USE PROHIBITED


L1-1

Module 1: Introduction to SQL Server 2012 and its Toolset

Lab 1: Introduction to SQL Server and its


Toolset
Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

Exercise 1: Verify SQL Server Component Installation

X Task 1: Check that Database Engine and Reporting Services have been installed for
the MKTG instance
1.

Click Start, click All Programs, click Microsoft SQL Server 2012, click Configuration Tools, and
then click SQL Server Configuration Manager.

2.

In the left-hand pane of the Sql Server Configuration Manager window, click SQL Server Services.

3.

In the right-hand pane, ensure that the following services are listed for the MKTG instance:

SQL Server (MKTG)

SQL Full-text Filter Daemon Launcher (MKTG)

SQL Server Reporting Services (MKTG)

SQL Server Agent (MKTG)

X Task 2: Note the services that are installed for the default instance
1.

In the left-hand pane of the SQL Server Configuration Manager window, click SQL Server Services.

2.

In the right-hand pane, ensure that the following services are listed for the default instance:

SQL Server (MSSQLSERVER)

SQL Full-text Filter Daemon Launcher (MSSQLSERVER)

SQL Server Analysis Services (MSSQLSERVER)

SQL Server Agent (MSSQLSERVER)

X Task 3: Ensure that all required services including SQL Server Agent are started and
set to autostart for both instances
1.

Check that all the services for the MKTG instance have a Start Mode of Automatic. Ignore the SQL
Full-text Filter Daemon Launcher service at this time.

2.

Check that all the services for the default instance have a Start Mode of Automatic. Ignore the SQL
Full-text Filter Daemon Launcher service at this time.

Module 1: Introduction to SQL Server 2012 and its Toolset

Exercise 2: Alter Service Accounts for New Instance


X Task 1: Change the service account for the MKTG database engine

MCT USE ONLY. STUDENT USE PROHIBITED

L1-2

1.

In the left-hand pane of the SQL Server Configuration Manager window, click SQL Server Services.

2.

In the right-hand pane, right-click SQL Server (MTKG), and select Properties.

3.

In the Account Name text box, type AdventureWorks\PWService.

4.

In the Password text box, type Pa$$w0rd.

5.

In the Confirm Password text box, type Pa$$w0rd and click OK.

6.

In the Confirm Account Change window, click Yes.

X Task 2: Change the service account for the MKTG SQL Server Agent
1.

In the left-hand pane of the SQL Server Configuration Manager window, click SQL Server Services.

2.

In the right-hand pane, right-click SQL Server Agent (MTKG), and select Properties.

3.

In the Account Name text box, type AdventureWorks\PWService.

4.

In the Password text box, type Pa$$w0rd.

5.

In the Confirm Password text box, type Pa$$w0rd and click OK.

6.

Right-click SQL Server Agent (MTKG) and select Start to restart the service.

Exercise 3: Enable Named Pipes Protocol for Both Instances


X Task 1: Enable the named pipes protocol for the default instance
1.

In the left-hand pane of the SQL Server Configuration Manager window, expand SQL Server
Network Configuration and then click Protocols for MSSQLSERVER.

2.

In the right-hand pane, if the Named Pipes protocol is not enabled, right-click Named Pipes and
select Enable. In the Warning window, click OK.

X Task 2: Enable the named pipes protocol for the MKTG instance
1.

In the left-hand pane of the SQL Server Configuration Manager window, click Protocols for
MKTG.

2.

In the right-hand pane, if the Named Pipes protocol is not enabled, right-click Named Pipes and
select Enable. In the Warning window, click OK.

X Task 3: Restart both database engine services


1.

In the left-hand pane of the SQL Server Configuration Manager window, click SQL Server Services.

2.

Right-click SQL Server (MSSQLSERVER) and select Restart.

3.

Right-click SQL Server (MKTG) and select Restart.

4.

In the left-hand pane of the SQL Server Configuration Manager window, click SQL Server Services.

5.

In the toolbar, click the Refresh icon.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 1: Introduction to SQL Server and its Toolset L1-3

Exercise 4: Create an Alias for AdvDev


X Task 1: Create a 32-bit alias (AdvDev) for the MKTG instance
1.

In the left-hand pane of the SQL Server Configuration Manager window, expand SQL Native
Client 11.0 Configuration (32bit) and click Client Protocols.

2.

Confirm that the Named Pipes protocol is Enabled.

3.

In the left-hand pane, click Aliases.

4.

In the left-hand pane, right-click Aliases and select New Alias.

5.

In the Alias New window, in the Alias Name text box, type AdvDev.

6.

In the Protocol drop-down list box, select Named Pipes.

7.

In the Server text box, type .\MKTG and click OK.

X Task 2: Create a 64-bit alias (AdvDev) for the MKTG instance


1.

In the left-hand pane of the SQL Server Configuration Manager window, expand SQL Native
Client 11.0 Configuration and click Client Protocols.

2.

Confirm that the Named Pipes protocol is Enabled.

3.

In the left-hand pane, click Aliases.

4.

In the left-hand pane, right-click Aliases and select New Alias.

5.

In the Alias New window, in the Alias Name text box, type AdvDev.

6.

In the Protocol drop-down list box, select Named Pipes.

7.

In the Server text box, type .\MKTG and click OK.

X Task 3: Use SQL Server Management Studio to connect to the alias to ensure it works
as expected
1.

Click Start, click All Programs, click Microsoft SQL Server 2012, and then click SQL Server
Management Studio.

2.

In the Connect to Server window, ensure that Server Type is set to Database Engine.

3.

In the Server name text box, type AdvDev.

4.

In the Authentication drop-down list, select Windows Authentication, and click Connect.

5.

In Object Explorer, under AdvDev expand Databases.


Note The databases that are present include at least the following: System Databases,
Database Snapshots, ReportServer$MKTG, and ReportServer$MKTGTempDB.

6.

Close SQL Server Management Studio.

7.

Close SQL Server Configuration Manager.

Module 1: Introduction to SQL Server 2012 and its Toolset

Challenge Exercise 5: Ensure SQL Browser is Disabled and Configure a


Fixed TCP/IP Port (Only if time permits)
X Task 1: Configure the TCP port for the MKTG database engine instance to 51550

MCT USE ONLY. STUDENT USE PROHIBITED

L1-4

1.

In the Virtual Machine window, from the Start menu, click All Programs, click Microsoft SQL
Server 2012, click Configuration Tools, and then click SQL Server Configuration Manager.

2.

In the left-hand pane of the SQL Server Configuration Manager window, expand SQL Server
Network Configuration and then click Protocols for MKTG.

3.

Right-click the TCP/IP protocol and select Properties.

4.

In the TCP/IP Properties window, click IP Addresses tab.

5.

Scroll to the bottom of the screen, under the IP All section, clear the value for TCP Dynamic Ports.

6.

For TCP Port, type 51550, and click OK.

7.

In the Warning window, click OK.

8.

In the left-hand pane, click SQL Server Services.

9.

Right-click SQL Server (MKTG) and select Restart.

10. In the left-hand pane of the SQL Server Configuration Manager window, click SQL Server Services.
11. In the toolbar, click the Refresh icon and make sure the service starts.

X Task 2: Disable the SQLBrowser service


1.

In the left-hand pane of the SQL Server Configuration Manager window, click SQL Server Services.

2.

In the right-hand pane, if the state for the SQL Server Browser is Running, right-click SQL Server
Browser and click Stop.

3.

Right-click the SQL Server Browser and click Properties.

4.

In the SQL Server Browser Properties window, in the Service tab, set the Start Mode to Disabled
and click OK.

5.

Close SQL Server Configuration Manager.

MCT USE ONLY. STUDENT USE PROHIBITED


L2-5

Module 2: Working with Data Types

Lab 2: Working with Data Types


Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_02_PRJ\10776A_02_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Choosing Appropriate Data Types


X Task 1: Determine column names and data types

Review the supporting documentation for details of the PhoneCampaign, Opportunity and
SpecialOrder tables and determine column names, data types, and nullability for each data item in
the design.
Note With any design exercise, there is no one correct answer. A sample solution has
been provided below.

Table 1: PhoneCampaign
Description

Column Name and Data Type

Which campaign this relates to.

PhoneCampaignID INT NOT NULL

The prospect that was contacted.

ProspectID INT NOT NULL

When contact was first attempted with the


prospect.

FirstAttemptedContact DATETIME NULL

Comments related to the contact that was


made, if it was made.

ContactComments NVARCHAR(MAX) NULL

When contact was actually made with the


prospect.

InitialContact DATETIME NULL

Outcome of the contact: sale, later follow-up or


no interest

ContactOutcomeCode CHAR(1) NULL

Value of any sale made (up to 2 decimal places)

SalesValue DECIMAL(10,2) NULL

Module 2: Working with Data Types

Table 2: Opportunity
Description

Column Name and Data Type

Name of the opportunity

OpportunityID INT NOT NULL

Which prospect this opportunity relates to

ProspectID INT NOT NULL

Stage the sale is at: Lead, Qualification,


Proposal Development, Contract
Negotiations, Complete, Lost

SalesStageCode CHAR(2) NOT NULL

Date that the opportunity was raised

DateRaised DATETIME NOT NULL (Note: if


only the date is required with no time
component, then the DATE data type could
well have been used instead)

Probability of success

Likelihood TINYINT NOT NULL

Rating: Cold, Warm, Hot

Rating CHAR(1) NOT NULL

Estimated closing date

EstimatedClosingDate DATE NOT NULL

Estimated revenue

EstimatedRevenue DECIMAL(10,2) NOT NULL

Delivery address

DeliveryAddress NVARCHAR(MAX) NOT


NULL

Table 3: SpecialOrder
Description

Column Name and Data Type

Which prospect this order is for

ProspectID INT NOT NULL

External supplier of the item

SupplierID INT NOT NULL

Description of the item

ItemDescription NVARCHAR(100) NOT NULL

Quantity Required (some quantities are


whole numbers, some are fractional with up
to 3 decimal places)

QuantityRequired DECIMAL(10,3) NOT NULL

Date of order

OrderDate DATETIME NOT NULL (Note: if only


the date is required with no time component,
then the DATE data type could well have been
used instead)

Promised delivery date

PromisedDeliveryDate DATE NOT NULL

Actual delivery date

ActualDeliveryDate DATE NULL

Special requirements (any comments related


to the special order)

SpecialRequirements NVARCHAR(MAX) NULL

Quoted price per unit (up to 2 decimal


places)

QuotedPricePerUnit DECIMAL(10,2) NOT


NULL

MCT USE ONLY. STUDENT USE PROHIBITED

L2-6

Exercise 2: Writing Queries With Data Type Conversions


X Task 1: Connect to the MarketDev Database
1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 2: Working with Data Types

L2-7

X Task 2: Review the first query requirement and write a SELECT statement to meet the
requirement
1.

Review the supporting documentation for details for the first query requirement.

2.

In Object Explorer, expand the Databases under Proseware.

3.

Right-click the MarketDev database and click New Query.

4.

Type the query below in the query pane:


SELECT ProductID,
ProductName,
CONVERT(varchar(8),SellEndDate,112) AS SellEndDate
FROM Marketing.Product;
GO

5.

In the toolbar, click Execute.

6.

Ensure that the results from the query match the required output as shown in the supporting
documentation.

X Task 3: Review the second query requirement and write a SELECT statement to meet
the requirement
1.

Review the supporting documentation for details for the second query requirement.

2.

In Object Explorer, expand the Databases under Proseware.

3.

Right-click the MarketDev database and click New Query.

4.

Type the query below in the query pane:


SELECT ProspectID,
CAST(Demographics AS nvarchar(1000)) AS Demographics
FROM Marketing.Prospect;
GO

5.

In the toolbar, click Execute.

6.

Ensure that the results from the query match the required output as shown in the supporting
documentation.

Module 2: Working with Data Types

Challenge Exercise 3: Designing and Creating Alias Data Types (Only if


time permits)
X Task 1: Investigate the storage of phone numbers and email addresses
1.

In the Object Explorer, expand Databases, and expand MarketDev, and expand Tables.

2.

In each table, expand Columns, noting any columns that are holding details of Phone Numbers or
Email Addresses. The following columns should be identified:
Table

Phone Number Columns

Definition

Marketing.Prospect

CellPhoneNumber

NVARCHAR(20)

Marketing.Prospect

HomePhoneNumber

NVARCHAR(25)

Marketing.Prospect

WorkPhoneNumber

VARCHAR(22)

Email Address Columns

Definition

Table
Marketing.Prospect

EmailAddress

NVARCHAR(100)

Marketing.SalesPerson

EmailAlias

NVARCHAR(256)

X Task 2: Create a data type that stores phone numbers


1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


CREATE TYPE PhoneNumber
FROM nvarchar(25);
GO

4.

In the toolbar, click Execute.

X Task 3: Create a data type that stores email addresses


1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


CREATE TYPE EmailAddress
FROM nvarchar(256);
GO

4.

In the toolbar, click Execute

MCT USE ONLY. STUDENT USE PROHIBITED

L2-8

MCT USE ONLY. STUDENT USE PROHIBITED


L3-9

Module 3: Designing and Implementing Tables

Lab 3: Designing and Implementing Tables


Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_03_PRJ\10776A_03_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Improve the Design of Tables


X Task 1: Review the supplied design

Review the supplied design in the supporting documentation for the exercise.

X Task 2: Suggest an improved design


Note With any design exercise, there is no one correct answer. A sample solution has
been provided below.

Table1: Competitor
Name

Data Type

Design Rationale

CompetitorID

INT
NOT NULL

Prefer numeric code for surrogate keys

CompetitorName

NVARCHAR(30)
NOT NULL

The column name Name is too generic


Unicode data type for flexibility

StreetAddress

NVARCHAR(max)
NOT NULL

Common to have multiple address lines


Unicode data type for flexibility

DateEntered

DATE
NOT NULL

Removed underscore for consistency


Chose appropriate data type

Module 3: Designing and Implementing Tables

(continued)
Name

Data Type

Design Rationale

MCT USE ONLY. STUDENT USE PROHIBITED

L3-10

StrengthOfCompe
tition

NVARCHAR(8)
NOT NULL

Renamed for consistency


Note: Suspect this should be a code or a number
but would require more information from Business
Analyst
Unicode data type for flexibility

Comments

NVARCHAR(max) NULL

Unicode data type for flexibility


NULL because there may be no comments

Table2A: City (Duplicated City details extracted from the original table called
TVAdvertisements)
Name

Data Type

Design Rationale

CityID

INT
NOT NULL

Added a surrogate key

CityName

NVARCHAR(25) NOT NULL

Renamed original column


Retained data type where no further information
is available

Table2B: TVStation (Duplicated TV Station details extracted from the original


table called TVAdvertisements)
Name

Data Type

Design Rationale

TVStationID

INT
NOT NULL

Added a surrogate key

TVStationName

NVARCHAR(15)
NOT NULL

Renamed original column


Retained data type where no further information
is available

CityID

INT
NOT NULL

City details extracted to a separate table

CostPerAdvertisement

DECIMAL(12,2)
NOT NULL

Chose appropriate data type


TotalCostOfAllAdvertisements removed as it
should be calculated

NumberOfAdvertisements removed as it should


be calculated

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 3: Designing and Implementing Tables

L3-11

Table2C: TVAdvertisement (Extracted repeating groups from the original table


called TVAdvertisements)
Name

Data Type

Design Rationale

TVAdvertisementID

INT
NOT NULL

Added a surrogate key

TVStationID

INT
NOT NULL

Reference to TV Station table

ScreeningTime

DATETIME
NOT NULL

Single data type to hold both values


Name to reflect combination of values

Table3: CampaignResponse
Name

Data Type

Design Rationale

TVAdvertisementID

INT
NOT NULL

Added a surrogate key

ResponseReceived

DATETIME
NOT NULL

Improved column name

ProspectID

INT
NOT NULL

Reference to existing Prospect table

ResponseMethodCode

CHAR(1)
NOT NULL

Changed to a straightforward code using first letter


An alternative would be to extract the response
method as another lookup table

ChargeFromReferrer

DECIMAL(12,2)
NOT NULL

Chose appropriate data type

RevenueReceived

DECIMAL(12,2)
NOT NULL

Chose appropriate data type

ResponseProfit should be removed as it should be


calculated rather than stored in the table

Exercise 2: Create a Schema


X Task 1: Connect to the MarketDev Database
1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

X Task 2: Create a schema named DirectMarketing


1.

Type the query below in the query pane:


CREATE SCHEMA DirectMarketing AUTHORIZATION dbo;
GO

2.

In the toolbar, click Execute.

Module 3: Designing and Implementing Tables

Challenge Exercise 3: Create the Tables (Only if time permits)


X Task 1: Create the tables
1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


Note The sample query below is based on the sample solution. Your query should be
based on your design. The solution shown below is contained in the 71 Lab Exercise 3.sql
script file in Solution Explorer.
CREATE TABLE DirectMarketing.Competitor
( CompetitorID INT NOT NULL PRIMARY KEY,
CompetitorName NVARCHAR(30) NOT NULL,
StreetAddress NVARCHAR(MAX) NOT NULL,
DateEntered DATE NOT NULL,
StrengthOfCompetition NVARCHAR(8) NOT NULL,
Comments NVARCHAR(MAX) NULL
);
GO
CREATE TABLE DirectMarketing.City
( CityID INT NOT NULL PRIMARY KEY,
CityName NVARCHAR(25) NOT NULL
);
GO
CREATE TABLE DirectMarketing.TVStation
( TVStationID INT NOT NULL PRIMARY KEY,
TVStationName NVARCHAR(15) NOT NULL,
CityID INT NOT NULL,
CostPerAdvertisement DECIMAL(12,2) NOT NULL
);
GO
CREATE TABLE DirectMarketing.TVAdvertisement
( TVAdvertisementID INT NOT NULL PRIMARY KEY,
TVStationID INT NOT NULL,
ScreeningTime DATETIME NOT NULL
);
GO
CREATE TABLE DirectMarketing.CampaignResponse
( CampaignResponseID INT NOT NULL PRIMARY KEY,
ResponseReceived DATETIME NOT NULL,
ProspectID INT NOT NULL,
ResponseMethodCode CHAR(1) NOT NULL,
ChargeFromReferrer DECIMAL(12,2) NOT NULL,
RevenueReceived DECIMAL(12,2) NOT NULL
);
GO

4.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

L3-12

MCT USE ONLY. STUDENT USE PROHIBITED


L4-13

Module 4: Ensuring Data Integrity through Constraints

Lab 4: Ensuring Data Integrity through


Constraints
Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click SQL
Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_04_PRJ\10776A_04_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Constraint Design


X Task 1: Review the supporting documentation

Review the supplied table design requirements in the supporting documentation.

X Task 2: Alter the Marketing.Yield table


1.

In Object Explorer, expand the Proseware server, expand Databases, right-click the MarketDev
database and click New Query.

2.

Type the query below in the query pane (note that the query continues to the next page):
ALTER TABLE Marketing.Yield
ALTER COLUMN ProspectID int NOT NULL;
GO
ALTER TABLE Marketing.Yield
ALTER COLUMN LanguageID nchar(6) NOT NULL;
GO
ALTER TABLE Marketing.Yield
ALTER COLUMN YieldOutcome int NOT NULL;
GO
ALTER TABLE Marketing.Yield
ALTER COLUMN RowID uniqueidentifier NOT NULL;
GO

(code continued on following page)

Module 4: Ensuring Data Integrity through Constraints

ALTER TABLE Marketing.Yield


ALTER COLUMN LastUpdate datetime2 NOT NULL;
GO
ALTER TABLE Marketing.Yield
ADD CONSTRAINT PK_Yield PRIMARY KEY CLUSTERED (ProspectID,
LanguageID);
GO
ALTER TABLE Marketing.Yield
ADD CONSTRAINT FK_Yield_Prospect
FOREIGN KEY (ProspectID) REFERENCES Marketing.Prospect(ProspectID);
GO
ALTER TABLE Marketing.Yield
ADD CONSTRAINT FK_Yield_Language
FOREIGN KEY (LanguageID) REFERENCES Marketing.Language(LanguageID);
GO
ALTER TABLE Marketing.Yield
ADD CONSTRAINT DF_Yield_YieldOutcome
DEFAULT (0) FOR YieldOutcome;
GO
ALTER TABLE Marketing.Yield
ADD CONSTRAINT DF_Yield_RowID
DEFAULT (NEWID()) FOR RowID;
GO
ALTER TABLE Marketing.Yield
ADD CONSTRAINT DF_Yield_LastUpdate
DEFAULT (SYSDATETIME()) FOR LastUpdate;
GO

3.

In the toolbar, click Execute.

Challenge Exercise 2: Test the constraints (Only if time permits)


X Task 1: Test the default values and data types
1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


INSERT INTO Marketing.Yield (ProspectID, LanguageID, Notes)
VALUES (1,'en', 'Excellent outcome');
SELECT * FROM Marketing.Yield;
GO

3.

In the toolbar, click Execute.


Note

This should execute without errors.

MCT USE ONLY. STUDENT USE PROHIBITED

L4-14

X Task 2: Test the primary key


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


INSERT INTO Marketing.Yield (ProspectID, LanguageID, Notes)
VALUES (1,'en', 'Another excellent outcome');
GO

3.

In the toolbar, click Execute.


Note This should fail due to the primary key constraint.

X Task 3: Test the foreign key reference on language


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


INSERT INTO Marketing.Yield (ProspectID, LanguageID, Notes)
VALUES (2,'ex', 'Excellent outcome');
GO

3.

In the toolbar, click Execute.


Note This should fail due to the foreign key reference.

X Task 4: Test the foreign key reference on prospect


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


INSERT INTO Marketing.Yield (ProspectID, LanguageID, Notes)
VALUES (29292,'en', 'Excellent outcome');
GO

3.

In the toolbar, click Execute.


Note This should fail due to the foreign key reference.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 4: Ensuring Data Integrity through Constraints

L4-15

Module 5: Planning for SQL Server 2012 Indexing

Lab 5: Planning for SQL Server Indexing


Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_05_PRJ\10776A_05_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Explore existing index statistics


X Task 1: Execute SQL Command
1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


SELECT * FROM sys.stats WHERE object_id = OBJECT_ID('Marketing.Product');
GO

4.

In the toolbar, click Execute.

X Task 2: Review the results

MCT USE ONLY. STUDENT USE PROHIBITED

L5-16

1.

In the Query Analyzer Results pane, review the results.

2.

Check to see if any autostats has been generated. If any autostats have been created, they will appear
in the results with an _WA prefix.

X Task 3: Create statistics


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE STATISTICS Product_Color_Stats ON Marketing.Product (Color)
WITH FULLSCAN;
GO

3.

In the toolbar, click Execute.

X Task 4: Re-execute the SQL command from task 1


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


SELECT * FROM sys.stats WHERE object_id = OBJECT_ID('Marketing.Product');
GO

3.

In the toolbar, click Execute.

X Task 5: Use DBCC SHOW_STATISTICS


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


DBCC SHOW_STATISTICS('Marketing.Product',Product_Color_Stats);
GO

3.

In the toolbar, click Execute.

X Task 6: Answer questions

Complete the answer column in the table below.


Note The results returned can vary. Sample results are shown in the following table.
Question

Answer

How many rows were sampled?

504

How many steps were created?

10

What was the average key length?

5.178571

How many Black products are there?

93

X Task 7: Execute SQL Command and check accuracy of statistics


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


SELECT COUNT(1) FROM Marketing.Product WHERE Color = 'Black';
GO

3.

In the toolbar, click Execute.

X Task 8: Calculate Selectivity of each query


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


-- Calculate the total number of rows in the table
SELECT COUNT(1) FROM Marketing.Prospect;
GO

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 5: Planning for SQL Server Indexing

L5-17

Module 5: Planning for SQL Server 2012 Indexing

3.

In the toolbar, click Execute.


Note

4.

A sample result would be 19955

Type the query below in the same query pane:


-- Query 1
SELECT ProspectID, FirstName, LastName FROM Marketing.Prospect WHERE FirstName LIKE
'A%';
GO

5.

Highlight only Query 1 and click Execute.

6.

Calculate the selectivity of the query.


Note

7.

A sample result would be 2013 / 19955 or approximately 10.1%

Type the query below in the same query pane:


-- Query 2
SELECT ProspectID, FirstName, LastName FROM Marketing.Prospect WHERE FirstName LIKE
'Alejandro%';
GO

8.

Highlight only Query 2 and click Execute.

9.

Calculate the selectivity of the query.


Note

A sample result would be 48 / 19955 or approximately 0.2%

10. Type the query below in the same query pane:


-- Query 3
SELECT ProspectID, FirstName, LastName FROM Marketing.Prospect WHERE FirstName LIKE
'Arif%';
GO

11. Highlight only Query 3 and click Execute.


12. Calculate the selectivity of the query.
Note

A sample result would be 1 / 19955 or approximately 0.0%

Challenge Exercise 2: Design column orders for indexes (Only if time


permits)
X Task 1: Design an index

MCT USE ONLY. STUDENT USE PROHIBITED

L5-18

Review the supporting documentation for Query 1, the columns and column order is detailed in the
table below:
Columns
ProspectID

Column Order
1

Rationale: Selecting by ProspectID only and no output order

X Task 2: Design an index

L5-19

Review the supporting documentation for Query 2, the columns and column order is detailed in the
table below:
Columns
FirstName

Column Order
1

Rationale: Selecting by FirstName only and no output order

X Task 3: Design an index

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 5: Planning for SQL Server Indexing

Review the supporting documentation for Query 3, the columns and column order is detailed in the
table below:
Columns

Column Order

FirstName

LastName

Rationale: Selecting only a single FirstName. Adding LastName to the index will supply the correct
output order

X Task 4: Design an index

Review the supporting documentation for Query 4, the columns and column order is detailed in the
table below:
Columns

Column Order

FirstName

LastName

Rationale: Selecting a range of FirstNames could have varying selectivity. In this case, the selectivity
of the FirstName column is sufficient to warrant the index on the FirstName. Note that if the
constant was an A rather than an S, the index would be better defined as LastName, FirstName
instead

X Task 5: Design an index

Review the supporting documentation for Query 5, the columns and column order is detailed in the
table below:
Columns
LanguageID

Column Order
1

Rationale: Grouping by LanguageID. Note that in this case an alternate output order would not
change the recommendation

Module 6: Implementing Table Structures in SQL Server


2012

Lab 6: Implementing Table Structures in


SQL Server
Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_06_PRJ\10776A_06_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Creating Tables as Heaps


X Task 1: Review the Requirements

Review the supporting documentation for Tables 1 and 2.

X Task 2: Create the Tables in the MarketDev database


1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


CREATE TABLE Relationship.ActivityLog
( ActivityTime datetimeoffset,
SessionID int,
Duration int,
ActivityType int
);
GO
CREATE TABLE Relationship.PhoneLog
( PhoneLogID int PRIMARY KEY NONCLUSTERED,
SalespersonID int,
CalledPhoneNumber nvarchar(16),
CallDurationSeconds int
);
GO

4.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

L6-20

Exercise 2: Creating Tables with Clustered Indexes


X Task 1: Review the Requirements

Review the supporting documentation for Tables 3 and 4.

X Task 2: Create the Tables in the MarketDev database


1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


CREATE TABLE Relationship.MediaOutlet
( MediaOutletID int PRIMARY KEY CLUSTERED,
MediaOutletName nvarchar(40),
PrimaryContact nvarchar(50),
City nvarchar(50)
);
GO
CREATE TABLE Relationship.PrintMediaPlacement
( PrintMediaPlacementID int PRIMARY KEY CLUSTERED,
MediaOutletID int,
PlacementDate datetime,
PublicationDate datetime,
RelatedProductID int,
PlacementCost decimal(18,2)
);
GO

4.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 6: Implementing Table Structures in SQL Server

L6-21

Challenge Exercise 3: Comparing the Performance of Clustered Indexes vs.


Heaps (Only if time permits)
X Task 1: Review the Table Design

Review the supporting documentation for Table 5.

X Task 2: Create the Relationship.Table_Heap Table


1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


CREATE TABLE Relationship.Table_Heap
( ApplicationID int IDENTITY(1,1),
ApplicantName nvarchar(150),
EmailAddress nvarchar(100),
ReferenceID uniqueidentifier,
Comments nvarchar(500)
);
GO

4.

In the toolbar, click Execute.

Module 6: Implementing Table Structures in SQL Server 2012

X Task 3: Create the Relationship.Table_ApplicationID Table


1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


CREATE TABLE Relationship.Table_ApplicationID
( ApplicationID int IDENTITY(1,1),
ApplicantName nvarchar(150),
EmailAddress nvarchar(100),
ReferenceID uniqueidentifier,
Comments nvarchar(500)
);
GO
CREATE CLUSTERED INDEX IX_ApplicantID
ON Relationship.Table_ApplicationID (ApplicationID);
GO

4.

In the toolbar, click Execute.

X Task 4: Create the Relationship.Table_EmailAddress Table


1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


CREATE TABLE Relationship.Table_EmailAddress
( ApplicationID int IDENTITY(1,1),
ApplicantName nvarchar(150),
EmailAddress nvarchar(100),
ReferenceID uniqueidentifier,
Comments nvarchar(500)
);
GO
CREATE CLUSTERED INDEX IX_EmailAddress
ON Relationship.Table_EmailAddress (EmailAddress);
GO

4.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

L6-22

X Task 5: Create the Relationship.Table_ReferenceID Table


1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


CREATE TABLE Relationship.Table_ReferenceID
( ApplicationID int IDENTITY(1,1),
ApplicantName nvarchar(150),
EmailAddress nvarchar(100),
ReferenceID uniqueidentifier,
Comments nvarchar(500)
);
GO
CREATE CLUSTERED INDEX IX_ReferenceID
ON Relationship.Table_ReferenceID (ReferenceID);
GO

4.

In the toolbar, click Execute.

X Task 6: Load and Execute the Workload Script


1.

In Solution Explorer, double-click the file 72 Lab Exercise 3 Workload Script.sql.

2.

In the toolbar, click Execute.


Note This may take approximately five minutes to complete, depending on available
hardware resources. You can check where it is up to by periodically switching to the
Messages tab. A message is printed as each of the four sections is completed. While the
script is running, review the contents of the script and estimate the proportion of time
difference you expect to see in the results.

X Task 7: Compare Table Performance

Compare the performance of each table structure as reported by the results of executing the
Workload script.
Note Results will vary substantially depending upon the hardware the Virtual Machine is
running on. Sample results are shown below.
HeapTime
23 seconds

ApplicationIDTime
21 seconds

EmailAddressTime

ReferenceIDTime

91 seconds

134 seconds

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 6: Implementing Table Structures in SQL Server

L6-23

Module 7: Reading SQL Server 2012 Execution Plans

Lab 7: Reading SQL Server Execution Plans


Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_07_PRJ\10776A_07_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Actual vs. Estimated Plans


X Task 1: Load the test script
1.

In Solution Explorer, double-click the file

2.

51 Lab Exercise 1.sql.

Highlight the following section of the script:


USE AdventureWorks;
GO

3.

In the toolbar, click Execute.

X Task 2: Generate an estimated execution plan for script 7.1


1.

Highlight script 7.1.

2.

From the Query menu, select Display Estimated Execution Plan.


Note

A Table Scan operator was used.

X Task 3: View the estimated execution plan for script 7.2 using SHOWPLAN_XML
1.

Highlight script 7.2.

2.

In the toolbar, click Execute.

3.

In the Results pane, click on the XML that is returned to view the execution plan.

4.

In the Results pane, right-click in the whitespace in the plan, click Show Execution Plan XML.

MCT USE ONLY. STUDENT USE PROHIBITED

L7-24

5.

In the ExecutionPlan1.xml tab, review the XML.


Note The XML is complex and it includes a large amount of detail that is not immediately
obvious from the graphical plan.

6.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 7: Reading SQL Server Execution Plans

L7-25

Close the XML window and the execution plan window. If prompted to save changes, do not save the
changes.

X Task 4: Generate the actual execution plan for script 7.3


1.

In the Query menu, click Include Actual Execution Plan.

2.

Highlight script 7.3.

3.

In the toolbar, click Execute.

4.

Click on the Execution Plan pane.


Note The plan is identical to the estimated plan from the previous task.

X Task 5: Try to generate an estimated execution plan for script 7.4


1.

Highlight script 7.4.

2.

From the Query menu, select Display Estimated Execution Plan.


Note An Estimated Execution Plan cannot be created. Note the reason (invalid object
name) for the inability to create a plan in the messages tab.

X Task 6: Review the actual execution plan for script 7.4


1.

Highlight script 7.4 again.

2.

In the toolbar, click Execute.

3.

Review the Execution Plan that has been returned.


Note The returned plan shows the INSERT and SELECT statements but not the CREATE
and DROP statements.

X Task 7: Review the execution plans currently cached in memory using script 7.5
1.

Highlight script 7.5.

2.

In the toolbar, click Execute.

3.

Review the results being returned.


Note The results returned will vary from system to system. Browse through the rows in the
Results tab to gain an understanding of the types of plans currently in memory.

Module 7: Reading SQL Server 2012 Execution Plans

Exercise 2: Identify Common Plan Elements


X Task 1: Load the test script
1.

In Solution Explorer, double-click the file

2.

61 Lab Exercise 2.sql.

Highlight the query as shown in the script below:


USE AdventureWorks;
GO

3.

In the toolbar, click Execute.

4.

From the Query menu click Include Actual Execution Plan.

X Task 2: Explain the actual execution plan from script 7.6


1.

Highlight script 7.6.

2.

In the toolbar, click Execute.

3.

Explain the results being returned.


Note The table has a clustered index and the entire table is being read.

X Task 3: Explain the actual execution plan from script 7.7


1.

Highlight script 7.7.

2.

In the toolbar, click Execute.

3.

Explain the results being returned.


Note The table has a clustered index and a specific clustering key is being used to seek.

X Task 4: Explain the actual execution plan from script 7.8


1.

Highlight script 7.8.

2.

In the toolbar, click Execute.

3.

Explain the results being returned.


Note The table has a clustered index and the entire table is being read. The rows are then
sorted by Shelf.

X Task 5: Explain the actual execution plan from script 7.9


1.

Highlight script 7.9.

2.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

L7-26

3.

Explain the results being returned.


Note The table is structured as a heap. The table has a nonclustered index on
DatabaseLogID that is being used to locate the row required. Because the entire row is then
required, a lookup is being made into the heap.

X Task 6: Explain the actual execution plan from script 7.10


1.

Highlight script 7.10.

2.

In the toolbar, click Execute.

3.

Explain the results being returned.


Note The table has a clustered index. There is a nonclustered index that is sufficiently
selective to be useful in locating entries for the given StateProvinceID. After the rows are
located by that index, lookups to the clustered index need to be performed because not all
columns that are required for the query are contained within the nonclustered index.

X Task 7: Explain the actual execution plan from script 7.11


1.

Highlight script 7.11.

2.

In the toolbar, click Execute.

3.

Explain the results being returned. Compare this plan to the one returned in script 7.10.

4.

The reason why the plans are different can be attributed to


Note SQL Server has chosen to scan an entire index as it covers the query. The rows with
the correct StateProvinceID are being selected from that index. Note the Missing Index hint
that appears.

X Task 8: Explain the actual execution plan from script 7.12


1.

Highlight script 7.12.

2.

In the toolbar, click Execute.

3.

Explain the results being returned.


Note SQL Server has decided to read an entire index that contains the City column rather
than reading the entire table. As the index is not in City order, it then needs to execute a
hash match aggregate operation to calculate the required aggregate.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 7: Reading SQL Server Execution Plans

L7-27

Module 7: Reading SQL Server 2012 Execution Plans

X Task 9: Explain the actual execution plan from script 7.13


1.

Highlight script 7.13.

2.

In the toolbar, click Execute.

3.

Explain the results being returned. Compare this plan to the one returned in script 7.12.

4.

The reason why the plans are different can be attributed to


Note There is a nonclustered index that is in StateProvinceID order. SQL Server has chosen
to scan that index. As it is already in StateProvinceID order, a stream aggregate operation
was able to be performed.

X Task 10: Explain the actual execution plan from script 7.14
1.

Highlight script 7.14.

2.

In the toolbar, click Execute.

3.

Note the difference in this plan from the plan for script 7.12.
Note This query is identical to the one in script 7.12 with the addition of the HAVING
clause. That clause has caused SQL Server to add a filter operation.

Challenge Exercise 3: Query Cost Comparison (Only if time permits)


X Task 1: Load the test script
1.

In Solution Explorer, double-click the file

2.

71 Lab Exercise 3.sql.

Highlight the query as shown in the script below:


USE AdventureWorks;
GO

3.

In the toolbar, click Execute.

4.

From the Query menu click Include Actual Execution Plan.

X Task 2: Explain the actual execution plan from script 7.15


1.

Highlight script 7.15.

2.

In the toolbar, click Execute.

3.

Explain the results being returned.


Note Even though the two queries are structured quite differently, they are requesting
exactly the same results in the same order. It is then logical that SQL Server executes the
same plan for both queries.

MCT USE ONLY. STUDENT USE PROHIBITED

L7-28

MCT USE ONLY. STUDENT USE PROHIBITED


L8-29

Module 8: Improving Performance through Nonclustered


Indexes

Lab 8: Improving Performance through


Nonclustered Indexes
Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_08_PRJ\10776A_08_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Nonclustered index usage review


X Task 1: Review the query

Review the supporting documentation for Query 1.

X Task 2: Review the existing Index and Table structures


1.

In Object Explorer, expand Proseware, expand Databases, expand MarketDev, expand Tables, and
expand Marketing.WebLog.

2.

Review existing indexes and table structures for this table.


Note Clustered Primary Key on WebLogID and Non-Clustered index on SessionStart.

X Task 3: Design and implement a more appropriate index


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE INDEX IX_WebLog_Perf_20100830_A
ON Marketing.WebLog (SessionStart)
INCLUDE (SessionID, ServerID, UserName);
GO

3.

In the toolbar, click Execute.

Module 8: Improving Performance through Nonclustered Indexes

X Task 4: Test your design


1.

Right-click the MarketDev database and click New Query.

2.

From the Query menu, click Include Actual Execution Plan.

3.

Type the query below in the query pane:


DECLARE @StartTime datetime2 = '2010-08-30 16:27';
SELECT TOP(5000) wl.SessionID, wl.ServerID, wl.UserName
FROM Marketing.WebLog AS wl
WHERE wl.SessionStart >= @StartTime
ORDER BY wl.SessionStart, wl.ServerID;

4.

In the toolbar, click Execute.

Exercise 2: Improving nonclustered index designs


Task 1: Review the index design

Review the supporting documentation for Query 2.

X Task 2: Implement the index


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE INDEX IX_WebLog_Perf_20100830_B
ON Marketing.WebLog (ServerID, SessionStart)
INCLUDE (SessionID, UserName);
GO

3.

In the toolbar, click Execute.

X Task 3: Test the design and explain why the index was not used
1.

Right-click the MarketDev database and click New Query.

2.

From the Query menu, click Include Actual Execution Plan.

3.

Type the query below in the query pane:


DECLARE @StartTime datetime2 = '2010-08-30 16:27';
SELECT TOP(5000) wl.SessionID, wl.ServerID, wl.UserName
FROM Marketing.WebLog AS wl
WHERE wl.SessionStart >= @StartTime
ORDER BY wl.SessionStart, wl.ServerID;
GO

4.

In the toolbar, click Execute.


Note The structure of the index does not support efficient selection of rows or ordering of
results.

MCT USE ONLY. STUDENT USE PROHIBITED

L8-30

Exercise 3: SQL Server Profiler and Database Engine Tuning Advisor


X Task 1: Review the query

Review the supporting documentation for Query 3.

X Task 2: Review the existing Index and Table structures

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 8: Improving Performance through Nonclustered Indexes

L8-31

1.

In Object Explorer, expand Proseware, expand Databases, expand MarketDev, expand Tables, and
expand Marketing.PostalCode.

2.

Review existing indexes and table structures for this table.


Note Clustered index on Country, PostalCode.

X Task 3: Design a more appropriate index by following the Missing Index suggestion
1.

Right-click the MarketDev database and click New Query.

2.

From the Query menu, click Include Actual Execution Plan.

3.

Type the query below in the query pane:


SELECT PostalCode, Country
FROM Marketing.PostalCode
WHERE StateCode = 'KY'
ORDER BY StateCode, PostalCode;
GO

4.

In the toolbar, click Execute.

5.

In the Execution Plan tab, note the missing index suggestion in green.

6.

Right-click in whitespace in the middle of the graphical execution plan and choose Missing Index
Details.
Note Review the suggested index design.

7.

Close the Missing Index Details query window.

8.

Type the query below in the previous query pane:


CREATE NONCLUSTERED INDEX IX_PostalCode_Perf_20100830_A
ON Marketing.PostalCode (StateCode)
INCLUDE (Country,PostalCode);
GO

9.

Highlight the above query, click Execute.

10. Highlight the previous query as shown below in the query pane:
SELECT PostalCode, Country
FROM Marketing.PostalCode
WHERE StateCode = 'KY'
ORDER BY StateCode, PostalCode;
GO

11. In the toolbar, click Execute.

Module 8: Improving Performance through Nonclustered Indexes

12. In the Execution Plan tab, note that the missing index suggestion has now disappeared.
Note The query is now utilizing the new index.
13. Leave the query pane open for the next task.

MCT USE ONLY. STUDENT USE PROHIBITED

L8-32

X Task 4: Create a better index that removes the sort operation. If you create another
index, confirm that SQL Server selects it
1.

Type the query below in the query pane from the previous task:
CREATE NONCLUSTERED INDEX IX_PostalCode_Perf_20100830_B
ON Marketing.PostalCode (StateCode, PostalCode)
INCLUDE (Country);
GO

2.

Highlight the above query, click Execute.

3.

Highlight the previous query as shown below in the query pane:


SELECT PostalCode, Country
FROM Marketing.PostalCode
WHERE StateCode = 'KY'
ORDER BY StateCode, PostalCode;
GO

4.

In the toolbar, click Execute.

5.

Click on the Execution Plan tab.


Note The sort operation is now unnecessary and the new index is now utilized.

Challenge Exercise 4: Nonclustered index design (Only if time permits)


X Task 1: Open SQL Server Profiler and configure and start a trace
1.

In SQL Server Management Studio, in the Tools menu, click SQL Server Profiler.

2.

In the Connect to Server window, type Proseware in the Server name text box.

3.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

4.

In the Trace Properties window, type Module08Trace in the Trace Name textbox.

5.

In the Use the Template drop down list box, select Tuning.

6.

Click Save to file checkbox.

7.

In the Save As window, click Desktop in the left-hand pane under Favorites and click Save.

8.

Uncheck the Enable File Rollover checkbox.

9.

In the Set maximum file size(MB) textbox, type 500.

10. Click Events Selection tab, and click Column Filters.


11. In the Edit Filter pane, click the DatabaseName column header.
12. In the DatabaseName pane, expand the Like node, type MarketDev, and click OK.

13. In the Trace Properties window, click Run.


14. In the Window menu, ensure that AutoScroll is not selected.

X Task 2: Load and execute the workload file


1.

In SQL Server Management Studio, double-click 81 Lab Exercise 4.sql in Solution Explorer.

2.

In the toolbar, click Execute.

3.

Wait for the query to complete.


Note The query will take approximately 3 minutes to execute, depending upon the
available hardware resources.

X Task 3: Stop and analyze the trace using DETA

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 8: Improving Performance through Nonclustered Indexes

L8-33

1.

In SQL Server Profiler, from the File menu, click Stop Trace.

2.

From the Tools menu, click Database Engine Tuning Advisor.

3.

In the Connect to Server window, click Connect.

4.

Maximize the Database Engine Tuning Advisor window.

5.

In the Workload group box, ensure that File is selected and click the Browse for a Workload File
icon (the icon is the first icon to the right of the text box in the Workload group box).

6.

Browse to the Desktop folder, select the Module08Trace.trc file and click Open.

7.

In the Database for workload analysis dropdown, select MarketDev.

8.

In the Select databases and tables to tune list, check the MarketDev database.

9.

Click the Tuning Options tab and click Advanced Options.

10. In the Advanced Tuning Options window, check the Define max space for recommendations
(MB) checkbox and enter 500 for the size and click OK.
11. From the toolbar, click the Start Analysis.
Note Observe the tuning progress and when the analysis is complete, note the
recommendations. The exact recommendations you see will depend upon how long you ran
the query, etc. but it is likely you will see a recommendation for two new indexes and a
substantial estimated performance improvement.
12. On the Recommendations tab, note the Estimated improvement.

13. Scroll the Index Recommendations output to the right and note the hyperlinks to sample code.
14. Click each of the recommendations in the Definition column turn and note the suggested index
structures.
15. Close DTA.
16. Close SQL Server Profiler.

Module 9: Designing and Implementing Views

Lab 9: Designing and Implementing Views


Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_09_PRJ\10776A_09_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Design, Implement and Test the WebStock Views


X Task 1: Create the WebStock schema
1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


CREATE SCHEMA WebStock AUTHORIZATION dbo;
GO

4.

In the toolbar, click Execute.

X Task 2: Review the design requirements

Review the supplied design in the supporting documentation for the OnlineProducts and
AvailableModels views.

MCT USE ONLY. STUDENT USE PROHIBITED

L9-34

X Task 3: Design and implement the views


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE VIEW WebStock.OnlineProducts
AS
SELECT p.ProductID,
p.ProductName,
p.ProductNumber,
COALESCE(p.Color,'N/A') AS Color,
CASE p.DaysToManufacture
WHEN 0 THEN 'Instock'
WHEN 1 THEN 'Overnight'
WHEN 2 THEN 'Fast'
ELSE 'Call'
END AS Availability,
p.Size,
p.SizeUnitMeasureCode AS UnitOfMeasure,
p.ListPrice AS Price,
p.Weight
FROM Marketing.Product AS p
WHERE p.SellEndDate IS NULL
AND p.SellStartDate IS NOT NULL;
GO

CREATE VIEW WebStock.AvailableModels


AS
SELECT p.ProductID,
p.ProductName,
pm.ProductModelID,
pm.ProductModel
FROM Marketing.Product AS p
INNER JOIN Marketing.ProductModel AS pm
ON p.ProductModelID = pm.ProductModelID
WHERE p.SellEndDate IS NULL
AND p.SellStartDate IS NOT NULL;
GO

3.

In the toolbar, click Execute.

X Task 4: Test the views


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


SELECT * FROM WebStock.OnlineProducts;
GO
SELECT * FROM WebStock.AvailableModels;
GO

3.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 9: Designing and Implementing Views

L9-35

Module 9: Designing and Implementing Views

Exercise 2: Design and Implement the Contacts View


X Task 1: Create the Relationship schema
1.

In Object Explorer, expand the Databases under Proseware.

2.

Right-click the MarketDev database and click New Query.

3.

Type the query below in the query pane:


CREATE SCHEMA Relationship AUTHORIZATION dbo;
GO

4.

In the toolbar, click Execute.

X Task 2: Review the design requirements

Review the supplied design in the supporting documentation for the Contacts view.

X Task 3: Design and implement the view


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE VIEW Relationship.Contacts
AS
SELECT p.ProspectID AS ContactID,
p.FirstName,
p.MiddleName,
p.LastName,
'PROSPECT' AS ContactRole
FROM Marketing.Prospect AS p
UNION ALL
SELECT sp.SalespersonID,
sp.FirstName,
sp.MiddleName,
sp.LastName,
'SALESPERSON'
FROM Marketing.Salesperson AS sp;
GO

3.

In the toolbar, click Execute.

X Task 4: Test the view


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


SELECT * FROM Relationship.Contacts;
GO

3.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

L9-36

Challenge Exercise 3: Modify the AvailableModels View (Only if time


permits)
X Task 1: Alter the AvailableModels View
1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


ALTER VIEW WebStock.AvailableModels
AS
SELECT p.ProductID,
p.ProductName,
pm.ProductModelID,
pm.ProductModel,
COALESCE(ed.Description,id.Description) AS CatalogDescription
FROM Marketing.Product AS p
INNER JOIN Marketing.ProductModel AS pm
ON p.ProductModelID = pm.ProductModelID
LEFT OUTER JOIN Marketing.ProductDescription AS ed
ON pm.ProductModelID = ed.ProductModelID
AND ed.LanguageID = 'en'
LEFT OUTER JOIN Marketing.ProductDescription as id
ON pm.ProductModelID = id.ProductModelID
AND id.LanguageID = ''
WHERE p.SellEndDate IS NULL
AND p.SellStartDate IS NOT NULL;
GO

3.

In the toolbar, click Execute.

X Task 2: Test the view


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


SELECT * FROM WebStock.AvailableModels;
GO

3.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 9: Designing and Implementing Views

L9-37

MCT USE ONLY. STUDENT USE PROHIBITED

L10-38

Module 10: Designing and Implementing Stored Procedures

Lab 10: Designing and Implementing Stored


Procedures
Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_10_PRJ\10776A_10_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Create stored procedures


X Task 1: Review the Reports.GetProductColors stored procedure specification

Review the supplied design requirements in the supporting documentation for


Reports.GetProductColors.

X Task 2: Design, create and test the Reports.GetProductColors stored procedure


1.

In Object Explorer, expand the Proseware server, expand Databases, right-click the MarketDev
database and click New Query.

2.

Type the query below in the query pane:


CREATE PROCEDURE Reports.GetProductColors
AS
SELECT DISTINCT p.Color
FROM Marketing.Product AS p
WHERE p.Color IS NOT NULL
ORDER BY p.Color;
GO
EXEC Reports.GetProductColors;
GO

3.

In the toolbar, click Execute.


Note Ensure that approximately 9 colors are returned and that no NULL row is returned.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 10: Designing and Implementing Stored Procedures

L10-39

X Task 3: Review the Reports.GetProductsAndModels stored procedure specification

Review the supplied design requirements in the supporting documentation for


Reports.GetProductsAndModels.

X Task 4: Design, create and test the Reports.GetProductsAndModels stored procedure


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE PROCEDURE Reports.GetProductsAndModels
AS
SELECT p.ProductID,
p.ProductName,
p.ProductNumber,
p.SellStartDate,
p.SellEndDate,
p.Color,
pm.ProductModelID,
COALESCE(ed.Description,id.Description,p.ProductName)
AS EnglishDescription,
COALESCE(fd.Description,id.Description,p.ProductName)
AS FrenchDescription,
COALESCE(cd.Description,id.Description,p.ProductName)
AS ChineseDescription
FROM Marketing.Product AS p
LEFT OUTER JOIN Marketing.ProductModel AS pm
ON p.ProductModelID = pm.ProductModelID
LEFT OUTER JOIN Marketing.ProductDescription AS ed
ON pm.ProductModelID = ed.ProductModelID
AND ed.LanguageID = 'en'
LEFT OUTER JOIN Marketing.ProductDescription AS fd
ON pm.ProductModelID = fd.ProductModelID
AND fd.LanguageID = 'fr'
LEFT OUTER JOIN Marketing.ProductDescription AS cd
ON pm.ProductModelID = cd.ProductModelID
AND cd.LanguageID = 'zh-cht'
LEFT OUTER JOIN Marketing.ProductDescription AS id
ON pm.ProductModelID = id.ProductModelID
AND id.LanguageID = ''
ORDER BY p.ProductID,pm.ProductModelID;
GO
EXEC Reports.GetProductsAndModels;
GO

3.

In the toolbar, click Execute.


Note: Ensure that approximately 504 rows are returned. Check that appropriate language
descriptions appear. ProductID 1 should have the same description for all three languages.
ProductID 776 should have different descriptions for each language.

Exercise 2: Create a parameterized stored procedure


X Task 1: Review the Reports.GetProductsByColor stored procedure specification

Review the supplied design requirements in the supporting documentation for


Reports.GetProductsByColor.

Module 10: Designing and Implementing Stored Procedures

X Task 2: Design, create and test the Reports.GetProductsByColor stored procedure


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE PROCEDURE Reports.GetProductsByColor
@Color nvarchar(16)
AS
SELECT p.ProductID,
p.ProductName,
p.ListPrice AS Price,
p.Color,
p.Size,
p.SizeUnitMeasureCode AS UnitOfMeasure
FROM Marketing.Product AS p
WHERE (p.Color = @Color) OR (p.Color IS NULL AND @Color IS NULL)
ORDER BY ProductName;
GO
EXEC Reports.GetProductsByColor 'Blue';
GO
EXEC Reports.GetProductsByColor NULL;
GO

3.

In the toolbar, click Execute.


Note Ensure that approximately 26 rows are returned for blue products. Ensure that
approximately 248 rows are returned for products with no color.

Challenge Exercise 3: Alter the execution context of stored procedures


(Only if time permits)

MCT USE ONLY. STUDENT USE PROHIBITED

L10-40

X Task 1: Alter the Reports.GetProductColors stored procedure to execute as OWNER


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


ALTER PROCEDURE Reports.GetProductColors
WITH EXECUTE AS OWNER
AS
SELECT DISTINCT p.Color
FROM Marketing.Product AS p
WHERE p.Color IS NOT NULL
ORDER BY p.Color;
GO
EXEC Reports.GetProductColors;
GO

3.

In the toolbar, click Execute. Check that the procedure still executes without error.

X Task 2: Alter the Reports.GetProductsAndModels stored procedure to execute as


OWNER
1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


ALTER PROCEDURE Reports.GetProductsAndModels
WITH EXECUTE AS OWNER

AS
SELECT p.ProductID, p.ProductName, p.ProductNumber,
p.SellStartDate, p.SellEndDate, p.Color,
pm.ProductModelID,
COALESCE(ed.Description,id.Description,p.ProductName)
AS EnglishDescription,
COALESCE(fd.Description,id.Description,p.ProductName)
AS FrenchDescription,
COALESCE(cd.Description,id.Description,p.ProductName)
AS ChineseDescription
FROM Marketing.Product AS p
LEFT OUTER JOIN Marketing.ProductModel AS pm
ON p.ProductModelID = pm.ProductModelID
LEFT OUTER JOIN Marketing.ProductDescription AS ed
ON pm.ProductModelID = ed.ProductModelID
AND ed.LanguageID = 'en'
LEFT OUTER JOIN Marketing.ProductDescription AS fd
ON pm.ProductModelID = fd.ProductModelID
AND fd.LanguageID = 'fr'
LEFT OUTER JOIN Marketing.ProductDescription AS cd
ON pm.ProductModelID = cd.ProductModelID
AND cd.LanguageID = 'zh-cht'
LEFT OUTER JOIN Marketing.ProductDescription AS id
ON pm.ProductModelID = id.ProductModelID
AND id.LanguageID = ''
ORDER BY p.ProductID,pm.ProductModelID;
GO
EXEC Reports.GetProductsAndModels;
GO

3.

In the toolbar, click Execute. Check that the procedure still executes without error.

X Task 3: Alter the Reports.GetProductsByColor stored procedure to execute as


OWNER
1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


ALTER PROCEDURE Reports.GetProductsByColor
@Color nvarchar(16)
WITH EXECUTE AS OWNER
AS
SELECT p.ProductID,
p.ProductName,
p.ListPrice AS Price,
p.Color,
p.Size,
p.SizeUnitMeasureCode AS UnitOfMeasure
FROM Marketing.Product AS p
WHERE (p.Color = @Color) OR (p.Color IS NULL AND @Color IS NULL)
ORDER BY ProductName;
GO
EXEC Reports.GetProductsByColor 'Blue';
GO
EXEC Reports.GetProductsByColor NULL;
GO

3.

In the toolbar, click Execute. Check that the procedure still executes without error.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 10: Designing and Implementing Stored Procedures

L10-41

Module 11: Merging Data and Passing Tables

Lab 11: Passing Tables and Merging Data


Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_11_PRJ\10776A_11_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Create a TABLE Type


X Task 1: Review the parameters of a stored procedure

Review the supplied design requirements in the supporting documentation for


Reports.GetProductsByColorList.

X Task 2: Review the existing function


1.

In Object Explorer, expand Proseware, expand Databases, expand MarketDev, expand


Programmability, expand Functions, and expand Table-valued Functions.

2.

Right-click dbo.StringListToTable, click Script Function as, click CREATE To, select New Query
Editor Window.

3.

Review the design of the existing function.

X Task 3: Create a new TABLE type


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE TYPE StringList AS TABLE(StringValue nvarchar(1000) NOT NULL);
GO

3.

In the toolbar, click Execute.

Exercise 2: Use a TABLE Type Parameter


X Task 1: Create the stored procedure
1.

Right-click the MarketDev database and click New Query.

MCT USE ONLY. STUDENT USE PROHIBITED

L11-42

2.

Type the query below in the query pane:


CREATE PROCEDURE Reports.GetProductsByColorList_Test
(@ColorList StringList READONLY)
AS
SELECT p.ProductID,
p.ProductName,
p.ListPrice AS Price,
p.Color,
p.Size,
p.SizeUnitMeasureCode AS UnitOfMeasure
FROM Marketing.Product AS p
INNER JOIN @ColorList AS cl
ON p.Color = cl.StringValue
ORDER BY p.ProductName;
GO

3.

In the toolbar, click Execute.

X Task 2: Test the new procedure


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


DECLARE @ColorList StringList;
INSERT INTO @ColorList VALUES('Red'),('Blue'),('Silver');
EXEC Reports.GetProductsByColorList_Test @ColorList;
GO

3.

In the toolbar, click Execute.


Note

Approximately 107 rows should be returned.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 11: Passing Tables and Merging Data

L11-43

Challenge Exercise 3: Use a TABLE Type with MERGE (Only if time permits)
X Task 1: Create a new TABLE type
1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE TYPE dbo.SalespersonList
AS TABLE (SalespersonID int NOT NULL,
FirstName nvarchar(50) NULL,
MiddleName nvarchar(50) NULL,
LastName nvarchar(50) NULL,
BadgeNumber nvarchar(15) NULL,
EmailAlias nvarchar(256) NULL,
SalesTerritoryID int NULL);
GO

3.

In the toolbar, click Execute.

X Task 2: Create a replacement stored procedure


1.

Review the supplied design in the supporting documentation for the Marketing.SalespersonMerge
Stored Procedure.

2.

Right-click the MarketDev database and click New Query.

Module 11: Merging Data and Passing Tables

3.

Type the query below in the query pane:


CREATE PROCEDURE Marketing.SalespersonMerge
(@SalespersonDetails SalespersonList READONLY)
AS BEGIN
MERGE INTO Marketing.Salesperson AS sp
USING @SalespersonDetails AS spd
ON sp.SalespersonID = spd.SalespersonID
WHEN MATCHED THEN
UPDATE SET
sp.FirstName = COALESCE(spd.FirstName, sp.FirstName),
sp.MiddleName = COALESCE(spd.MiddleName, sp.MiddleName),
sp.LastName = COALESCE(spd.LastName, sp.LastName),
sp.BadgeNumber = COALESCE(spd.BadgeNumber, sp.BadgeNumber),
sp.EmailAlias = COALESCE(spd.EmailAlias, sp.EmailAlias),
sp.SalesTerritoryID
= COALESCE(spd.SalesTerritoryID, sp.SalesTerritoryID)
WHEN NOT MATCHED THEN
INSERT (SalespersonID,
FirstName,
MiddleName,
LastName,
BadgeNumber,
EmailAlias,
SalesTerritoryID)
VALUES (spd.SalespersonID,
spd.FirstName,
spd.MiddleName,
spd.LastName,
spd.BadgeNumber,
spd.EmailAlias,
spd.SalesTerritoryID)
OUTPUT $action AS Action, inserted.SalespersonID;
END;
GO

4.

In the toolbar, click Execute.

X Task 3: Test the replacement procedure


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


DECLARE @Salespeople SalespersonList;
INSERT INTO @Salespeople
(SalespersonID,
FirstName,
MiddleName,
LastName,
BadgeNumber,
EmailAlias,
SalesTerritoryID)
VALUES
(276,'Michael','','Wong',NULL,'adventure-works\mwong',NULL),
(601,'John','','Wong','234232','adventure-works\jwong',3);
EXEC Marketing.SalespersonMerge @Salespeople;
GO

3.

In the toolbar, click Execute.


Note The output should show UPDATE for 276 and should show INSERT for 601.

MCT USE ONLY. STUDENT USE PROHIBITED

L11-44

MCT USE ONLY. STUDENT USE PROHIBITED

L12-45

Module 12: Designing and Implementing User-Defined


Functions

Lab 12: Designing and Implementing UserDefined Functions


Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_12_PRJ\10776A_12_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Formatting Phone Numbers


X Task 1: Review the design requirements

Review the supplied function specifications in the supporting documentation in the Lab section of the
Student Manual, for the Phone Number.

X Task 2: Design and create the function


1.

In Object Explorer, expand Databases, right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane: (note that the query continues over the page)
CREATE FUNCTION dbo.FormatPhoneNumber
( @PhoneNumberToFormat nvarchar(16)
)
RETURNS nvarchar(16)
AS BEGIN
DECLARE @Digits nvarchar(16) = '';
DECLARE @Remaining nvarchar(16) = @PhoneNumberToFormat;
DECLARE @Character nchar(1);
IF LEFT(@Remaining,1) = N'+' RETURN @Remaining;
WHILE (LEN(@Remaining) > 0) BEGIN
SET @Character = LEFT(@Remaining,1);
SET @Remaining = SUBSTRING(@Remaining,2,LEN(@Remaining));
IF (@Character >= N'0') AND (@Character <= N'9')
SET @Digits += @Character;
END;

Module 12: Designing and Implementing User-Defined Functions

RETURN CASE LEN(@Digits)


WHEN 10 THEN N'(' + SUBSTRING(@Digits,1,3) + N') '
+ SUBSTRING(@Digits,4,3) + N'-'
+ SUBSTRING(@Digits,7,4)
WHEN 8 THEN SUBSTRING(@Digits,1,4) + N'-'
+ SUBSTRING(@Digits,5,4)
WHEN 6 THEN SUBSTRING(@Digits,1,3) + N'-'
+ SUBSTRING(@Digits,4,3)
ELSE @Digits
END;
END;
GO

3.

In the toolbar, click Execute.

X Task 3: Test the function

MCT USE ONLY. STUDENT USE PROHIBITED

L12-46

1.

In Object Explorer, expand Databases, right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


SELECT
SELECT
SELECT
SELECT
SELECT
GO

3.

dbo.FormatPhoneNumber('+61 3 9485-2342');
dbo.FormatPhoneNumber('415 485-2342');
dbo.FormatPhoneNumber('(41) 5485-2342');
dbo.FormatPhoneNumber('94852342');
dbo.FormatPhoneNumber('85-2342');

In the toolbar, click Execute.


Note The output should resemble the following:
+61 3 9485-2342
(415) 485-2342
(415) 485-2342
9485-2342
852-342

Exercise 2: Modifying an Existing Function


X Task 1: Review the requirements

Review the supplied function specifications in the supporting documentation in the Lab section of the
Student Manual, for the dbo.IntegerListToTable function.

X Task 2: Design and create the function


1.

In Object Explorer, expand Databases, then right-click the MarketDev database and click New
Query.

2.

L12-47

Type the query below in the query pane:


CREATE FUNCTION dbo.IntegerListToTable
( @InputList nvarchar(MAX),
@Delimiter nchar(1) = N',')
RETURNS @OutputTable TABLE (PositionInList int IDENTITY(1, 1) NOT NULL,
IntegerValue int)
AS BEGIN
DECLARE @RemainingString nvarchar(MAX) = @InputList;
DECLARE @DelimiterPosition int;
DECLARE @CurrentToken nvarchar(8);
WHILE LEN(@RemainingString) > 0
BEGIN
SET @DelimiterPosition
= CHARINDEX(@Delimiter, @RemainingString);
IF (@DelimiterPosition = 0)
SET @DelimiterPosition = LEN(@RemainingString) + 1;
IF (@DelimiterPosition > 8) SET @DelimiterPosition = 8;
SET @CurrentToken =
SUBSTRING(@RemainingString,1,@DelimiterPosition - 1);
INSERT INTO @OutputTable (IntegerValue)
VALUES(CAST(@CurrentToken AS int));
SET @RemainingString =
SUBSTRING(@RemainingString,
@DelimiterPosition + 1,
LEN(@RemainingString));
END;
RETURN;
END;
GO

3.

In the toolbar, click Execute.

X Task 3: Test the function


1.

In Object Explorer, expand Databases, then right-click the MarketDev database and click New
Query.

2.

Type the query below in the query pane:


SELECT * FROM dbo.IntegerListToTable('234,354253,3242,2',',');
GO

3.

In the toolbar, click Execute.


Note The output should resemble the following:

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 12: Designing and Implementing User-Defined Functions

Module 12: Designing and Implementing User-Defined Functions

X Task 4: Test the function with an alternate delimiter such as the pipe | character
1.

In Object Explorer, expand Databases, then right-click the MarketDev database and click New
Query.

2.

Type the query below in the query pane:


SELECT * FROM dbo.IntegerListToTable('234|354253|3242|2','|');
GO

3.

In the toolbar, click Execute.


Note The output should resemble the following:

MCT USE ONLY. STUDENT USE PROHIBITED

L12-48

Challenge Exercise 3: Resolve a Function-related Performance Issue (Only if


time permits)
X Task 1: Review the query

Review the supplied problematic query in the supporting documentation in the Lab section of the
Student Manual.

X Task 2: Design an alternate query


1.

In Object Explorer, expand Databases, right-click the MarketDev database and click New Query.

2.

Type the problem query from the supporting documentation and the replacement query in the query
pane:
SET STATISTICS TIME ON;
GO
SELECT dbo.JoinNames(FirstName,MiddleName,LastName) AS FullName
FROM Marketing.Prospect
ORDER BY FullName;
SELECT FirstName + N' '
+ CASE WHEN LEN(MiddleName) > 0 THEN MiddleName + N' '
ELSE N''
END
+ LastName AS FullName
FROM Marketing.Prospect
ORDER BY FullName;
GO

3.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 12: Designing and Implementing User-Defined Functions

L12-49

X Task 3: Use SET STATISTICS TIME ON to compare the performance of the new and
old queries
1.

In Object Explorer, expand Databases, right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


SET STATISTICS TIME ON;
GO
SELECT dbo.JoinNames(FirstName,MiddleName,LastName) AS FullName
FROM Marketing.Prospect
ORDER BY FullName;
GO
SELECT FirstName + N' '
+ CASE WHEN LEN(MiddleName) > 0 THEN MiddleName + N' '
ELSE N''
END
+ LastName AS FullName
FROM Marketing.Prospect
ORDER BY FullName;
GO
SET STATISTICS TIME OFF;
GO

3.

In the toolbar, click Execute.

Note The execution time for the alternate query should be shorter than the time for the original
query. The actual times will vary from computer to computer. The output should resemble the
following:

Module 13: Creating Highly Concurrent SQL Server 2012


Applications

Lab 13: Creating Highly Concurrent


SQL Server Applications
Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_13_PRJ\10776A_13_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Detecting Deadlocks


X Task 1: Start and configure SQL Server Profiler

MCT USE ONLY. STUDENT USE PROHIBITED

L13-50

1.

In SQL Server Management Studio, from Tools menu, click SQL Server Profiler.

2.

In Connect to Server window, type Proseware in the Server name text box.

3.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

4.

In the Trace Properties window, type Deadlock Detection in the Trace name textbox.

5.

Click Events Selection tab.

6.

Check the Show all events checkbox.

7.

Expand Locks, check Deadlock graph.

8.

Uncheck the Show all events checkbox.

9.

De-select all events except the Deadlock graph. You may need to scroll the window down to do this.

10. Click Run to start the trace.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 13: Creating Highly Concurrent SQL Server Applications

L13-51

X Task 2: Load and execute the test scripts


1.

In SQL Server Management Studio, double-click 51 Lab Exercise 1.sql in Solution Explorer.

2.

Review the script.

3.

In SQL Server Management Studio, double-click 52 Lab Exercise 1 2nd Window.sql in Solution
Explorer.

4.

Review the script.

5.

Execute 51 Lab Exercise 1.sql and then immediately execute 52 Lab Exercise 1 2nd
Window.sql. Wait for both to complete
Note

One of the scripts will return a deadlock error.

X Task 3: Stop the trace and review the deadlock graph


1.

In SQL Server Profiler, from File menu, click Stop Trace.

2.

Review the results from the Deadlock Trace.

3.

Maximize the Trace window inside SQL Server Profiler.

4.

Click on the event line that contains the Deadlock graph.

5.

Review the graph that appears at the bottom of the screen.

6.

Hover your mouse over each of the ellipses that represent processes.
Note When you hover your mouse over a process, you will see a pop-up window
displaying the T-SQL statement that was executed.

7.

Close SQL Server Profiler.

Challenge Exercise 2: Investigating Transaction Isolation Levels (Only if


time permits)
X Task 1: Load the scripts
1.

In SQL Server Management Studio, double-click 62 Lab Exercise 2 2nd Window.sql in Solution
Explorer.

2.

Right-click the tab at the top of the window that opens and click Close All But This.

3.

In SQL Server Management Studio, double-click 61 Lab Exercise 2.sql in Solution Explorer.

X Task 2: Execute the code

Execute the code step by step by following the instructions in the script files.
Note This exercise involves two script windows. Make sure that you execute only the code
for each step and in the correct window as directed by the scripts.

Module 14: Handling Errors in T-SQL Code

Lab 14: Handling Errors in T-SQL Code


Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_14_PRJ\10776A_14_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Replace @@ERROR based error handling with structured


exception handling
X Task 1: Review the existing code

MCT USE ONLY. STUDENT USE PROHIBITED

L14-52

1.

In Object Explorer, expand Proseware, expand Databases, expand MarketDev, expand


Programmability, expand Stored Procedures, right-click Marketing.MoveCampaignBalance, and
click Modify.

2.

Review the existing stored procedure.

X Task 2: Rewrite the stored procedure to use structured exception handling


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane: (note the query continues over the page)
CREATE PROCEDURE Marketing.MoveCampaignBalance_Test
( @FromCampaignID int,
@ToCampaignID int,
@BalanceToMove decimal(18,2)
)
AS BEGIN
SET XACT_ABORT ON;
BEGIN TRY
BEGIN TRANSACTION
UPDATE Marketing.CampaignBalance
SET RemainingBalance -= @BalanceToMove
WHERE CampaignID = @FromCampaignID;
UPDATE Marketing.CampaignBalance
SET RemainingBalance += @BalanceToMove
WHERE CampaignID = @ToCampaignID;
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION;
PRINT 'Unable to move balance';
RETURN 1;
-- 7
END CATCH;
END;
GO

3.

In the toolbar, click Execute.

4.

Leave the query window open for use in the next exercise.

X Task 3: Test the stored procedure


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


EXEC Marketing.MoveCampaignBalance_Test 3,2,51.50;
GO

3.

In the toolbar, click Execute.


Note

4.

You should see two messages each indicating that a single row has been updated.

Leave the query window open for use in the next exercise.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 14: Handling Errors in T-SQL Code

L14-53

Module 14: Handling Errors in T-SQL Code

Challenge Exercise 2: Add deadlock retry logic to the stored procedure


(Only if time permits)
X Task 1: Modify the code to re-try on deadlock
1.

In SQL Server Management Studio, return to the query window with the
Marketing.MoveCampaignBalance_Test stored procedure code.

2.

Modify the query as per the code shown below:


ALTER PROCEDURE Marketing.MoveCampaignBalance_Test
( @FromCampaignID int,
@ToCampaignID int,
@BalanceToMove decimal(18,2)
)
AS BEGIN
DECLARE @RetriesRemaining int = 5;
SET XACT_ABORT ON;
WHILE (@RetriesRemaining > 0) BEGIN
BEGIN TRY
BEGIN TRANSACTION
UPDATE Marketing.CampaignBalance
SET RemainingBalance -= @BalanceToMove
WHERE CampaignID = @FromCampaignID;
UPDATE Marketing.CampaignBalance
SET RemainingBalance += @BalanceToMove
WHERE CampaignID = @ToCampaignID;
COMMIT TRANSACTION;
SET @RetriesRemaining = 0;
END TRY
BEGIN CATCH
IF (ERROR_NUMBER() = 1205) BEGIN
SET @RetriesRemaining -=1;
PRINT 'Warning: deadlock retry';
WAITFOR DELAY '00:00:05';
END ELSE BEGIN
SET @RetriesRemaining = 0; -- exit the retry loop
IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;
PRINT 'Unable to move balance';
RETURN 1;
END;
END CATCH;
END;
END;
GO

3.

In the toolbar, click Execute.

X Task 2: Test the stored procedure


1.

In SQL Server Management Studio, return to the query window with the EXEC
Marketing.MoveCampaignBalance_Test code as shown in the query pane:
EXEC Marketing.MoveCampaignBalance_Test 3,2,51.50;
GO

2.

In the toolbar, click Execute.


Note

You should see two messages each indicating that a single row has been updated.

MCT USE ONLY. STUDENT USE PROHIBITED

L14-54

MCT USE ONLY. STUDENT USE PROHIBITED

L15-55

Module 15: Responding to Data Manipulation via Triggers

Lab 15: Responding to Data Manipulation


via Triggers
Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_15_PRJ\10776A_15_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Creating and Testing the Audit Trigger


X Task 1: Review the supporting documentation and existing system
1.

Review the supplied table requirements in the supporting documentation for


Marketing.CampaignAudit table.

2.

In Object Explorer, expand Proseware, expand Databases, expand MarketDev, expand Tables,
expand Marketing.CampaignBalance, and expand Columns.

3.

Review the table design.

X Task 2: Design a trigger to meet the requirements as stated in the scenario for this
exercise
1.

In Object Explorer, expand Databases, right-click the MarketDev database and click New Query,
then type the query below in the query pane: (note that query continues over page)
CREATE TRIGGER Marketing.TR_CampaignBalance_Update
ON Marketing.CampaignBalance
AFTER UPDATE
AS BEGIN
SET NOCOUNT ON;
INSERT Marketing.CampaignAudit
(AuditTime, ModifyingUser, RemainingBalance)
SELECT SYSDATETIME(),
ORIGINAL_LOGIN(),
inserted.RemainingBalance
FROM deleted
INNER JOIN inserted
ON deleted.CampaignID = inserted.CampaignID
WHERE deleted.RemainingBalance > 10000
OR inserted.RemainingBalance > 10000;
END;
GO

Module 15: Responding to Data Manipulation via Triggers

2.

In the toolbar, click Execute.

X Task 3: Write code to test the behavior of the trigger


1.

In Object Explorer, expand Databases, right-click the MarketDev database and click New Query
then type the query below in the query pane:
EXEC Marketing.MoveCampaignBalance 3,2,101000;
GO
SELECT * FROM Marketing.CampaignBalance;
GO
SELECT * FROM Marketing.CampaignAudit;
GO

2.

In the toolbar, click Execute.


Note

The output should be similar to the following:

Note

The dates and times will depend upon when you execute the code.

Challenge Exercise 2: Improve the Audit Trigger (Only if time permits)


X Task 1: Modify the trigger based on the updated requirements

MCT USE ONLY. STUDENT USE PROHIBITED

L15-56

1.

In Object Explorer, expand Proseware, expand Databases, right-click the MarketDev database, and
click Refresh.

2.

Expand the MarketDev, expand Tables, expand Marketing.CampaignBalance, and expand


Triggers.

3.

Review the trigger design.

4.

In Object Explorer, expand Databases, right-click the MarketDev database and click New Query,
then type the query below in the query pane: (note that query continues over page)
ALTER TRIGGER Marketing.TR_CampaignBalance_Update
ON Marketing.CampaignBalance
AFTER UPDATE
AS BEGIN
SET NOCOUNT ON;
INSERT Marketing.CampaignAudit
(AuditTime, ModifyingUser, RemainingBalance)
SELECT SYSDATETIME(),
ORIGINAL_LOGIN(),
inserted.RemainingBalance
FROM deleted

(code continued on the following page)

INNER JOIN inserted


ON deleted.CampaignID = inserted.CampaignID
WHERE ABS(deleted.RemainingBalance
- inserted.RemainingBalance) > 10000;
END;
GO

5.

In the toolbar, click Execute.

X Task 2: Delete all rows from the Marketing.CampaignAudit table

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 15: Responding to Data Manipulation via Triggers

L15-57

1.

In Object Explorer, expand Databases, right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


DELETE FROM Marketing.CampaignAudit;
GO

3.

In the toolbar, click Execute.

X Task 3: Test the modified trigger


1.

In Object Explorer, expand Databases, right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


SELECT * FROM Marketing.CampaignBalance;
GO
EXEC Marketing.MoveCampaignBalance 3,2,10100;
GO
EXEC Marketing.MoveCampaignBalance 3,2,1010;
GO
SELECT * FROM Marketing.CampaignAudit;
GO

3.

In the toolbar, click Execute.


Note The output should be similar to the following:

Note The dates and times will depend upon when you execute the code.

Module 16: Implementing Managed Code in SQL Server 2012

Lab 16: Implementing Managed Code in


SQL Server
Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Assess Proposed CLR Code


X Task 1: Review the supporting documentation

Review the supplied proposed SQLCLR functionality list in the supporting documentation.

X Task 2: For each object listed, determine if implementing it in managed code is


appropriate or not

The following components should be included in SQL CLR.


Proposed SQLCLR Functionality

Should be implemented

Table-valued function that returns a list of files in a


particular folder.

Yes, good use of external access.

Function that formats phone numbers as strings.

Yes, good use of string handling.

Trigger that records balance movements


with a value of more than 1000.

No, only involves data access.

Stored procedure that writes an XML file for a


given XML parameter.

Yes, good use of external access.

Function that counts rows in a table.

No, only involves data access.

A new Customer data type.

No, not an object oriented database.

MCT USE ONLY. STUDENT USE PROHIBITED

L16-58

Exercise 2: Implement a CLR Assembly


X Task 1: Ensure the database is configured appropriately to support an
EXTERNAL_ACCESS assembly
1.

RECONFIGURE;
GO
ALTER DATABASE MarketDev SET TRUSTWORTHY ON;
GO

In the toolbar, click Execute.

X Task 2: Catalog the assembly and the functions contained within it


1.

L16-59

In Object Explorer, under Proseware expand Databases, expand System Databases, and right-click
the master database and click New Query, then type the query below in the query pane:
EXEC sp_configure 'clr enabled',1;
GO

2.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 16: Implementing Managed Code in SQL Server

In Object Explorer, under Proseware expand Databases, and right-click the MarketDev database
and click New Query, then type the query below in the query pane:
CREATE ASSEMBLY SQLCLRDemo
FROM 'D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ\SQLCLRDemo.DLL'
WITH PERMISSION_SET = EXTERNAL_ACCESS;
GO

2.

In the toolbar, click Execute.

3.

Type the query below in the query pane:


SELECT * FROM sys.assemblies;
SELECT * FROM sys.assembly_files;
GO

4.

Highlight the query above and in the toolbar click Execute.

5.

Type the query below in the query pane:


CREATE FUNCTION dbo.IsValidEmailAddress(@email NVARCHAR(4000))
RETURNS BIT AS EXTERNAL NAME
SQLCLRDemo.[SQLCLRDemo.CLRDemoClass].IsValidEmailAddress;
GO

6.

Highlight the query above and in the toolbar click Execute.

7.

Type the query below in the query pane:


CREATE FUNCTION dbo.FormatAustralianPhoneNumber(@PhoneNumber NVARCHAR(4000))
RETURNS NVARCHAR(4000) AS EXTERNAL NAME
SQLCLRDemo.[SQLCLRDemo.CLRDemoClass].FormatAustralianPhoneNumber;
GO

Module 16: Implementing Managed Code in SQL Server 2012

8.

Highlight the query above and in the toolbar click Execute.

9.

Type the query below in the query pane:

MCT USE ONLY. STUDENT USE PROHIBITED

L16-60

CREATE FUNCTION dbo.FolderList(@RequiredPath NVARCHAR(4000),@FileMask NVARCHAR(4000))


RETURNS TABLE (FileName NVARCHAR(4000))
AS EXTERNAL NAME SQLCLRDemo.[SQLCLRDemo.CLRDemoClass].FolderList;
GO

10. Highlight the query above and in the toolbar click Execute.

Task 3: Test the functions contained within the assembly


1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
SELECT dbo.IsValidEmailAddress('test@somewhere.com');
GO
SELECT dbo.IsValidEmailAddress('test.somewhere.com');
GO
SELECT dbo.FormatAustralianPhoneNumber('0419201410');
SELECT dbo.FormatAustralianPhoneNumber('9 87 2 41 23');
SELECT dbo.FormatAustralianPhoneNumber('039 87 2 41 23');
GO
SELECT * FROM dbo.FolderList(
'D:\10776A_Labs\10776A_16_PRJ\10776A_16_PRJ','*.txt');
GO

2.

In the toolbar, click Execute.

Challenge Exercise 3: Implement a CLR User-defined Aggregate and CLR


User-defined Data Type (Only if time permits)
X Task 1: Catalog and test the user-defined aggregate
1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
CREATE AGGREGATE dbo.AggString(@s nvarchar(4000))
RETURNS nvarchar(4000)
EXTERNAL NAME SQLCLRDemo.[SQLCLRDemo.AggString];
GO
SELECT dbo.AggString(DISTINCT ProductNumber)
FROM Marketing.Product
WHERE Color = 'Black';
GO

2.

In the toolbar, click Execute.

X Task 2: Catalog and test the user-defined data type


1.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 16: Implementing Managed Code in SQL Server

L16-61

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
CREATE TYPE dbo.zVarChar
EXTERNAL NAME SQLCLRDemo.[SQLCLRDemo.zVarChar];
GO

2.

In the toolbar, click Execute.

3.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
CREATE TABLE dbo.TestTable
( RecID int IDENTITY(1,1),
TextValue zVarchar
);
GO

4.

In the toolbar, click Execute.

5.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
INSERT INTO dbo.TestTable VALUES('Some
GO

compressible

SELECT TextValue.ToString(),
TextValue.CompressedLength(),
TextValue.CompressionPercentage()
FROM dbo.TestTable;
GO
SELECT AVG(TextValue.CompressionPercentage()) FROM dbo.TestTable;
GO
DROP TABLE dbo.TestTable;
GO

data');

6.

In the toolbar, click Execute.

7.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
DECLARE @TestString zVarChar;
SET @TestString = 'Some
compressible
PRINT @TestString.CompressedValue();
GO
PRINT zVarChar::Compress(
'Some
compressible
GO

8.

In the toolbar, click Execute.

value');

value';

Module 17: Storing XML Data in SQL Server 2012

Lab 17: Storing XML Data in SQL Server


Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_17_PRJ\10776A_17_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Appropriate Usage of XML Data Storage in SQL Server


X Task 1: Review the list of Use Cases

Review the supplied Use Cases requirements in the supporting documentation.

X Task 2: Determine whether each is appropriate for XML

The following components should be implemented


Use Cases Requirements

Should be
implemented

Reason

Existing XML data that is stored but not


processed.

Yes

Data is already XML and does


not need to be processed.

Storing attributes for a customer.

No

Should be database columns.

Relational data that is being passed through


a system but not processed within it.

Perhaps

Only if the data is being sent


and received as XML.

Storing attributes that are nested (that is


attributes stored within attributes).

Yes

Not standard relational data.

Exercise 2: Investigate the Storage of XML Data in Variables


X Task 1: Review and execute the queries, and review the results
1.

Double-click the script 61 Lab Exercise 2.sql, in Solution Explorer.

2.

Highlight and execute the first command block as shown:


USE tempdb;
GO

MCT USE ONLY. STUDENT USE PROHIBITED

L17-62

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 17: Storing XML Data in SQL Server

L17-63

3.

Highlight and execute scripts 17.1 to 17.9 separately, comparing the results of each script with the
script comment.

4.

Review the results from each script.


Query Number Query Title

Output

17.1

Create a table with an xml column.

Command completed successfully.

17.2

Use implicit casting to assign an xml


variable.

One row is inserted.

17.3

Use implicit casting to assign a string


constant.

One row is inserted.

17.4

Explicitly cast string to xml.

One row is inserted.

17.5

Explicitly convert string to xml.

One row is inserted.

17.6

Well-formed document. This will


succeed.

One row is inserted.

17.7

Well-formed fragment. This will


succeed.

One row is inserted.

17.8

Not well-formed. This will fail.

Command will fail due to incorrect


XML format.

17.9

Clean up the temporary table.

Command completed successfully.

Exercise 3: Investigate the use of XML Schema Collections


X Task 1: Review and execute the queries, and review the results
1.

Double-click the script 71 Lab Exercise 3.sql, in Solution Explorer.

2.

Highlight and execute the first command block as shown:


USE tempdb;
GO

3.

Highlight and execute scripts 17.10 to 17.11 separately, comparing the results of each script with the
script comment.

4.

Review the results from each script.


Query
Number

Query Title

Output

17.10

Create an xml schema


collection for resumes.

Command completed successfully.

17.11

Retrieve information about


the components in the
schema collection.

Ten rows showing how the individual


components of the XML schema collection are
stored. Note the values in the kind_desc column.

Module 17: Storing XML Data in SQL Server 2012

MCT USE ONLY. STUDENT USE PROHIBITED

L17-64

Challenge Exercise 4: Investigate the Creation of Database Columns Based


on XML (Only if time permits)
X Task 1: Review and execute the queries, and review the results
1.

Double-click the script 81 Lab Exercise 4.sql, in Solution Explorer.

2.

Highlight and execute the first command block as shown:


USE tempdb;
GO

3.

Highlight and execute scripts 17.12 to 17.20 separately, comparing the results of each script with the
script comment.

4.

Review the results from each script.


Query Number

Query Title

Output

17.12

Create a table with a typed xml


column for documents.

Command completed successfully.

17.13

Insert valid XML.

One row is inserted.

17.14

Try to insert invalid XML (this will fail).

No rows are inserted. Even though


well-formed XML was supplied, it
did not meet the required schema.

17.15

Use CONTENT keyword.

Command completed successfully.

17.16

Insert a single document into


a CONTENT column.

One row is inserted.

17.17

Insert multiple valid elements into


a CONTENT column.

One row is inserted. Note the row


is inserted, even though no root
element is present.

17.18

Use DOCUMENT keyword.

Command completed successfully.

17.19

Insert a single document into a


DOCUMENT column.

One row is inserted.

17.20

Insert multiple valid elements into a


DOCUMENT column. This will fail.

No row is inserted as no root


element is present.

MCT USE ONLY. STUDENT USE PROHIBITED

L18-65

Module 18: Querying XML Data in SQL Server 2012

Lab 18: Querying XML Data in SQL Server


Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In the Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_18_PRJ\10776A_18_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Learn to query SQL Server data as XML


X Task 1: Review and execute the query and review the results
1.

Double-click the script 51 Lab Exercise 1.sql, in Solution Explorer.

2.

In the query below, highlight and execute scripts 18.1 to 18.9 separately, comparing the results of
each script with the script comment.
Query Number

Query Title

Output

18.1

FOR XML AUTO.

One row per product. Note the element name is


based on the table name.

18.2

ELEMENTS with RAW


mode.

Note the element centric output and the element


name is row.

18.3

NULL columns with


ELEMENTS.

Products without color show xsi:nil=true. Other


products show the color.

18.4

Note the effect of the


column alias
compared to 18.3.

Note the element name is now Product, based on


the alias name of the table.

18.5

Inline XSD schema.

Note the inclusion of an XSD schema.

18.6

Nested XML with


TYPE.

Note that rows with a value in the Description


column show that value as XML.

Module 18: Querying XML Data in SQL Server 2012

(continued)
Query Number

Query Title

Output

18.7

PATH Mode.

Note how the output can be constructed with a


PATH query. In the output locate each of the
elements in the SELECT clause.

18.8

ROOT directive.

Note the AvailableItems root node.

18.9

Named element in
RAW modes.

Note the AvailableItem ElementName.

Exercise 2: Write a stored procedure returning XML


X Task 1: Review the requirements

Review the supplied stored procedure specification in the supporting documentation for
WebStock.GetAvailableModelsAsXML.

X Task 2: Create the stored procedure


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE PROCEDURE WebStock.GetAvailableModelsAsXML
AS BEGIN
SELECT p.ProductID,
p.ProductName,
p.ListPrice,
p.Color,
p.SellStartDate,
pm.ProductModelID,
pm.ProductModel
FROM Marketing.Product AS p
INNER JOIN Marketing.ProductModel AS pm
ON p.ProductModelID = pm.ProductModelID
WHERE p.SellStartDate IS NOT NULL
AND p.SellEndDate IS NULL
ORDER BY p.SellStartDate, p.ProductName
FOR XML RAW('AvailableModel'), ROOT('AvailableModels');
END;
GO

3.

In the toolbar, click Execute.

X Task 3: Test the stored procedure


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


EXEC WebStock.GetAvailableModelsAsXML;
GO

3.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

L18-66

L18-67

Challenge Exercise 3: Write a stored procedure that updates using XML


(Only if time permits)
X Task 1: Review the requirements

Review the supplied stored procedure specification in the supporting documentation for
Marketing.UpdateSalesTerritoriesByXML.

X Task 2: Create the stored procedure


1.

Right-click the MarketDev database and click New Query.

2.

Type the query below in the query pane:


CREATE PROCEDURE Marketing.UpdateSalesTerritoriesByXML
( @SalespersonMods xml
)
AS BEGIN
DECLARE @Handle int;
EXEC master.dbo.sp_xml_preparedocument @Handle OUTPUT, @SalespersonMods;
UPDATE sp
SET sp.SalesTerritoryID = spm.SalesTerritoryID
FROM Marketing.Salesperson AS sp
INNER JOIN OPENXML(@Handle,'SalespersonMods/SalespersonMod/Mods/Mod',1)
WITH (SalesTerritoryID int '@SalesTerritoryID',
SalespersonID int '../../@SalespersonID') AS spm
ON sp.SalespersonID = spm.SalespersonID;
EXEC master.dbo.sp_xml_removedocument @Handle;
END;
GO

3.

In the toolbar, click Execute.

X Task 3: Test the stored procedure


1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
DECLARE @SalespersonMods xml;
SET @SalespersonMods = '<SalespersonMods>
<SalespersonMod SalespersonID="274">
<Mods>
<Mod SalesTerritoryID="3"/>
</Mods>
</SalespersonMod>
<SalespersonMod SalespersonID="278">
<Mods>
<Mod SalesTerritoryID="4"/>
</Mods>
</SalespersonMod>
</SalespersonMods>';
EXEC Marketing.UpdateSalesTerritoriesByXML @SalespersonMods;
GO
SELECT * FROM Marketing.Salesperson;
GO

2.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 18: Querying XML Data in SQL Server

In the toolbar, click Execute.

Module 19: Working with SQL Server 2012 Spatial Data

Lab 19: Working with SQL Server Spatial


Data
Lab Setup
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_19_PRJ\10776A_19_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Familiarity With Geometry Data Type


X Task 1: Review and execute the sample script
1.

Double-click the script 51 Lab Exercise 1.sql, in Solution Explorer.

2.

In the query below, highlight and execute scripts 19.1 to 19.9 separately, comparing the results of
each script with the script comment.

3.

Review the results from each script. Remember to click on the Spatial results tab to see the output.

Query Number Query Title

Output

MCT USE ONLY. STUDENT USE PROHIBITED

L19-68

19.1

Draw a square.

A square is drawn.

19.2

Try an invalid value - note the 6522


error and the wrapped error message.

A .NET error is returned. Scroll to the right to read


the full message. Note how specific the message is.

19.3

Draw a more complex shape.

The shape is drawn. Note how a polygon is


represented in text in the query.

19.4

Multiple shapes.

Two shapes are drawn.

19.5

Intersecting shapes.

The shapes are moved to intersect.

19.6

Union of two shapes.

A combined shape is drawn.

19.7

Intersection of shapes.

The two intersecting shapes and the intersection


are drawn.

19.8

Draw Australia.

Australian map is drawn.

19.9

Draw Australia with a buffer around


it.

Note the calculation of a buffer region around the


map.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 19: Working with SQL Server Spatial Data L19-69

Exercise 2: Adding Spatial Data to an Existing Table


X Task 1: Add a Location column
1.

In Object Explorer, expand Databases, then right-click the MarketDev database and click New
Query.
Type the query below in the query pane:
ALTER TABLE Marketing.ProspectLocation
ADD Location GEOGRAPHY NULL;
GO

2.

In the toolbar, click Execute.

X Task 2: Write code to assign values to this column


1.

Right-click the MarketDev database and click New Query.


Type the query below in the query pane:
UPDATE Marketing.ProspectLocation
SET Location = GEOGRAPHY::STGeomFromText(
'POINT(' + CAST(Longitude AS varchar(20))
+ ' ' + CAST(Latitude AS varchar(20))
+ ')',4326);
GO

2.

In the toolbar, click Execute.

X Task 3: Drop the existing Latitude and Longitude columns


1.

Right-click the MarketDev database and click New Query.


Type the query below in the query pane:
ALTER TABLE Marketing.ProspectLocation
DROP COLUMN Latitude;
GO
ALTER TABLE Marketing.ProspectLocation
DROP COLUMN Longitude;
GO

2.

In the toolbar, click Execute.

Challenge Exercise 3: Business Application of Spatial Data (Only if time


permits)
X Task 1: Review the requirements

Review the supporting documentation for details of the required stored procedure.

X Task 2: Create a spatial index


1.

Right-click the MarketDev database and click New Query.


Type the query below in the query pane:
CREATE SPATIAL INDEX IX_ProspectLocation_Location
ON Marketing.ProspectLocation (Location);
GO

2.

In the toolbar, click Execute.

Module 19: Working with SQL Server 2012 Spatial Data

X Task 3: Design and implement the Stored Procedure


1.

Right-click the MarketDev database and click New Query.


Type the query below in the query pane:
CREATE PROCEDURE Marketing.GetNearbyProspects
( @ProspectID int,
@DistanceInKms int
)
AS BEGIN
DECLARE @LocationToTest GEOGRAPHY;
SET @LocationToTest = (SELECT pl.Location
FROM Marketing.ProspectLocation AS pl
WHERE pl.ProspectID = @ProspectID);
SELECT pl.Location.STDistance(@LocationToTest)
/ 1000 AS Distance,
p.ProspectID,
p.LastName,
p.FirstName,
p.WorkPhoneNumber,
p.CellPhoneNumber,
pl.AddressLine1,
pl.AddressLine2,
pl.City,
pl.Location.Long AS Longitude,
pl.Location.Lat AS Latitude
FROM Marketing.Prospect AS p
INNER JOIN Marketing.ProspectLocation AS pl
ON p.ProspectID = pl.ProspectID
WHERE pl.Location.STDistance(@LocationToTest)
< (@DistanceInKms * 1000)
AND p.ProspectID <> @ProspectID
ORDER BY Distance;
END;
GO

2.

In the toolbar, click Execute.

X Task 4: Test the procedure


1.

Right-click the MarketDev database and click New Query.


Type the query below in the query pane:
EXEC Marketing.GetNearbyProspects 2,50;

2.

In the toolbar, click Execute.

MCT USE ONLY. STUDENT USE PROHIBITED

L19-70

MCT USE ONLY. STUDENT USE PROHIBITED

L20-71

Module 20: Working with Full-Text Indexes and Queries

Lab 20: Working with Full-Text Indexes and


Queries
Lab Setup

For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.

Revert the virtual machines as per the instructions in D:\10776A_Labs\Revert.txt.

2.

In the virtual machine, click Start, click All Programs, click Microsoft SQL Server 2012, and click
SQL Server Management Studio.

3.

In Connect to Server window, type Proseware in the Server name text box.

4.

In the Authentication drop-down list box, select Windows Authentication and click Connect.

5.

In the File menu, click Open, and click Project/Solution.

6.

In the Open Project window, open the project


D:\10776A_Labs\10776A_20_PRJ\10776A_20_PRJ.ssmssln.

7.

In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.

Exercise 1: Implement a full-text index


X Task 1: Create a full-text catalog
1.

In Object Explorer, expand the Proseware server, expand Databases, right-click the MarketDev
database and click New Query, then type the query below in the query pane:
CREATE FULLTEXT CATALOG DefaultFullTextCatalog AS DEFAULT;
GO

2.

In the toolbar, click Execute.

X Task 2: Create a full-text index on the Description column of the


Marketing.ProductDescription table
1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
CREATE FULLTEXT INDEX ON Marketing.ProductDescription(Description)
KEY INDEX PK_ProductDescription
WITH CHANGE_TRACKING OFF, NO POPULATION;
GO

2.

In the toolbar, click Execute.

Module 20: Working with Full-Text Indexes and Queries

X Task 3: Enable automatic change tracking for the index


1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
ALTER FULLTEXT INDEX ON Marketing.ProductDescription SET CHANGE_TRACKING AUTO;
GO

2.

In the toolbar, click Execute. Note that a warning might be returned when you execute the above
command, even though the command executed successfully. If a warning appears, it can be ignored.

X Task 4: Check to see that the population of the index is complete


1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
SELECT is_enabled, has_crawl_completed, crawl_type,
crawl_start_date, crawl_end_date
FROM sys.fulltext_indexes;
GO

2.

In the toolbar, click Execute.

3.

Re-execute the query until the has_crawl_completed column shows the value 1.

X Task 5: Write a simple CONTAINS query


1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
SELECT d.ProductDescriptionID,
d.Description
FROM Marketing.ProductDescription AS d
WHERE CONTAINS(d.Description,'bottle')
ORDER BY d.ProductDescriptionID;
GO

2.

In the toolbar, click Execute.


Note Three rows are returned.

X Task 6: Write a CONTAINS query that searches for two words


1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
SELECT d.ProductDescriptionID,
d.Description
FROM Marketing.ProductDescription AS d
WHERE CONTAINS(d.Description,'elastic AND lycra')
ORDER BY d.ProductDescriptionID;
GO

2.

MCT USE ONLY. STUDENT USE PROHIBITED

L20-72

In the toolbar, click Execute.


Note

One row is returned.

X Task 7: Write a CONTAINS query that uses INFLECTIONAL forms


1.

In the toolbar, click Execute.


Note

One row is returned.

Exercise 2: Implement a stoplist


X Task 1: Review the existing system stopwords
1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
SELECT * FROM sys.fulltext_system_stopwords
WHERE language_id = 1033;
GO

2.

In the toolbar, click Execute. A list of system stop words for the English language is returned.

X Task 2: Create a full-text stoplist


1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
CREATE FULLTEXT STOPLIST CommonWords;
GO

2.

In the toolbar, click Execute.

X Task 3: Add words to the stoplist


1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
ALTER
ADD
ALTER
ADD
GO

2.

L20-73

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
SELECT d.ProductDescriptionID,
d.Description
FROM Marketing.ProductDescription AS d
WHERE CONTAINS(d.Description,'FORMSOF(INFLECTIONAL,wash)')
ORDER BY d.ProductDescriptionID;
GO

2.

MCT USE ONLY. STUDENT USE PROHIBITED

Lab 20: Working with Full-Text Indexes and Queries

FULLTEXT STOPLIST CommonWords


'Bike' LANGUAGE 1033;
FULLTEXT STOPLIST CommonWords
'AdventureWorks' LANGUAGE 1033;

In the toolbar, click Execute.

Module 20: Working with Full-Text Indexes and Queries

X Task 4: View the stoplist


1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
SELECT * FROM sys.fulltext_stoplists;
GO
SELECT * FROM sys.fulltext_stopwords;
GO

2.

In the toolbar, click Execute. A list of stoplists and stopwords is returned.

Challenge Exercise 3: Create a stored procedure to implement a full-text


search (Only if time permits)
X Task 1: Review the supporting documentation

Review the supporting documentation for specification for a new stored procedure.

X Task 2: Design, implement, and test the stored procedure


1.

Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
CREATE PROCEDURE Marketing.GetRelevantDescriptions
( @PhraseToSearch nvarchar(1000)
)
AS BEGIN
SELECT d.ProductDescriptionID,
d.Description,
ft.RANK AS Ranking
FROM Marketing.ProductDescription AS d
INNER JOIN FREETEXTTABLE(Marketing.ProductDescription,
Description,
@PhraseToSearch) AS ft
ON d.ProductDescriptionID = ft.[KEY]
WHERE d.LanguageID = 'en'
ORDER BY ft.RANK DESC;
END;
GO
EXEC Marketing.GetRelevantDescriptions 'strong frame';
GO
EXEC Marketing.GetRelevantDescriptions 'serious competition';
GO

2.

MCT USE ONLY. STUDENT USE PROHIBITED

L20-74

In the toolbar, click Execute.


Note The first query returns twenty rows. The second query returns five rows.

Potrebbero piacerti anche