Tải bản đầy đủ (.pdf) (1,341 trang)

sql server 2005 bible

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (32.31 MB, 1,341 trang )

SQL Server

2005
Bible
Paul Nielsen
01_542567 ffirs.qxp 9/27/06 9:26 PM Page iii
SQL Server

2005
Bible
01_542567 ffirs.qxp 9/27/06 9:26 PM Page i
01_542567 ffirs.qxp 9/27/06 9:26 PM Page ii
SQL Server

2005
Bible
Paul Nielsen
01_542567 ffirs.qxp 9/27/06 9:26 PM Page iii
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS
OR WARRANTIES WITH RESPECT TO THE ACCURACY OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND
SPECIFICALLY DISCLAIM ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A
PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES OR PROMOTIONAL MATERIALS.
THE ADVICE AND STRATEGIES CONTAINED HEREIN MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS
SOLD WITH THE UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL, ACCOUNTING,
OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE IS REQUIRED, THE SERVICES OF A
COMPETENT PROFESSIONAL PERSON SHOULD BE SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE
LIABLE FOR DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS REFERRED TO IN
THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF FURTHER INFORMATION DOES NOT MEAN THAT
THE AUTHOR OR THE PUBLISHER ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE
OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEBSITES LISTED


IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT
IS READ.
SQL Server

2005 Bible
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana
Published by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN-13: 978-0-7645-4256-5
ISBN-10: 0-7645-4256-7
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1B/RW/RQ/QW/IN
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means,
electronic, mechanical, photocopying, recording, scanning or otherwise, except as permitted under Sections 107 or 108 of
the 1976 United States Copyright Act, without either the prior written permission of the Publisher, or authorization through
payment of the appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978)
750-8400, fax (978) 646-8600. Requests to the Publisher for permission should be addressed to the Legal Department, Wiley
Publishing, Inc., 10475 Crosspoint Blvd., Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at
/>For general information on our other products and services or to obtain technical support, please contact our Customer
Care Department within the U.S. at (800) 762-2974, outside the U.S. at (317) 572-3993 or fax (317) 572-4002.
Library of Congress Cataloging-in-Publication Data:
Nielsen, Paul.
SQL server 2005 bible / Paul Nielsen.
p. cm.

ISBN-13: 978-0-7645-4256-5 (paper/website)
ISBN-10: 0-7645-4256-7 (paper/website)
1. SQL server. 2. Client/server computing. 3. Database management. I. Title.
QA76.9.C55N55 2006
005.2’768—dc22
2006016151
Trademarks: Wiley and related trade dress are registered trademarks of Wiley Publishing, Inc., in the United States and
other countries, and may not be used without written permission. All other trademarks are the property of their respective
owners. Wiley Publishing, Inc., is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not be available in
electronic books.
01_542567 ffirs.qxp 9/27/06 9:26 PM Page iv
For Edie, my bride, who is so beautiful to me.
01_542567 ffirs.qxp 9/27/06 9:26 PM Page v
Credits
Executive Editor
Robert Elliott
Development Editors
Mark Cierzniak
Ami Frank Sullivan
Technical Editors
John Mueller
Kevin Lloyd
Chris Shaw
Production Editor
William A. Barton
Copy Editor
Luann Rouff
Editorial Manager
Mary Beth Wakefield

Production Manager
Tim Tate
Vice President and Executive Group
Publisher
Richard Swadley
Vice President and Executive
Publisher
Joseph B. Wikert
Project Coordinator
Ryan Steffen
Graphics and Production Specialists
Sean Decker
Lauren Goddard
Brooke Graczyk
Jennifer Mayberry
Jill Proll
Quality Control Technician
John Greenough
Media Development Specialists
Angela Denny
Kit Malone
Travis Silvers
Media Development Coordinator
Laura Atkinson
Proofreading and Indexing
Techbooks
01_542567 ffirs.qxp 9/27/06 9:26 PM Page vi
About the Author
Paul Nielsen is a hands-on database developer, Microsoft SQL Server MVP, author, and
trainer specializing in data architecture and Microsoft SQL Server technologies. Besides hold-

