SQL Server 2012 T-SQL Fundamentals

2y ago
7 Views
2 Downloads
6.66 MB
442 Pages
Last View : 3m ago
Last Download : 3m ago
Upload by : Lilly Kaiser
Transcription

www.allitebooks.com

www.allitebooks.com

Microsoft SQL Server2012 T-SQL Fundamentals Itzik Ben-Ganwww.allitebooks.com

Published with the authorization of Microsoft Corporation by:O’Reilly Media, Inc.1005 Gravenstein Highway NorthSebastopol, California 95472Copyright 2012 by Itzik Ben-GanAll rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by anymeans without the written permission of the publisher.ISBN: 978-0-735-65814-1123456789 M 765432Printed and bound in the United States of America.Microsoft Press books are available through booksellers and distributors worldwide. If you need support relatedto this book, email Microsoft Press Book Support at mspinput@microsoft.com. Please tell us what you think ofthis book at soft and the trademarks listed at ctualProperty/Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property oftheir respective owners.The example companies, organizations, products, domain names, email addresses, logos, people, places, andevents depicted herein are ictitious. No association with any real company, organization, product, domain name,email address, logo, person, place, or event is intended or should be inferred.This book expresses the author’s views and opinions. The information contained in this book is provided withoutany express, statutory, or implied warranties. Neither the author, O’Reilly Media, Inc., Microsoft Corporation, norits resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly orindirectly by this book.Acquisitions and Developmental Editor: Russell JonesProduction Editor: Kristen BorgEditorial Production and Illustration: Online Training Solutions, Inc.Technical Reviewer: Gianluca Hotz and Herbert AlbertCopyeditor: Kathy KrauseIndexer: Allegro Technical IndexingCover Design: Twist Creative SeattleCover Composition: Karen Montgomerywww.allitebooks.com

To DatoTo live in hearts we leave behind,Is not to die.—Thomas Campbellwww.allitebooks.com

www.allitebooks.com

Contents at a GlanceForewordxixIntroductionxxiChapTer 1Background to T-SQL Querying and programmingChapTer 2Single-Table Queries1ChapTer 3JoinsChapTer 4Subqueries129ChapTer 5Table expressions157ChapTer 6Set Operators191ChapTer 7Beyond the Fundamentals of Querying211ChapTer 8Data Modiication247ChapTer 9Transactions and Concurrency297ChapTer 10programmable Objects339appendIx aGetting Started375Index397About the Author4132799www.allitebooks.com

www.allitebooks.com

ContentsForeword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xixIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiChapter 1Background to T-SQL Querying and Programming1Theoretical Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2Set Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3Predicate Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4The Relational Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4The Data Life Cycle. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9SQL Server Architecture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12The ABC Flavors of SQL Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12SQL Server Instances . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15Schemas and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .18Creating Tables and Deining Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . .19Creating Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .19Deining Data Integrity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25Chapter 2Single-Table Queries27Elements of the SELECT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .27The FROM Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .29The WHERE Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31The GROUP BY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .32What do you think of this book? We want to hear from you!Microsoft is interested in hearing your feedback so we can continually improve ourbooks and learning resources for you. To participate in a brief online survey, please tebooks.com

