Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
L E A R N I N G
10776A
Developing Microsoft
Databases
P R O D U C T
O F F I C I A L
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
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.
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.
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.
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.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
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.
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
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
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 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)
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:
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
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
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.
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.
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
xiv
Acknowledgments
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.
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.
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 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 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'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.
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
2-3
2-16
2-25
2-33
2-39
3-3
3-14
3-19
3-29
4-3
4-9
4-17
4-31
5-3
5-11
5-19
5-24
6-3
6-13
6-19
6-22
xvi
7-3
7-12
7-22
7-28
8-3
8-10
8-18
8-24
9-3
9-10
9-19
9-27
10-3
10-11
10-22
10-31
10-37
11-3
11-14
11-22
11-26
12-3
12-7
12-13
12-18
12-25
12-28
xvii
13-3
13-17
13-28
13-38
13-44
14-3
14-13
14-24
14-32
15-3
15-12
15-19
15-29
16-3
16-14
16-21
16-39
17-3
17-15
17-26
17-31
18-3
18-15
18-26
18-35
19-3
19-12
19-25
19-35
xviii
20-3
20-9
20-20
20-32
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
xix
Module 16
Implementing Managed Code in SQL Server 2012
Contents:
Lesson 1: Introduction to SQL CLR Integration
16-3
16-14
16-21
16-39
Module Overview
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:
Lesson 1
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:
Key Points
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.
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?
Key Points
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.
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.
Key Points
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.
The CLR Integration feature within Microsoft SQL Server allows you to use .NET assemblies to customize
your SQL databases.
The .NET CLR offers:
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
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 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.
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.
Key Points
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.
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.
Stored procedures
DML triggers
DDL triggers
User-defined aggregates
16-9
Key Points
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.
Key Points
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.
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.
DDL Triggers
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.
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 Steps
16-13
1.
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.
5.
6.
7.
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?
Lesson 2
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:
Detail the permission sets that are available for securing assemblies
Import an assembly
What Is an Assembly?
Key Points
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.
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.
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.
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.
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?
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
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.)
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.
Demonstration Steps
1.
16-20
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.
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
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:
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 and that require access to
external resources
Implement user-defined data types that have been written in managed code
Attribute Usage
Key Points
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
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.
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
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.
Key Points
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:
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.
Parameter Naming
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 Steps
1.
16-27
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.
3.
Follow the instructions contained within the comments of the script file.
Key Points
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();
}
};
16-29
Key Points
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.
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.
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
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.
Demonstration Steps
1.
16-32
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.
3.
Follow the instructions contained within the comments of the script file.
User-defined Aggregates
Key Points
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.
Attribute Properties
A few more useful attribute properties are shown in the example.
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?
Key Points
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.
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))
Key Points
UDDTs are not directly comparable but their properties are.
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
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.
Demonstration Steps
1.
16-38
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.
3.
Follow the instructions contained within the comments of the script file.
16-39
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.
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.
6.
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.
Supporting Documentation
The following list details the proposed functionality being considered for managed code.
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.
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.
2.
3.
Ensure that SQL CLR integration is enabled for the SQL Server instance.
2.
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.
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.
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.
16-42
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.
2.
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
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.
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.
Review Questions
1.
2.
Best Practices
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.
Rare use of UNSAFE permissions and only after very serious justifications and testing.
Module 17
Storing XML Data in SQL Server 2012
Contents:
Lesson 1: Introduction to XML and XML Schemas
17-3
17-15
17-26
17-31
Module Overview
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:
Lesson 1
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:
Determine appropriate use cases for XML data storage in SQL Server
17-3
Discussion
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.
Key Points
17-5
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?
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.
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?
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.
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>
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.
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?
17-8
XML Namespaces
Key Points
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"
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
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:
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.
Key Points
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.
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.
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.
Demonstration Steps
17-14
1.
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.
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
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:
Choose whether XML fragments can be stored rather than entire XML documents
Key Points
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.
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
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;
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.
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.
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>
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>';
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';
Key Points
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)
);
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?
Key Points
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.
DOCUMENT
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 Steps
1.
17-25
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.
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?
Lesson 3
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:
Explain how each of the four types of XML index can be used
Key Points
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.
Key Points
17-28
SQL Server supports four types of XML index: a primary XML index and up to three secondary XML types.
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);
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;
17-29
Demonstration Steps
1.
17-30
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.
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 Setup
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.
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.
6.
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.
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)
2.
Determine whether the use cases are suitable for XML storage.
17-32
Results: After this exercise, you have seen how to analyze requirements and determine on appropriate
use cases for XML storage.
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:
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.
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:
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.
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.
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.
Review Questions
1.
What is XML?
2.
3.
Best Practices
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.
Module 18
Querying XML Data in SQL Server 2012
Contents:
Lesson 1: Using the T-SQL FOR XML Statement
18-3
18-15
18-26
18-35
Module Overview
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:
Lesson 1
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:
Key Points
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?
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.
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;
18-5
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;
Note that each column has been returned as a sub-element of the row element.
Key Points
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.
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:
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;
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;
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;
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.
Key Points
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).
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.
Key Points
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.
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?
Key Points
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;
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:
Demonstration Steps
18-14
1.
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.
5.
Follow the instructions contained within the comments of the script file.
Question: When should you use EXPLICIT mode in an XQuery?
Lesson 2
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:
What Is XQuery?
Key Points
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
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()
value()
exist()
modify()
nodes()
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?
query() Method
Key Points
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.
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:
value() Method
Key Points
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
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:
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
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
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:
Slide Example
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 Steps
1.
18-25
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.
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.
Lesson 3
Shredding XML
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:
Use system stored procedures for creating and managing in-memory node trees extracted from
XML documents
Key Points
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.
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.
Key Points
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
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.
OPENXML
Key Points
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.
Key Points
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.
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.
nodes() Method
Key Points
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.
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?
Demonstration Steps
1.
18-34
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.
3.
Follow the instructions contained within the comments of the script file.
Lab Setup
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.
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.
6.
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.
Supporting Documentation
Stored Procedure Specifications
18-36
Stored Procedure
WebStock.GetAvailableModelsAsXML
Input Parameters:
None
Output Parameters:
None
Returned Rows:
Output Order:
Marketing.UpdateSalesTerritoriesByXML
Input Parameters:
@SalespersonMods xml
Output Parameters:
None
Returned Rows:
None
Actions:
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:
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.
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.
2.
3.
Review the supporting documentation for details of the required stored procedure
WebStock.GetAvailableModelsAsXML.
Create and implement the stored procedure based on the specifications provided.
Results: After this exercise, you should have created and tested the required stored procedure that
returns XML.
18-38
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.
2.
3.
Test the stored procedure and ensure the updates have been applied.
Review the supporting documentation for details of the required stored procedure
Marketing.UpdateSalesTerritoriesByXML.
Create and implement the stored procedure based on the specifications given in the supporting
documentation for the lab.
Results: After this exercise, you should have created and tested the required stored procedure that
updates XML.
Review Questions
1.
2.
3.
4.
Best Practices
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.
Module 19
Working with SQL Server 2012 Spatial Data
Contents:
Lesson 1: Introduction to Spatial Data
19-3
19-12
19-25
19-35
Module Overview
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
Lesson 1
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 the relationship between the SQL Server spatial data support and the industry standards
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
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.
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.
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.
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.
Key Points
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.
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?
Key Points
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?
Key Points
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).
Key Points
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.
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]]
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 Steps
19-11
1.
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.
5.
Follow the instructions contained within the comments of the script file.
Lesson 2
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:
Explain how system CLR types differ from user CLR types
Use Microsoft extensions to the OGC standard when working with spatial data
Key Points
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.
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.
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.
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?
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.
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;
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.
Key Points
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.
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();
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.
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.
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.
Key Points
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.
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.
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.
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.
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.
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.
Key Points
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.
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?
Key Points
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.
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
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.
Demonstration Steps
1.
19-24
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.
3.
Follow the instructions contained within the comments of the script file.
Lesson 3
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:
Explain the basic tessellation process used within SQL Server spatial indexes
Key Points
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
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.
Spatial Indexes
Key Points
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.
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.
Key Points
19-29
Spatial indexes are created using the CREATE SPATIAL INDEX statement. Indexes on geometry data type
should specify a BOUNDING_BOX.
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.
Grid Density
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.
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.
Key Points
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.
Key Points
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.
Key Points
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.
Demonstration Steps
1.
19-34
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.
3.
Follow the instructions contained within the comments of the script file.
Lab Setup
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.
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.
6.
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.
Supporting Documentation
Stored Procedure Specifications
Stored Procedure Name
Marketing.GetNearbyProspects
Input Parameters
@ProspectID int
@DistanceInKms int
Output Parameters
None
Output Order
Distance
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 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.
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.
2.
3.
19-37
Write code to assign values to this column based on the existing Latitude and Longitude columns.
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.
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.
2.
3.
4.
Review the supporting documentation for details of the required stored procedure.
Design and implement the stored procedure based on the specifications given in the supporting
documentation for the lab.
Results: After this exercise, you should have created and tested the required stored procedure.
Review Questions
1.
What is the main difference between the geometry and geography data types?
2.
Best Practices
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.
Module 20
Working with Full-Text Indexes and Queries
Contents:
Lesson 1: Introduction to Full-Text Indexing
20-3
20-9
20-20
20-32
Module Overview
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
Lesson 1
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
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?
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?
Key Points
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.
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?
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
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
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.
Demonstration Steps
20-8
1.
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.
5.
Follow the instructions contained within the comments of the script file.
Lesson 2
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:
Key Points
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.
Key Points
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:
text
varchar(max)
ntext
nvarchar(max)
image
varbinary(max)
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.
Key Points
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.
Stemmer
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.
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?
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.
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 Stemmer has been applied, the following output could be obtained:
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.
Key Points
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?
Key Points
Creating and querying full-text indexes is not difficult but involves a number of steps.
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.
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.
Full-Text Index
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.
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.
Demonstration Steps
1.
20-19
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.
3.
Follow the instructions contained within the comments of the script file.
Lesson 3
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:
Return CONTAINS and FREETEXT results as tables along with ranking of relevance
CONTAINS Queries
Key Points
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
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')
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
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.
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.
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.)
Key Points
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.
RANK
Besides returning the relevant rows, CONTAINSTABLE and FREETEXTTABLE also return a measure of
relevance of each returned row called RANK.
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
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.
Diacritic Sensitivity
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.
Key Points
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.
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;
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;
Key Points
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.
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;
Demonstration Steps
1.
20-31
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.
3.
Follow the instructions contained within the comments of the script file.
20-32
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.
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.
6.
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
Provide relevant results by using the FREETEXTTABLE function and limit your results to rows with
LanguageID = 'en' (for English).
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.
2.
3.
4.
5.
6.
7.
Execute T-SQL statements to create a full-text index on the Description column of the
Marketing.ProductDescription table.
Execute T-SQL statements to enable automatic change tracking for the table.
Write a query to search for ProductDescriptionID and Description where the Description column
contains the word bottle.
Write a query to search for ProductDescriptionID and Description where the Description column
contains the words, elastic and lycra.
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.
2.
3.
4.
Review the existing system stopwords by querying the view sys.fulltext_system_stopwords for
language_id 1033 (English).
Add the words, Bike and AdventureWorks to the stoplist for the English language.
View the new stoplist by querying the sys.fulltext_stoplists and sys.fulltext_stopwords views.
20-34
Results: After this exercise, you should have created a new stoplist that will help avoid excessive index
size growth.
20-35
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.
2.
The supporting documentation includes specifications for the design of a new 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.
Review Questions
1.
2.
3.
4.
Best Practices
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.
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.
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.
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
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:
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:
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.
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.
4.
5.
In the Confirm Password text box, type Pa$$w0rd and click OK.
6.
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.
4.
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.
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.
In the left-hand pane of the SQL Server Configuration Manager window, click SQL Server Services.
2.
3.
4.
In the left-hand pane of the SQL Server Configuration Manager window, click SQL Server Services.
5.
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.
3.
4.
5.
In the Alias New window, in the Alias Name text box, type AdvDev.
6.
7.
In the left-hand pane of the SQL Server Configuration Manager window, expand SQL Native
Client 11.0 Configuration and click Client Protocols.
2.
3.
4.
5.
In the Alias New window, in the Alias Name text box, type AdvDev.
6.
7.
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.
4.
In the Authentication drop-down list, select Windows Authentication, and click Connect.
5.
6.
7.
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.
4.
5.
Scroll to the bottom of the screen, under the IP All section, clear the value for TCP Dynamic Ports.
6.
7.
8.
9.
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.
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.
4.
In the SQL Server Browser Properties window, in the Service tab, set the Start Mode to Disabled
and click OK.
5.
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
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
Table 2: Opportunity
Description
Probability of success
Estimated revenue
Delivery address
Table 3: SpecialOrder
Description
Date of order
L2-6
2.
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.
3.
4.
5.
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.
3.
4.
5.
6.
Ensure that the results from the query match the required output as shown in the supporting
documentation.
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
Definition
Marketing.Prospect
CellPhoneNumber
NVARCHAR(20)
Marketing.Prospect
HomePhoneNumber
NVARCHAR(25)
Marketing.Prospect
WorkPhoneNumber
VARCHAR(22)
Definition
Table
Marketing.Prospect
EmailAddress
NVARCHAR(100)
Marketing.SalesPerson
EmailAlias
NVARCHAR(256)
2.
3.
4.
2.
3.
4.
L2-8
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
Review the supplied design in the supporting documentation for the exercise.
Table1: Competitor
Name
Data Type
Design Rationale
CompetitorID
INT
NOT NULL
CompetitorName
NVARCHAR(30)
NOT NULL
StreetAddress
NVARCHAR(max)
NOT NULL
DateEntered
DATE
NOT NULL
(continued)
Name
Data Type
Design Rationale
L3-10
StrengthOfCompe
tition
NVARCHAR(8)
NOT NULL
Comments
NVARCHAR(max) NULL
Table2A: City (Duplicated City details extracted from the original table called
TVAdvertisements)
Name
Data Type
Design Rationale
CityID
INT
NOT NULL
CityName
Data Type
Design Rationale
TVStationID
INT
NOT NULL
TVStationName
NVARCHAR(15)
NOT NULL
CityID
INT
NOT NULL
CostPerAdvertisement
DECIMAL(12,2)
NOT NULL
L3-11
Data Type
Design Rationale
TVAdvertisementID
INT
NOT NULL
TVStationID
INT
NOT NULL
ScreeningTime
DATETIME
NOT NULL
Table3: CampaignResponse
Name
Data Type
Design Rationale
TVAdvertisementID
INT
NOT NULL
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
2.
2.
2.
3.
4.
L3-12
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
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
3.
2.
3.
L4-14
2.
3.
2.
3.
2.
3.
L4-15
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
2.
3.
4.
L5-16
1.
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.
2.
3.
2.
3.
2.
3.
Answer
504
10
5.178571
93
2.
3.
2.
L5-17
3.
4.
5.
6.
7.
8.
9.
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
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
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
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
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
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
2.
3.
4.
L6-20
2.
3.
4.
L6-21
2.
3.
4.
2.
3.
4.
2.
3.
4.
L6-22
2.
3.
4.
2.
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
L6-23
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
2.
3.
2.
X Task 3: View the estimated execution plan for script 7.2 using SHOWPLAN_XML
1.
2.
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.
L7-24
5.
6.
L7-25
Close the XML window and the execution plan window. If prompted to save changes, do not save the
changes.
2.
3.
4.
2.
2.
3.
X Task 7: Review the execution plans currently cached in memory using script 7.5
1.
2.
3.
2.
3.
4.
2.
3.
2.
3.
2.
3.
2.
L7-26
3.
2.
3.
2.
3.
Explain the results being returned. Compare this plan to the one returned in script 7.10.
4.
2.
3.
L7-27
2.
3.
Explain the results being returned. Compare this plan to the one returned in script 7.12.
4.
X Task 10: Explain the actual execution plan from script 7.14
1.
2.
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.
2.
3.
4.
2.
3.
L7-28
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
In Object Explorer, expand Proseware, expand Databases, expand MarketDev, expand Tables, and
expand Marketing.WebLog.
2.
2.
3.
2.
3.
4.
2.
3.
X Task 3: Test the design and explain why the index was not used
1.
2.
3.
4.
L8-30
L8-31
1.
In Object Explorer, expand Proseware, expand Databases, expand MarketDev, expand Tables, and
expand Marketing.PostalCode.
2.
X Task 3: Design a more appropriate index by following the Missing Index suggestion
1.
2.
3.
4.
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.
8.
9.
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
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.
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.
3.
4.
5.
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.
7.
In the Save As window, click Desktop in the left-hand pane under Favorites and click Save.
8.
9.
In SQL Server Management Studio, double-click 81 Lab Exercise 4.sql in Solution Explorer.
2.
3.
L8-33
1.
In SQL Server Profiler, from the File menu, click Stop Trace.
2.
3.
4.
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.
8.
In the Select databases and tables to tune list, check the MarketDev database.
9.
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.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
2.
3.
4.
Review the supplied design in the supporting documentation for the OnlineProducts and
AvailableModels views.
L9-34
2.
3.
2.
3.
L9-35
2.
3.
4.
Review the supplied design in the supporting documentation for the Contacts view.
2.
3.
2.
3.
L9-36
2.
3.
2.
3.
L9-37
L10-38
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
In Object Explorer, expand the Proseware server, expand Databases, right-click the MarketDev
database and click New Query.
2.
3.
L10-39
2.
3.
2.
3.
L10-40
2.
3.
In the toolbar, click Execute. Check that the procedure still executes without error.
2.
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.
2.
3.
In the toolbar, click Execute. Check that the procedure still executes without error.
L10-41
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
2.
Right-click dbo.StringListToTable, click Script Function as, click CREATE To, select New Query
Editor Window.
3.
2.
3.
L11-42
2.
3.
2.
3.
L11-43
Challenge Exercise 3: Use a TABLE Type with MERGE (Only if time permits)
X Task 1: Create a new TABLE type
1.
2.
3.
Review the supplied design in the supporting documentation for the Marketing.SalespersonMerge
Stored Procedure.
2.
3.
4.
2.
3.
L11-44
L12-45
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
Review the supplied function specifications in the supporting documentation in the Lab section of the
Student Manual, for the Phone Number.
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;
3.
L12-46
1.
In Object Explorer, expand Databases, right-click the MarketDev database and click New Query.
2.
3.
dbo.FormatPhoneNumber('+61 3 9485-2342');
dbo.FormatPhoneNumber('415 485-2342');
dbo.FormatPhoneNumber('(41) 5485-2342');
dbo.FormatPhoneNumber('94852342');
dbo.FormatPhoneNumber('85-2342');
Review the supplied function specifications in the supporting documentation in the Lab section of the
Student Manual, for the dbo.IntegerListToTable function.
In Object Explorer, expand Databases, then right-click the MarketDev database and click New
Query.
2.
L12-47
3.
In Object Explorer, expand Databases, then right-click the MarketDev database and click New
Query.
2.
3.
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.
3.
L12-48
Review the supplied problematic query in the supporting documentation in the Lab section of the
Student Manual.
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.
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.
3.
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:
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
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.
6.
7.
8.
9.
De-select all events except the Deadlock graph. You may need to scroll the window down to do this.
L13-51
In SQL Server Management Studio, double-click 51 Lab Exercise 1.sql in Solution Explorer.
2.
3.
In SQL Server Management Studio, double-click 52 Lab Exercise 1 2nd Window.sql in Solution
Explorer.
4.
5.
Execute 51 Lab Exercise 1.sql and then immediately execute 52 Lab Exercise 1 2nd
Window.sql. Wait for both to complete
Note
2.
3.
4.
5.
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.
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.
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.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
L14-52
1.
2.
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.
4.
Leave the query window open for use in the next exercise.
2.
3.
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.
L14-53
In SQL Server Management Studio, return to the query window with the
Marketing.MoveCampaignBalance_Test stored procedure code.
2.
3.
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.
You should see two messages each indicating that a single row has been updated.
L14-54
L15-55
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
2.
In Object Explorer, expand Proseware, expand Databases, expand MarketDev, expand Tables,
expand Marketing.CampaignBalance, and expand Columns.
3.
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
2.
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.
Note
The dates and times will depend upon when you execute the code.
L15-56
1.
In Object Explorer, expand Proseware, expand Databases, right-click the MarketDev database, and
click Refresh.
2.
3.
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
5.
L15-57
1.
In Object Explorer, expand Databases, right-click the MarketDev database and click New Query.
2.
3.
In Object Explorer, expand Databases, right-click the MarketDev database and click New Query.
2.
3.
Note The dates and times will depend upon when you execute the code.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
Review the supplied proposed SQLCLR functionality list in the supporting documentation.
Should be implemented
L16-58
RECONFIGURE;
GO
ALTER DATABASE MarketDev SET TRUSTWORTHY ON;
GO
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.
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.
3.
4.
5.
6.
7.
8.
9.
L16-60
10. Highlight the query above and in the toolbar click Execute.
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.
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.
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.
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.
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.
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.
value');
value';
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
Should be
implemented
Reason
Yes
No
Perhaps
Yes
2.
L17-62
L17-63
3.
Highlight and execute scripts 17.1 to 17.9 separately, comparing the results of each script with the
script comment.
4.
Output
17.1
17.2
17.3
17.4
17.5
17.6
17.7
17.8
17.9
2.
3.
Highlight and execute scripts 17.10 to 17.11 separately, comparing the results of each script with the
script comment.
4.
Query Title
Output
17.10
17.11
L17-64
2.
3.
Highlight and execute scripts 17.12 to 17.20 separately, comparing the results of each script with the
script comment.
4.
Query Title
Output
17.12
17.13
17.14
17.15
17.16
17.17
17.18
17.19
17.20
L18-65
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
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
18.2
18.3
18.4
18.5
18.6
(continued)
Query Number
Query Title
Output
18.7
PATH Mode.
18.8
ROOT directive.
18.9
Named element in
RAW modes.
Review the supplied stored procedure specification in the supporting documentation for
WebStock.GetAvailableModelsAsXML.
2.
3.
2.
3.
L18-66
L18-67
Review the supplied stored procedure specification in the supporting documentation for
Marketing.UpdateSalesTerritoriesByXML.
2.
3.
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.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
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.
Output
L19-68
19.1
Draw a square.
A square is drawn.
19.2
19.3
19.4
Multiple shapes.
19.5
Intersecting shapes.
19.6
19.7
Intersection of shapes.
19.8
Draw Australia.
19.9
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.
2.
2.
Review the supporting documentation for details of the required stored procedure.
2.
2.
2.
L19-70
L20-71
For this lab, you will use the available virtual machine environment. Before you begin the lab, you must
complete the following steps:
1.
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.
6.
7.
In Solution Explorer, double-click the query 00-Setup.sql. When the query window opens, click
Execute on the toolbar.
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.
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.
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.
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.
3.
Re-execute the query until the has_crawl_completed column shows the value 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.
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.
L20-72
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.
Right-click the MarketDev database and click New Query, then type the query below in the query
pane:
CREATE FULLTEXT STOPLIST CommonWords;
GO
2.
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.
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.
Review the supporting documentation for specification for a new stored procedure.
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.
L20-74