ing several certifications, he was the Design-SME (subject matter expert) for the Microsoft
Official Course 2784: Tuning and Optimizing Queries Using Microsoft SQL Server 2005.
Paul has been developing datacentric solutions since 1982, and was the Enterprise Data
Architect for Compassion International, a SQL Server instructor with Learning Tree, the
technical editor for a database magazine, and a U. S. Navy Submariner (Data Systems Tech).
He serves on the PASS (Professional Association for SQL Server) Board of Directors, is active
in the Colorado area SQL Server user groups, and organizes the Colorado PASS Camp. For
recreation, Paul enjoys scuba diving, playing guitar, and hiking/biking in the Front Range
mountains of Colorado. Paul regularly offers public and private workshops in SQL Server
development and data architecture, including the popular Advanced Design and Optimization
course. For more information visit his website at
www.SQLServerBible.com.
About the Co-Authors and Contributors
Hilary Cotter, Microsoft SQL Server MVP and author of Chapter 13, “Using Full-Text Search,”
and Chapter 39, “Replicating Data,” is director of Text Mining and Database Strategy for
RelevantNoise, which indexes blogs for business intelligence. He received a degree in
mechanical engineering from the University of Toronto, and subsequently studied economics
at the University of Calgary and computer science at the University of California (Berkeley).
He and his beautiful wife, Miriam, have five children ranging in age from 13 months to six
years. Hilary Cotter has a companion volume to this text on merge replication, which is in
the works for 2005.
Hilary has worked for Microsoft, Merrill Lynch, UBS-Paine Webber, MetLife, VoiceStream,
Tiffany & Co., Pacific Bell, Cahners, Novartis, Petro-Canada, Associated Press, and Johnson
and Johnson. You can find Hilary on the Microsoft newsgroups or contact him at hilary.

Monte Holyfield, author of Chapter 46, “Authoring Reports with Reporting Services,” and
Chapter 47, “Administering Reports with Reporting Services,” is the founder and manager of
MJH Software Solutions LLC (
www.mjhsoftware.com). He has over 10 years of experience
developing custom software and has served in many capacities including solution architect,

principal consultant, mentor, and project lead. He is a Microsoft Certified Professional and
has successfully leveraged the Microsoft.NET technologies to deliver outstanding solutions to
a diverse group of clients. Monte offers a unique combination of technical, analytical, and
leadership skills and shares a contagious “can do” attitude. When Monte is not building soft-
ware, he can most likely be found with his family doing something active such as riding dirt
bikes, skiing, windsurfing, or just enjoying the incredible Colorado outdoors.
Kevin Lloyd, Technical Editor, is an experienced SQL Server Developer and Data Architect,
Microsoft MCP, consultant and primary member of Data Solutions, LLC. He specializes in
data architecture and application integration, data modeling, data quality, and performance
tuning. Besides working with SQL Server, he develops C# XML Web Services for SQL Mobile
01_542567 ffirs.qxp 9/27/06 9:26 PM Page vii
viii
About the Co-Authors and Contributors
replication. He also enjoys mentoring others in SQL and writing Windows Mobile applica-
tions. Kevin has been developing data-centric solutions since 1996 in finance, e-commerce,
and government, and has worked or consulted with organizations such as Microsoft
Corporation; Frontier Airlines, Inc.; Compassion International, Inc.; and Keane, Inc. He can be
reached at When not writing code, he enjoys woodworking, spending
time with his family, and traveling.
George Mudrak, author of chapter 4, “Installing SQL Server 2005,” Chapter 5, “Client Software
Connectivity,” and Chapter 54, “Designing High-Performance Data Access Providers,” works
as an Enterprise Data Architect for Compassion International in Colorado Springs, CO. He has
been doing full life-cycle programming in a variety of languages for over 12 years, with his
most recent development activities using Microsoft’s .NET technologies. He is currently work-
ing within the Enterprise Information Architecture and Management (EIM/EIA) realm and has
worked at start-ups as well as major corporations. When he’s not working, George enjoys
spending time with his family, working out, and pursuing hobbies.
John Mueller, primary technical reviewer and co-author of Chapter 34, “Configuring SQL
Server,” Chapter 35, “Transferring Databases,” Chapter 37, “Maintaining the Database,”
Chapter 38, “Automating Database Maintenance with SQL Server Agent,” and Chapter 41,