The HAVING Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36The SELECT Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .36The ORDER BY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42The TOP and OFFSET-FETCH Filters . . . . . . . . . . . . . . . . . . . . . . . . . . .44A Quick Look at Window Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .48Predicates and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .50CASE Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .53NULL Marks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55All-at-Once Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59Working with Character Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61Collation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .62Operators and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .64The LIKE Predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .71Working with Date and Time Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73Date and Time Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .73Literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74Working with Date and Time Separately . . . . . . . . . . . . . . . . . . . . . . . 78Filtering Date Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .79Date and Time Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .80Querying Metadata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88Catalog Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .88Information Schema Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .89System Stored Procedures and Functions . . . . . . . . . . . . . . . . . . . . . .89Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .91Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .911 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .912 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .923 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .924 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .925 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .936 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .937 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .948 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .94viiiContentswww.allitebooks.com

Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .951 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .952 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .953 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .964 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .965 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .976 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .977 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .988 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .98Chapter 3Joins99Cross Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .99ANSI SQL-92 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .100ANSI SQL-89 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101Self Cross Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .101Producing Tables of Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .102Inner Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103ANSI SQL-92 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .103ANSI SQL-89 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105Inner Join Safety . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105More Join Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106Composite Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .106Non-Equi Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .107Multi-Join Queries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .109Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110Fundamentals of Outer Joins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .110Beyond the Fundamentals of Outer Joins . . . . . . . . . . . . . . . . . . . . .113Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .120Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1201-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1201-2 (Optional, Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1212 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1223 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1234 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .123Contentsix

5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1236 (Optional, Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1247 (Optional, Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .125Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1251-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1251-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1262 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1263 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1274 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1275 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1276 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1287 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .128Chapter 4Subqueries129Self-Contained Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .129Self-Contained Scalar Subquery Examples . . . . . . . . . . . . . . . . . . . .130Self-Contained Multivalued Subquery Examples . . . . . . . . . . . . . . .132Correlated Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .136The EXISTS Predicate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .138Beyond the Fundamentals of Subqueries . . . . . . . . . . . . . . . . . . . . . . . . . . .140Returning Previous or Next Values . . . . . . . . . . . . . . . . . . . . . . . . . . .140Using Running Aggregates. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .141Dealing with Misbehaving Subqueries . . . . . . . . . . . . . . . . . . . . . . . .142Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .147Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1471 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1472 (Optional, Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1483 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1494 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1495 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1506 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1507 (Optional, Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1518 (Optional, Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151xContents

Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1521 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1522 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1523 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1534 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1535 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1536 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1547 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1548 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .155Chapter 5Table Expressions157Derived Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .157Assigning Column Aliases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .159Using Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161Nesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .161Multiple References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .162Common Table Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .163Assigning Column Aliases in CTEs . . . . . . . . . . . . . . . . . . . . . . . . . . . .164Using Arguments in CTEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165Deining Multiple CTEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .165Multiple References in CTEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166Recursive CTEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .166Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .169Views and the ORDER BY Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . .170View Options. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .172Inline Table-Valued Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176The APPLY Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .178Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .181Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1821-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1821-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1822-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1832-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1833 (Optional, Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .184Contentsxi

4-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1844-2 (Optional, Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1855-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1865-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .186Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1871-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1871-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1872-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1872-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1883 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1884-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1894-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1895-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1905-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .190Chapter 6Set Operators191The UNION Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .192The UNION ALL Multiset Operator . . . . . . . . . . . . . . . . . . . . . . . . . . .192The UNION Distinct Set Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . .193The INTERSECT Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .194The INTERSECT Distinct Set Operator . . . . . . . . . . . . . . . . . . . . . . . .195The INTERSECT ALL Multiset Operator . . . . . . . . . . . . . . . . . . . . . . .195The EXCEPT Operator. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .198The EXCEPT Distinct Set Operator . . . . . . . . . . . . . . . . . . . . . . . . . . .198The EXCEPT ALL Multiset Operator . . . . . . . . . . . . . . . . . . . . . . . . . .199Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .200Circumventing Unsupported Logical Phases . . . . . . . . . . . . . . . . . . . . . . . .202Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .204Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2041 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2042 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2043 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2064 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2065 (Optional, Advanced) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .206xiiContents

Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2081 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2082 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2093 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2094 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2095 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .210Chapter 7Beyond the Fundamentals of Querying211Window Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .211Ranking Window Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .214Offset Window Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .217Aggregate Window Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .220Pivoting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .222Pivoting with Standard SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .224Pivoting with the Native T-SQL PIVOT Operator . . . . . . . . . . . . . . .225Unpivoting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .228Unpivoting with Standard SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .229Unpivoting with the Native T-SQL UNPIVOT Operator . . . . . . . . . .231Grouping Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .232The GROUPING SETS Subclause . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234The CUBE Subclause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .234The ROLLUP Subclause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .235The GROUPING and GROUPING ID Functions . . . . . . . . . . . . . . . .236Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .239Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2391 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2392 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2403 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2404 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2415 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .242What do you think of this book? We want to hear from you!Microsoft is interested in hearing your feedback so we can continually improve ourbooks and learning resources for you. To participate in a brief online survey, please i

Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2431 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2432 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2433 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2434 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2455 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .246Chapter 8Data Modiication247Inserting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247The INSERT VALUES Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .247The INSERT SELECT Statement. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .249The INSERT EXEC Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .250The SELECT INTO Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .251The BULK INSERT Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .252The Identity Property and the Sequence Object . . . . . . . . . . . . . . .252Deleting Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .261The DELETE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .262The TRUNCATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263DELETE Based on a Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .263Updating Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .264The UPDATE Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .265UPDATE Based on a Join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .267Assignment UPDATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269Merging Data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .270Modifying Data Through Table Expressions . . . . . . . . . . . . . . . . . . . . . . . .274Modiications with TOP and OFFSET-FETCH . . . . . . . . . . . . . . . . . . . . . . . 277The OUTPUT Clause . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .280INSERT with OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280DELETE with OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282UPDATE with OUTPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283MERGE with OUTPUT. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284Composable DML. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .285Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .287xivContents

Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2871 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2871-1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2881-2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2881-3 . . . . . . . . . . . . . . . . . . . .

Contents at a Glance Foreword xix Introduction xxi ChapTer 1 Background to T-SQL Querying and programming 1 ChapTer 2 Single-Table Queries 27 ChapTer 3 Joins 99 ChapTer 4 Subqueries 129 ChapTer 5 Table expressions 157 ChapTer 6 Set Operators 191 ChapTer 7 Beyond the Fundamentals of Querying 211 ChapTer 8 Data M

Related Documents:

Server 2005 , SQL Server 2008 , SQL Server 2008 R2 , SQL Server 2012 , SQL Server 2014 , SQL Server 2005 Express Edition , SQL Server 2008 Express SQL Server 2008 R2 Express , SQL Server 2012 Express , SQL Server 2014 Express .NET Framework 4.0, .NET Framework 2.0,

SQL Server 2005 SQL Server 2008 (New for V3.01) SQL Server 2008 R2 (New for V3.60) SQL Server 2012 (New for V3.80) SQL Server 2012 R2 (New for V3.95) SQL Server 2014 (New for V3.97) SQL Server 2016 (New for V3.98) SQL Server 2017 (New for V3.99) (Recommend Latest Service Pack) Note: SQL Server Express is supported for most situations. Contact .

MS SQL Server: MS SQL Server 2017, MS SQL Server 2016, MS SQL Server 2014, MS SQL Server 2012, MS SQL Server 2008 R2, 2008, 2008 (64 bit), 2008 Express, MS SQL Server 2005, 2005 (64 bit), 2005 Express, MS SQL Server 2000, 2000 (64 bit), 7.0 and mixed formats. To install the software, follow the steps: 1. Double-click Stellar Repair for MS SQL.exe.

Bitlocker Administration and Monitoring MBAM Anforderungen: Hardware: 4 CPUs 12 GB RAM 100 GB Disk OS: Windows Server 2008 R2 SP1 Windows Server 2012/R2 Windows Server 2016 SQL Server: SQL Server 2008 R2 SQL Server 2012 SP1 SQL Server 2012 SP2 SQL Server 2014 SQL Server 2014 SP1

4. To upgrade SQL Server 2008 SP3 Express to SQL Server 2012 SP2 Express, launch th e installer for SQL Server 2012 SP2 Express, and then follow the wizard. 5. Select the Upgrade from SQL Server 2005, SQL Server 2008 or SQL Server 2008R2 option under the Installation option. The Upgrade to SQL

Business Intelligence SQL Server 2012 as well as Standard Edition Servers and legacy Enterprise Edition Servers. How to license 1. License each individual server with a SQL server license 2. License each user or device accessing the server with a SQL Server CAL SQL Server 2012 CALs can be used to access any SQL Server database licensed per .

4395 querying data with transact -sql (m20761) microsoft sql server 6552 querying microsoft sql server 2014 (m20461) microsoft sql server 1833 sql server performance tuning and optimization (m55144) microsoft sql server 4394 updating your skills to sql server 2016 (m10986) microsoft sql server

install the SQL Server Reporting Services 2019 or 2017 installation program. For more information, see Installing SQL Server Reporting Services for SQL Server 2019 and SQL Server 2017 To use the SQL Installer to install a full version of SQL Server software (for example, SQL Server 2019 Standard editio