“Administering SQL Server Express,” is a freelance author and technical editor. He has writing
in his blood, having produced 69 books and over 300 articles to date, with the topics that
range from networking to artificial intelligence and from database management to heads-
down programming. Some of his current books include a Windows power optimization book,
a book on .NET security, and books on Amazon Web Services, Google Web Services, and eBay
Web Services. He’s currently editor of the .NET electronic newsletter for Pinnacle Publishing.
See
www.freeenewsletters.com for more information. When John isn’t working at the com-
puter, he is likely in his workshop. An avid candle maker and woodworker, he can be found
working at a lathe or putting the finishing touches on a bookcase. His website is
www.mwt.net/~jmueller.
Darren Shaffer, Microsoft Compact Framework MVP, author of Chapter 26, “Developing for
SQL Server Mobile,” is the Chief Architect for .NET Solutions at Connected Innovation LLC
(
www.connectedinnovation.com). Darren is responsible for the design and development of
over 25 Compact Framework solutions for Fortune 1000 clients in the past four years. Darren
is a frequent speaker at Global Microsoft Events such as MEDC and TechEd. as well as a con-
tributor to MSDN, emphasizing the importance of the .NET Compact Framework,
SQL CE/SQL Mobile, and teaching mobile development best practices. Darren moderates
the MSDN SQL Mobile forum, and authors a well-traveled blog for .NET junkies at
www.
dotnetjunkies.com/WebLog/darrenshaffer/default.aspx. A West Point graduate and
retired Army Telecommunications Officer, Darren now lives in Colorado with his wife and
daughter.
Chris Shaw, technical editor and author of chapter 52, “Providing High Availability” is President
of SQL on Call in Colorado Springs, Colorado, for the past 5 years Chris has been working at
Premiere Global Services. He has also worked with Yellow Pages Inc, Ford Fairlane Motor
Credit, AdminiQuest, Wells Fargo and AT&T Wireless. Chris has published a number of Articles
for a number of magazines including SQL Server Magazine and SQL Standard Magazine.
He is a member of Rocky Mountain Microsoft Community Insiders group, is the president of the

Colorado Springs SQL Server Users Group, and is one of the organizers of the Colorado PASS
Camp. He has also been a guest speaker to a number of other user groups, is a regular speaker
at PASS Summits and SQL Connection, and presents web casts with SSWUG; recent presenta-
tions include Replication SQL Server 2005, Performance Metrics, and Crisis Management.
01_542567 ffirs.qxp 9/27/06 9:26 PM Page viii
ix
About the Co-Authors and Contributors
Andrew Watt, Microsoft SQL Server MVP, wrote Chapter 33, “InfoPath and SQL Server 2005.”
Andrew Watt is an independent consultant and experienced computer book author. He has
been a Microsoft MVP since 2004.
Michael White has focused on database development and administration since 1992.
Concentrating on Microsoft’s Business Intelligence (BI) tools and applications since 2000, he
has architected and implemented large warehousing and Analysis Services applications, as
well as nontraditional applications of BI tools. After many years in corporate IT and consult-
ing, Mike currently works as a software engineer for Wall Street On Demand. He is a strong
advocate for the underused BI toolset; currently leads the Boulder (Colorado) SQL Server
User Group and is one of the organizers of the Colorado PASS Camp. Mike wrote Chapter 42,
“ETL with Integration Services,” Chapter 43, “Business Intelligence with Analysis Services,”
Chapter 44, “Data Mining with Analysis Services,” Chapter 45, “Programming MDX Queries,”
and Chapter 48, “Analyzing Data with Excel and Data Analyzer.”
Bill Wunder, Microsoft SQL Server MVP, wrote Chapter 27, “Programming CLR Assemblies
within SQL Server,” Chapter 29, “Persisting Custom Data Types,” and Chapter 30,
“Programming with ADO.NET 2.0.” Bill’s introduction to set based technology began when he
was an AS/400 developer in 1993. The query processor at the time was built by an Australian
company, LANSA. The 4GL language was called Relational Database Markup Language (RDML).
It was an innovative product written in RPG that included commands like SELECT, INSERT,
UPDATE and DELETE. Shortly after that introduction he became the DBA for the company’s
sales force automation application. The database had just been upgraded to SQL Server 4.2.1
running on the Novell OS. He has been working with SQL Server since that time. He began
sharing his scripts on the Internet in 1995. He has published over 100 SQL Server related arti-

cles at various web sites. He has been a speaker at swynk.com’s Back Office Administrators
Conferences and Professional Association for SQL Server (PASS) conferences in the US and
Europe. He founded and has been a frequent speaker at the Boulder SQL Server Users Group.
He received Microsoft’s MVP award for his contributions to the SQL Server community in 2004,
2005 and 2006. Today Bill is a working DBA on the Dutch Antilles island of Curaçao. Readers
are invited to contact him at with any questions or comments.
01_542567 ffirs.qxp 9/27/06 9:26 PM Page ix
Contents at a Glance
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xli
Part I: Laying the Foundation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Chapter 1: The Information Architecture Principle . . . . . . . . . . . . . . . . . . . . . . . . 3
Chapter 2: Relational Database Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Chapter 3: Exploring SQL Server 2005 Architecture . . . . . . . . . . . . . . . . . . . . . . . 47
Chapter 4: Installing SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Chapter 5: Client Software Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Chapter 6: Using Management Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Part II: Manipulating Data with Select . . . . . . . . . . . . . . . . . . . . . . 115
Chapter 7: Understanding Basic Query Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Chapter 8: Using Expressions and Scalar Functions. . . . . . . . . . . . . . . . . . . . . . . 141
Chapter 9: Merging Data with Joins and Unions . . . . . . . . . . . . . . . . . . . . . . . . . 171
Chapter 10: Including Data with Subqueries and CTEs . . . . . . . . . . . . . . . . . . . . . 199
Chapter 11: Aggregating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Chapter 12: Navigating Hierarchical Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Chapter 13: Using Full-Text Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 257
Chapter 14: Creating Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Chapter 15: Working with Distributed Queries. . . . . . . . . . . . . . . . . . . . . . . . . . 295
Chapter 16: Modifying Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Part III: Developing with SQL Server . . . . . . . . . . . . . . . . . . . . . . . 353

Chapter 17: Implementing the Physical Database Schema . . . . . . . . . . . . . . . . . . . 355
Chapter 18: Programming with Transact-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . 397
Chapter 19: Performing Bulk Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427
Chapter 20: Kill the Cursor!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Chapter 21: Developing Stored Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Chapter 22: Building User-Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Chapter 23: Implementing Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Chapter 24: Exploring Advanced T-SQL Solutions . . . . . . . . . . . . . . . . . . . . . . . 499
Chapter 25: Creating Extensibility with a Data Abstraction Layer . . . . . . . . . . . . . . 539
Chapter 26: Developing for SQL Server Everywhere . . . . . . . . . . . . . . . . . . . . . . 549
Chapter 27: Programming CLR Assemblies within SQL Server. . . . . . . . . . . . . . . . . 589
02_542567 ftoc.qxp 9/27/06 9:29 PM Page x
Chapter 28: Queueing Data with Service Broker . . . . . . . . . . . . . . . . . . . . . . . . . 617
Chapter 29: Persisting Custom Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . 623
Chapter 30: Programming with ADO.NET 2.0. . . . . . . . . . . . . . . . . . . . . . . . . . . 643
Chapter 31: Using XML, XPath, and XQuery . . . . . . . . . . . . . . . . . . . . . . . . . . . 677
Chapter 32: Building an SOA Data Store with Web Services . . . . . . . . . . . . . . . . . . 689
Chapter 33: InfoPath and SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 695
PART IV: Enterprise Data Management . . . . . . . . . . . . . . . . . . . . . . 703
Chapter 34: Configuring SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705
Chapter 35: Transferring Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 741
Chapter 36: Recovery Planning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751
Chapter 37: Maintaining the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 775
Chapter 38: Automating Database Maintenance with SQL Server Agent . . . . . . . . . . . 795
Chapter 39: Replicating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 813
Chapter 40: Securing Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857
Chapter 41: Administering SQL Server Express . . . . . . . . . . . . . . . . . . . . . . . . . 889
Part V: Business Intelligence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 897
Chapter 42: ETL with Integration Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . 899
Chapter 43: Business Intelligence with Analysis Services . . . . . . . . . . . . . . . . . . . 935

Chapter 44: Data Mining with Analysis Services . . . . . . . . . . . . . . . . . . . . . . . . . 973
Chapter 45: Programming MDX Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 989
Chapter 46: Authoring Reports with Reporting Services . . . . . . . . . . . . . . . . . . . 1005
Chapter 47: Administering Reports with Reporting Services . . . . . . . . . . . . . . . . . 1035
Chapter 48: Analyzing Data with Excel and Data Analyzer . . . . . . . . . . . . . . . . . . 1051
Part VI: Optimization Strategies . . . . . . . . . . . . . . . . . . . . . . . . . 1063
Chapter 49: Measuring Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1065
Chapter 50: Query Analysis and Index Tuning . . . . . . . . . . . . . . . . . . . . . . . . . 1081
Chapter 51: Managing Transactions, Locking, and Blocking . . . . . . . . . . . . . . . . . 1105
Chapter 52: Providing High Availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1141
Chapter 53: Scaling Very Large Databases. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1153
Chapter 54: Designing High-Performance Data Access Providers . . . . . . . . . . . . . . 1173
Part VII: Appendixes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1197
Appendix A: SQL Server 2005 Specifications . . . . . . . . . . . . . . . . . . . . . . . . . . 1199
Appendix B: Sample Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1205
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1213
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xi
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xii
Contents
Foreword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxix
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xli
Part I: Laying the Foundation 1
Chapter 1: The Information Architecture Principle . . . . . . . . . . . . . . . . 3
Simplicity vs. Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Simplicity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
The Usability Objective. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Suitability of Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Data Store Configurations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

Master Data Stores Design Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Entity Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Domain Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Referential Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
User-Defined Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
Transactional Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Transactional Faults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
Isolation Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
The Value of Null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Set-Based Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Partitioning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Caching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Availability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Redundancy. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Recovery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Extensibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Abstraction Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Generalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Restricted Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Information Ownership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Audit Trails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Optimization Theory and SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Schema Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xiii
xiv

Contents
Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Advanced Scalability. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Chapter 2: Relational Database Modeling. . . . . . . . . . . . . . . . . . . . . 25
Modeling Reality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Visible Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Every Tuple (Row) Is an Island . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Tables, Rows, Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Identifying Multiple Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Multiple Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Relationships Between Objects. . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
Organizing or Grouping Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Consistent Lookup Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Complex Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Relational Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Secondary Entities and Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . 31
Relationship Cardinality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Relationship Optionality. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Data-Model Diagramming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
One-to-Many Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
One-to-One Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Supertype/Subtype Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Many-to-Many Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Category Entities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Reflexive Relationships . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

Entity/Attribute Design Principles . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Normal Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
Simplicity and Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
First Normal Form (1NF). . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
The Second Normal Form (2NF) . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
The Third Normal Form (3NF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
The Boyce-Codd Normal Form (BCNF) . . . . . . . . . . . . . . . . . . . . . . . 44
The Fourth Normal Form (4NF). . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
The Fifth Normal Form (5NF) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
Relational Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Chapter 3: Exploring SQL Server 2005 Architecture . . . . . . . . . . . . . . . 47
Data Access Architectures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
The Client/Server Database Model. . . . . . . . . . . . . . . . . . . . . . . . . . 49
N-Tier Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Service-Oriented Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
SQL Server Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Relational Engine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Transact-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Visual Studio and the CLR. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Service Broker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Replication Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xiv
xv
Contents
Full-Text Search. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Notification Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Server Management Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
SQL Server Agent. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56
Distributed Transaction Coordinator (DTC) . . . . . . . . . . . . . . . . . . . . 57

SQL Mail . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Business Intelligence Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Integration Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Reporting Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
Analysis Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
SQL Server 2005 Editions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Enterprise (Developer) Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Standard Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Workgroup Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
SQL Server Express Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
SQL Server Everywhere Edition . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
SQL Server Tools and Components . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
SQL Server Management Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
SQL Server Configuration Manager . . . . . . . . . . . . . . . . . . . . . . . . . 63
Surface Area Configuration Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Business Intelligence Development Studio . . . . . . . . . . . . . . . . . . . . . 64
SQL Integrated Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
SQL Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Performance Monitor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Database Tuning Advisor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Command-Line Utilities: SQLCmd, Bulk Copy. . . . . . . . . . . . . . . . . . . . 65
SQL Server 2005 Feature Pack . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
AdventureWorks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Exploring the Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
System Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Metadata Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Chapter 4: Installing SQL Server 2005 . . . . . . . . . . . . . . . . . . . . . . . 69
Planning Your Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Operating System. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

Security Accounts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
Authentication Mode. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Server Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
Hardware Recommendations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Dedicated Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Copious Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Using Multiple CPUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
Disk-Drive Subsystems. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
RAID Disk Subsystems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75
Network Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Performing the Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Attended Installations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Unattended Installations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Remote Installations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
Installing in a Clustered Environment . . . . . . . . . . . . . . . . . . . . . . . . 79
Installing Multiple Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Testing the Installation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xv
xvi
Contents
Upgrading from Previous Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
SQL Server 2005 Upgrade Advisor Tool . . . . . . . . . . . . . . . . . . . . . . . 80
Upgrading from SQL Server 2000 . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Migrating to SQL Server. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Migration Assistant . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
Schema Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Data Migration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Business Logic Conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Validation and Integration. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
Configuring the Surface Area of SQL Server . . . . . . . . . . . . . . . . . . . . . . . . 85

Surface Area Configuration Tool . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
Command Prompt Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Removing SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Chapter 5: Client Software Connectivity . . . . . . . . . . . . . . . . . . . . . 91
Enabling Server Connectivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Server Configuration Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
SQL Native Client Connectivity (SNAC) . . . . . . . . . . . . . . . . . . . . . . . 92
SQL Server Native Client Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
Database Mirroring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Asynchronous Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
Multiple Active Result Sets (MARS) . . . . . . . . . . . . . . . . . . . . . . . . . 95
XML Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
User-Defined Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Large Value Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
Handling Expired Passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Snapshot Isolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
Chapter 6: Using Management Studio . . . . . . . . . . . . . . . . . . . . . . . 99
Organizing the Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Window Placement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
The Context Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
The Summary Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
Registered Servers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Object Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Navigating the Tree . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
Filtering Object Explorer. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
The Table Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Building Database Diagrams. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

The Query Designer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Using the Query Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Connecting to a Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Opening a .sql File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Executing SQL Batches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Shortcuts and Bookmarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Viewing Query Execution Plans . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Using the Solution Explorer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Introducing the Templates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xvi
xvii
Contents
Part II: Manipulating Data with Select 115
Chapter 7: Understanding Basic Query Flow . . . . . . . . . . . . . . . . . . 117
Understanding Query Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Syntactical Flow of the Query Statement . . . . . . . . . . . . . . . . . . . . . 118
A Graphical View of the Query Statement . . . . . . . . . . . . . . . . . . . . . 118
Logical Flow of the Query Statement . . . . . . . . . . . . . . . . . . . . . . . . 119
Physical Flow of the Query Statement . . . . . . . . . . . . . . . . . . . . . . . 120
From Clause Data Sources. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Possible Data Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Named Ranges. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
[Table Name] . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Four-Part Table Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Where Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Using the Between Search Condition. . . . . . . . . . . . . . . . . . . . . . . . 123
Using the In Search Condition. . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Using the Like Search Condition . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Multiple Where Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

Select Where . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Ordering the Result Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
Specifying the Order by Using Column Names . . . . . . . . . . . . . . . . . . 132
Specifying the Order by Using Expressions . . . . . . . . . . . . . . . . . . . . 133
Specifying the Order by Using Column Aliases . . . . . . . . . . . . . . . . . . 134
Specifying the Order by Using Column Ordinal Position. . . . . . . . . . . . . 134
Order by and Collation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
Select Distinct. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
Ranking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
Top . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
The With Ties Option. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Chapter 8: Using Expressions and Scalar Functions . . . . . . . . . . . . . . 141
Building Expressions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Bitwise Operators. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
Case Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Simple Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Boolean Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Working with Nulls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Testing for Null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Handling Nulls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Scalar Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
User Information Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
Data-Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
String Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158
Soundex Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Data-Type Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Server Environment Information . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169

02_542567 ftoc.qxp 9/27/06 9:29 PM Page xvii
xviii
Contents
Chapter 9: Merging Data with Joins and Unions . . . . . . . . . . . . . . . . 171
Using Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
Inner Joins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174
Creating Inner Joins within SQL Code . . . . . . . . . . . . . . . . . . . . . . . 175
Number of Rows Returned. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
ANSI SQL 89 Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Multiple Table Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 180
Outer Joins and Optional Foreign Keys . . . . . . . . . . . . . . . . . . . . . . 182
Full Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
Placing the Conditions within Outer Joins. . . . . . . . . . . . . . . . . . . . . 185
Self-Joins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
Cross (Unrestricted) Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Exotic Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Θ (theta) Joins. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Multiple-Condition Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Non-Key Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Set Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Using Unions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195
Intersection Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Difference Union/Except . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198
Chapter 10: Including Data with Subqueries and CTEs . . . . . . . . . . . . 199
Methods and Locations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Simple Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
Common Table Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
Using Scalar Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

Using Subqueries as Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Using Subqueries as Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
Correlated Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
Relational Division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
Relational Division with a Remainder . . . . . . . . . . . . . . . . . . . . . . . 216
Exact Relational Division. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
Chapter 11: Aggregating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Simple Aggregations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221
Basic Aggregations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
Beginning Statistics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
Grouping within a Result Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
Simple Groupings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Aggravating Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Generating Totals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Rollup Subtotals. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232
Cube Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Computing Aggregates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Building Crosstab Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Fixed-Column Crosstab Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Dynamic Crosstab Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 240
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xviii
xix
Contents
Chapter 12: Navigating Hierarchical Data . . . . . . . . . . . . . . . . . . . . 243
Adjacency List Schema Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243
Basic Adjacency List Pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
Adjacency List Variations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245
Navigating the Adjacency List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248

Using a Standard select Statement . . . . . . . . . . . . . . . . . . . . . . . . . 248
Using a Recursive Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
Using a Set-Based Solution. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
Using a User-Defined Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 252
Using Recursive Common Table Expressions . . . . . . . . . . . . . . . . . . . 254
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
Chapter 13: Using Full-Text Search . . . . . . . . . . . . . . . . . . . . . . . . 257
Configuring Full-Text Search Catalogs . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Creating a Catalog with the Wizard . . . . . . . . . . . . . . . . . . . . . . . . . 259
Creating a Catalog with T-SQL Code . . . . . . . . . . . . . . . . . . . . . . . . 260
Pushing Data to the Full-Text Index. . . . . . . . . . . . . . . . . . . . . . . . . 261
Maintaining a Catalog with Management Studio . . . . . . . . . . . . . . . . . 262
Maintaining a Catalog in T-SQL Code . . . . . . . . . . . . . . . . . . . . . . . . 263
Noise Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
Word Searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
The Contains Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
ContainsTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266
Advanced Search Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Multiple Word Searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Searches with Wildcards . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Phrase Searches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Word-Proximity Searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Word-Inflection Searches. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Thesaurus Searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Variable-Word-Weight Searches . . . . . . . . . . . . . . . . . . . . . . . . . . . 272
Fuzzy Searches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
Freetext. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
FreetextTable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Binary Object Indexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Chapter 14: Creating Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Why Use Views? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279
Working with Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281
Creating Views Using Management Studio. . . . . . . . . . . . . . . . . . . . . 281
Creating Views with DDL Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 283
Order By and Views. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
View Restrictions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284
Executing Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Locking Down the View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Protecting the Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285
Protecting the View . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Updating through Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288
Nesting Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xix
xx
Contents
Using Synonyms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Chapter 15: Working with Distributed Queries . . . . . . . . . . . . . . . . . 295
Distributed Query Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295
Accessing a Local SQL Server Database . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Linking to External Data Sources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Linking with Management Studio . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Linking with T-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300
Linking with Non–SQL Server Data Sources . . . . . . . . . . . . . . . . . . . . 303
Developing Distributed Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305
Distributed Queries and Management Studio . . . . . . . . . . . . . . . . . . . 306
Distributed Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Local-Distributed Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306

Pass-Through Distributed Queries . . . . . . . . . . . . . . . . . . . . . . . . . 310
Distributed Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312
Distributed Transaction Coordinator. . . . . . . . . . . . . . . . . . . . . . . . 312
Developing Distributed Transactions . . . . . . . . . . . . . . . . . . . . . . . 313
Monitoring Distributed Transactions. . . . . . . . . . . . . . . . . . . . . . . . 314
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315
Chapter 16: Modifying Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Inserting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Inserting One Row of Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319
Inserting a Result Set from Select . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Inserting the Result Set from a Stored Procedure. . . . . . . . . . . . . . . . . 322
Creating a Default Row . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Creating a Table While Inserting Data . . . . . . . . . . . . . . . . . . . . . . . 325
Updating Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Updating a Single Table . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 328
Performing Global Search and Replaces . . . . . . . . . . . . . . . . . . . . . . 329
Referencing Multiple Tables While Updating Data . . . . . . . . . . . . . . . . 329
Deleting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
Referencing Multiple Tables While Deleting . . . . . . . . . . . . . . . . . . . . 333
Cascading Deletes. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Alternatives to Physically Deleting Data . . . . . . . . . . . . . . . . . . . . . . 335
Returning Modified Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Returning Data from an Insert . . . . . . . . . . . . . . . . . . . . . . . . . . . 336
Returning Data from an Update . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Returning Data from a Delete . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337
Returning Data into a @Table Variable . . . . . . . . . . . . . . . . . . . . . . . 337
Potential Data-Modification Obstacles. . . . . . . . . . . . . . . . . . . . . . . . . . . 338
Data Type/Length Obstacles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Primary Key Obstacles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339
Foreign Key Obstacles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343

Unique Index Obstacles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
Null and Default Obstacles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Check Constraint Obstacles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Instead of Trigger Obstacles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 346
After Trigger Obstacles. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347
Calculated Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 348
Non-Updateable View Obstacles . . . . . . . . . . . . . . . . . . . . . . . . . . 349
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xx
xxi
Contents
Views with the with check option Obstacles . . . . . . . . . . . . . . . . . . . 349
Security Obstacles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Part III: Developing with SQL Server 353
Chapter 17: Implementing the Physical Database Schema . . . . . . . . . . 355
Designing the Physical Database Schema . . . . . . . . . . . . . . . . . . . . . . . . . 356
Physical Schema Design Options . . . . . . . . . . . . . . . . . . . . . . . . . . 356
Refining the Data Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Designing for Performance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
Designing for Extensibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Responsible Denormalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . 358
Creating the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
The Create DDL Command. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Database-File Concepts. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Configuring File Growth . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Using Multiple Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Planning Multiple Filegroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Designing Tables Using Management Studio . . . . . . . . . . . . . . . . . . . 367
Working with SQL Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369

Schemas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Table and Column Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371
Filegroups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Creating Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Primary Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Creating Foreign Keys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Creating User-Data Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381
Column Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Calculated Columns. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Column Constraints and Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Data Catalog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390
DDL Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391
Creating and Altering DDL Triggers. . . . . . . . . . . . . . . . . . . . . . . . . 392
EventData() . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Enabling and Disabling DDL Triggers. . . . . . . . . . . . . . . . . . . . . . . . 394
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Chapter 18: Programming with Transact-SQL . . . . . . . . . . . . . . . . . . 397
Transact-SQL Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
T-SQL Batches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
T-SQL Formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Debugging T-SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Variables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Variable Default and Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402
Using the Set and Select Commands . . . . . . . . . . . . . . . . . . . . . . . . 402
Conditional Select. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
Using Variables within SQL Queries . . . . . . . . . . . . . . . . . . . . . . . . 404
Multiple Assignment Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 404
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xxi
xxii
Contents

Procedural Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
If . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
While . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 406
Goto. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
Examining SQL Server with Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
sp_help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Temporary Tables and Table Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Local Temporary Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Global Temporary Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Table Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412
Dynamic SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Executing Dynamic SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
sp_excecuteSQL. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Developing Dynamic SQL Code . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Error Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Try Catch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 416
Legacy @@Error Global Variable . . . . . . . . . . . . . . . . . . . . . . . . . . 418
@@RowCount Global Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Raiserror . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420
Catch Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
T-SQL Fatal Errors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425
Chapter 19: Performing Bulk Operations. . . . . . . . . . . . . . . . . . . . . 427
Bulk Insert. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Bulk Insert Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429
BCP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430
Chapter 20: Kill the Cursor!. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431
Anatomy of a Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431

The Five Steps to Cursoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Managing the Cursor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Update Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Cursor Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 434
Cursors and Transactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Cursor Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Complex-Logic Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
The Logic Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
SQL-92 Cursor with Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . 439
Fast-Forward Cursor with Stored Procedure . . . . . . . . . . . . . . . . . . . 439
Fast-Forward Cursor and User-Defined Function . . . . . . . . . . . . . . . . . 440
Update Cursor with Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . 441
Update Query with User-Defined Function . . . . . . . . . . . . . . . . . . . . 441
Multiple Queries. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Query with Case Expression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 443
Performance Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444
Denormalizing a List Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 447
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xxii
xxiii
Contents
Chapter 21: Developing Stored Procedures . . . . . . . . . . . . . . . . . . . 449
Managing Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Create, Alter, and Drop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 450
Returning a Record Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Compiling Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Stored Procedure Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
System Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Passing Data to Stored Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453
Input Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

Parameter Defaults . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
Returning Data from Stored Procedures. . . . . . . . . . . . . . . . . . . . . . . . . . 455
Output Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 455
Using the Return Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457
Path and Scope of Returning Data . . . . . . . . . . . . . . . . . . . . . . . . . 457
Using Stored Procedures within Queries . . . . . . . . . . . . . . . . . . . . . . . . . 458
Executing Remote Stored Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . 459
The Complete Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
The pGetPrice Stored Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . 460
The pOrder_AddNew Stored Procedure . . . . . . . . . . . . . . . . . . . . . . 462
The pOrder_AddItem Stored Procedure . . . . . . . . . . . . . . . . . . . . . . 466
Adding an Order. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Chapter 22: Building User-Defined Functions . . . . . . . . . . . . . . . . . . 471
Scalar Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Creating a Scalar Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Calling a Scalar Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Creating Functions with Schema Binding . . . . . . . . . . . . . . . . . . . . . 475
Inline Table-Valued Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Creating an Inline Table-Valued Function . . . . . . . . . . . . . . . . . . . . . 476
Calling an Inline Table-Valued Function . . . . . . . . . . . . . . . . . . . . . . 476
Using Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Correlated User Defined Functions . . . . . . . . . . . . . . . . . . . . . . . . . 478
Multi-Statement Table-Valued Functions . . . . . . . . . . . . . . . . . . . . . . . . . 480
Creating a Multi-Statement Table-Valued Function . . . . . . . . . . . . . . . . 480
Calling the Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Chapter 23: Implementing Triggers . . . . . . . . . . . . . . . . . . . . . . . . 483
Trigger Basics. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
Transaction Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484

Creating Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485
After Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Instead of Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 487
Trigger Limitations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Disabling Triggers. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Listing Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Triggers and Security. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489
Working with the Transaction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 490
Determining the Updated Columns . . . . . . . . . . . . . . . . . . . . . . . . . 490
Inserted and Deleted Logical Tables . . . . . . . . . . . . . . . . . . . . . . . . 491
Developing Multi-Row Enabled–Triggers. . . . . . . . . . . . . . . . . . . . . . 492
02_542567 ftoc.qxp 9/27/06 9:29 PM Page xxiii

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×