Tải bản đầy đủ (.pdf) (675 trang)

beginning t-sql with microsoft sql server 2005 and 2008

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 (14.18 MB, 675 trang )

Beginning
T-SQL
with Microsoft
®
SQL Server
®
2005 and 2008
www.wrox.com
$39.99 USA
$43.99 CAN
Wrox Beginning guides are crafted to make learning programming languages and technologies easier than you think, providing
a structured, tutorial format that will guide you through all the techniques involved.
Recommended
Computer Book
Categories
Database Management
General
ISBN: 978-0-470-25703-6
Nearly all business applications read, store, and manipulate data stored in
relational databases. If you use Microsoft SQL Server in any way, you need to
learn and use T-SQL, Microsoft’s powerful implementation of the ANSI-standard
SQL database query language.
This book teaches all of the basics of T-SQL as it’s used with SQL Server 2005
and 2008 databases. The authors, leading T-SQL experts, begin with the
essentials of SQL Server that are needed to get the most from T-SQL. They then
quickly move on to introduce T-SQL itself, including the core elements of data
retrieval, SQL functions, aggregation and grouping, and multi-table queries, and
they fully explain transaction processing and data manipulation using T-SQL.
The authors also show you how to create and manage T-SQL programming
objects, including views, functions, and stored procedures. They detail how to
optimize T-SQL query performance and design queries for real-world business


applications. All of the methods and techniques in this book can be used with
both Microsoft SQL Server 2005 and 2008 databases.
In addition, the book includes a comprehensive set of reference appendices,
including T-SQL command syntax, system variables and functions, system stored
procedures, information schema views, and FileStream objects.
What you will learn from this book
● How to add, modify, and remove records
● How to query multiple tables
● Ways to use views to modify data
● How to create tools for managing databases using T-SQL
● T-SQL programming techniques using views, user-defined functions, and stored
Enhance Your Knowledge
Advance Your Career
procedures
● Methods for optimizing query performance
● How to use SQL Server Reporting Services to visualize T-SQL query results
Who this book is for
This book is for beginning SQL Server developers and administrators who need to learn how to use T-SQL. Basic familiarity with
relational databases and a general understanding of basic SQL functions is necessary.
T-SQL
with Microsoft
®
SQL Server
®
2005 and 2008
Turley, Wood
Beginning
spine=1.344"
Updates, source code, and Wrox technical support at www.wrox.com
Beginning

T-SQL
with Microsoft
®
SQL Server
®
2005 and 2008
Paul Turley, Dan Wood
Wrox Programmer to Programmer
TM
Wrox Programmer to Programmer
TM
www.it-ebooks.info
ffirs.indd iiffirs.indd ii 10/31/08 6:32:53 PM10/31/08 6:32:53 PM
www.it-ebooks.info
Beginning
T-SQL with Microsoft® SQL Server® 2005 and 2008
Introduction xix
Chapter 1: Introducing T-SQL and Data Management Systems 1
Chapter 2: SQL Server Fundamentals 23
Chapter 3: SQL Server Tools 49
Chapter 4: Introducing the T-SQL Language 101
Chapter 5: Data Retrieval 129
Chapter 6: SQL Functions 165
Chapter 7: Aggregation and Grouping 219
Chapter 8: Multi-Table Queries 245
Chapter 9: Advanced Queries and Scripting 273
Chapter 10: Transactions 297
Chapter 11: Advanced Capabilities 329
Chapter 12: T-SQL Programming Objects 355
Chapter 13: Creating and Managing Database Objects 409

Chapter 14: Analyzing and Optimizing Query Performance 443
Chapter 15: T-SQL in Applications and Reporting 477
Appendix A: Command Syntax Reference 527
Appendix B: System Variables and Functions Reference 549
Appendix C: System Stored Procedure Reference 573
Appendix D: Information Schema Views Reference 595
Appendix E: FileStream Objects and Syntax 609
Appendix F: Answers to Exercises 613
Index 625
ffirs.indd iffirs.indd i 10/31/08 6:32:52 PM10/31/08 6:32:52 PM
www.it-ebooks.info
ffirs.indd iiffirs.indd ii 10/31/08 6:32:53 PM10/31/08 6:32:53 PM
www.it-ebooks.info
Beginning
T-SQL with Microsoft® SQL Server®
2005 and 2008
Paul Turley and Dan Wood
Wiley Publishing, Inc.
ffirs.indd iiiffirs.indd iii 10/31/08 6:32:53 PM10/31/08 6:32:53 PM
www.it-ebooks.info
Beginning T-SQL with Microsoft® SQL Server® 2005 and 2008
Published by
Wiley Publishing, Inc.
10475 Crosspoint Boulevard
Indianapolis, IN 46256
www.wiley.com
Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN: 978-0-470-25703-6
Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1
Library of Congress Cataloging-in-Publication Data is available from the publisher.
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 Permissions Department, John Wiley &
Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax (201) 748-6008, or online at
/>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.
For general information on our other products and services please contact our Customer Care Department
within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related
trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the
United States and other countries, and may not be used without written permission. Microsoft and SQL
Server are registered trademarks of Microsoft Corporation in the United States and/or other countries. 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.
ffirs.indd ivffirs.indd iv 10/31/08 6:32:53 PM10/31/08 6:32:53 PM
www.it-ebooks.info
My wife, Sherri, is the reason I do anything meaningful in my life.
The rest of it I pretty much come up with on my own.
Thank you for your love, support and the occasional
knock upside the head.
—Paul Turley
For my wonderful wife, Sarah: Without your constant
love and support, I could accomplish nothing.
You are truly my best friend.
—Dan Wood
ffirs.indd vffirs.indd v 10/31/08 6:32:53 PM10/31/08 6:32:53 PM
www.it-ebooks.info
V413HAV
About the Authors
Paul Turley (Vancouver, WA) is a Manager of Specialized Services for Hitachi Consulting Education
Services. Paul manages the Business Intelligence training team and teaches classes for companies
throughout the world on Microsoft SQL Server technologies. He works with companies to architect and
build BI and reporting solutions. He has been developing business database solutions since 1991 for
companies like Microsoft, Disney, Nike, and Hewlett - Packard. He has been a Microsoft Certified Trainer
since 1996 and holds several industry certifications, including MCTS and MCITP for BI, MCSD, MCDBA,
MSF Practitioner, and IT Project+.
Paul has authored and co - authored several books and courses on database, business intelligence, and
application development technologies. He is the lead courseware developer for the Hitachi Consulting
courses: SQL Server 2008 Business Intelligence Solutions and SQL Server 2008 Reporting Services
Solutions. Books include the prior edition of this book, the 2008, 2005 and 2000 editions of Professional
SQL Server Reporting Services , Beginning SQL Server 2005 Administration , Beginning Access 2002 VBA , Data
Warehousing with SQL Server 2000 Analysis Services , and Professional Access 2000 Programming — all from
Wrox. He is also a contributing author for SQL Server 2005 Integration Services Step by Step from

Microsoft Press.
Dan Wood (Silverdale, WA) is the senior database administrator for Avalara, a sales tax compliance
company, where he both administers and develops database solutions for several enterprise applications
that handle global address validation, tax rate calculation, and sales tax remittance for e - commerce and
ERP clients. He has been working with SQL Server as a DBA, consultant, and trainer since 1999. Dan was
a contributing author on Beginning Transact - SQL with SQL Server 2000 and 2005 and the lead author of
Beginning SQL Server Administration , both from Wrox.
ffirs.indd viffirs.indd vi 10/31/08 6:32:54 PM10/31/08 6:32:54 PM
www.it-ebooks.info
Executive Editor
Robert Elliott
Development Editor
John Sleeva
Technical Editor
David Norton
Production Editor
Daniel Scribner
Copy Editor
Nancy Rapoport
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, Cover
Lynsey Stanford
Proofreader

Publication Services , Inc.
Indexer
Robert Swanson
Credits
ffirs.indd viiffirs.indd vii 10/31/08 6:32:54 PM10/31/08 6:32:54 PM
www.it-ebooks.info
Acknowledgments
This book wouldn ’ t exist without the hard work and dedication of my coauthor, Dan Wood. Dan ’ s a
good friend, a true professional, a great father and husband - and I hear he ’ s an okay football coach.
Thanks to Bob Elliot and John Sleeva at Wrox who have been incredibly patient and professional with
two completely over - committed authors for the past year. DJ Norton did a great job with the technical
review. Thanks, DJ, for breaking my code and making more work for me. Thanks to Lance Baldwin and
Drew Naukam on Hitachi Consulting ’ s Microsoft Strategic Alliance team for giving me the space to
complete this and the Reporting Services book this year. To all of the amazing people I work with at
Hitachi Consulting, thanks for making this such a terrific organization for our clients and a great place to
call home.
— Paul Turley
I ’ d like to thank Paul Turley, who is not only a great friend, but an amazing person, and I appreciate the
opportunity to work with him again. Many thanks to our Wrox development editor, John Sleeva, who
did an outstanding editing job — not to mention the job he did working with us, which was probably
very much like herding cats. Special thanks go to the awesome development team at Avalara for a
rewarding and stimulating work environment and for giving me inspiration for many of the examples in
this book. Most important, I would like to thank my wonderful wife, Sarah, for all her patience and
support as I disappeared for hours at a time and spent many a late night trying to finish the latest
chapter. I would also like to thank my kids, Lukas, Tessa, and Caleb, who think it ’ s cool that Dad is
writing a book but would much prefer that I spend time with them.
— Dan Wood
ffirs.indd viiiffirs.indd viii 10/31/08 6:32:54 PM10/31/08 6:32:54 PM
www.it-ebooks.info
Contents

Introduction xix
Chapter 1: Introducing T-SQL and Data Management Systems 1
T-SQL Language 1
Programming Language or Query Language? 2
What’s New in SQL Server 2008 3
Database Management Systems 4
SQL Server as a Relational Database Management System 5
Tables 5
Relationships 14
RDBMS and Data Integrity 17
SQL Server and Other Products 20
Microsoft SQL Server 20
Oracle 20
IBM DB2 21
Informix 21
Sybase SQLAnywhere 21
Microsoft Access (Jet) 21
MySQL 22
Summary 22
Chapter 2: SQL Server Fundamentals 23
Who Uses SQL Server? 23
SQL Server Editions and Features 25
SQL Server Compact Edition 25
SQL Server Express Edition 25
SQL Server Workgroup Edition 26
SQL Server Standard Edition 26
SQL Server Enterprise Edition 26
Relational Database Engine 27
Semantics 27
Changing Terminology 28

Relationships 30
ftoc.indd ixftoc.indd ix 10/31/08 6:35:23 PM10/31/08 6:35:23 PM
www.it-ebooks.info
Contents
x
Primary Keys 31
Foreign Keys 32
Normalization Rules 33
First Normal Form 34
Second Normal Form 35
Third Normal Form 35
Boyce-Codd Normal Form, Fourth and Fifth Normal Form 35
Other Normal Forms 36
Transforming Information into Data 36
Applying Normalization Rules 37
Thinking Ahead 39
Multiple Associations 42
Multi-Valued Columns 43
To Normalize or To De-normalize? 44
Question Authority 44
The Mechanics of Query Processing 45
The AdventureWorks Databases 47
Summary 48
Chapter 3: SQL Server Tools 49
Common SQL Server Tasks 49
SQL Server Management Studio 51
Tool Windows 56
Toolbars 64
SQL Server Management Studio Configuration 71
SQL Server Business Intelligence Development Studio 75

SQL Server Profiler 76
Database Tuning Advisor 76
SQL Server Configuration Manager 76
Command-Line Tools 76
SQLCMD 76
Writing Queries 79
Scripting Options 81
Using the Graphical Query Designer 83
Using Templates 88
Using the Debug Feature 91
Summary 97
Exercises 97
Exercise 1 98
Exercise 2 98
Exercise 3 98
Exercise 4 99
ftoc.indd xftoc.indd x 10/31/08 6:35:23 PM10/31/08 6:35:23 PM
www.it-ebooks.info
Contents
xi
Chapter 4: Introducing the T-SQL Language 101
The Nature of SQL 102
Where to Begin? 103
Data Manipulation Language 103
Queries Have Layers 104
Set-Based Operations 104
Row-Based Operations 105
Query Syntax Basics 105
Naming Conventions 108
Object Delimiting 111

Commenting Script 112
Using Templates 114
Generating Script 115
Managing Script 115
Version Control 115
Data Definition Language 116
Creating a Table 117
Creating a View 117
Creating a Stored Procedure 118
Creating a Trigger 118
Creating a User-Defined Function 119
Scripting Practices 120
Data Control Language 124
Summary 126
Exercises 127
Exercise 1 127
Exercise 2 127
Chapter 5: Data Retrieval 129
Storage and Retrieval 129
The SELECT Statement 130
Choosing Columns 131
Column Aliasing 137
Calculated and Derived Columns 139
Filtering Rows 141
The WHERE Clause 141
Using Parentheses 156
Sorting Results 158
Top Values 160
Summary 162
Exercises 163

ftoc.indd xiftoc.indd xi 10/31/08 6:35:24 PM10/31/08 6:35:24 PM
www.it-ebooks.info
Contents
xii
Exercise 1 163
Exercise 2 163
Exercise 3 163
Exercise 4 163
Chapter 6: SQL Functions 165
The Anatomy of a Function 166
I’d Like to Have an Argument 166
Deterministic Functions 167
Using User Variables with Functions 168
Using Functions in Queries 169
Nested Functions 169
Aggregate Functions 170
The AVG() Function 171
The COUNT() Function 172
The MIN() and MAX() Functions 172
The SUM() Function 173
Configuration Variables 173
The @@ERROR Variable 173
The @@SERVICENAME Variable 175
The @@TOTAL_ERRORS Variable 175
The @@TOTAL_READ Variable 175
The @@VERSION Variable 175
Error Functions 176
Conversion Functions 178
The CAST() Function 178
The CONVERT() Function 181

The STR() Function 184
1.0000 184
Cursor Functions and Variables 184
The CURSOR_STATUS() Function 185
The @@CURSOR_ROWS Global Variable 185
The @@FETCH_STATUS Global Variable 185
Date Functions 185
The DATEADD() Function 186
The DATEDIFF() Function 187
The DATEPART() and DATENAME() Functions 190
The GETDATE() and GETUTCDATE() Functions 190
The SYSDATETIME() and SYSUTCDATETIME() Functions 191
The DAY(), MONTH(), and YEAR() Functions 191
String Manipulation Functions 191
The ASCII(), CHAR(), UNICODE(), and NCHAR() Functions 191
ftoc.indd xiiftoc.indd xii 10/31/08 6:35:24 PM10/31/08 6:35:24 PM
www.it-ebooks.info
Contents
xiii
The CHARINDEX() and PATINDEX() Functions 195
The LEN() Function 195
The LEFT() and RIGHT() Functions 196
The SUBSTRING() Function 197
The LOWER() and UPPER() Functions 198
The LTRIM() and RTRIM() Functions 201
The REPLACE() Function 201
The REPLICATE() and SPACE() Functions 202
The REVERSE() Function 202
The STUFF() Function 202
The QUOTENAME() Function 203

Mathematical Functions 203
Metadata Functions 204
Ranking Functions 207
The ROW_NUMBER() Function 207
The RANK() and DENSE_RANK() Functions 208
The NTILE(n) Function 210
Security Functions 210
System Functions and Variables 211
The COALESCE() Function 214
The DATALENGTH() Function 214
Global System Statistical Variables 216
Summary 216
Exercises 217
Exercise 1 217
Exercise 2 217
Exercise 3 217
Exercise 4 217
Exercise 5 217
Chapter 7: Aggregation and Grouping 219
To Group or Not to Group 219
Using Aggregate Functions 220
The COUNT() Function 221
The SUM() Function 222
The AVG() Function 222
Understanding Statistical Functions 223
The STDEV() Function 224
The STDEVP() Function 224
The VAR() Function 224
The VARP() Function 226
User-Defined Aggregate Functions 226

ftoc.indd xiiiftoc.indd xiii 10/31/08 6:35:24 PM10/31/08 6:35:24 PM
www.it-ebooks.info
Contents
xiv
Grouping Data 226
The GROUP BY Clause 226
The HAVING Clause 231
Total and Subtotal Group Modifiers 235
Subgrouping 236
The ROLLUP Clause 237
The CUBE Clause 238
The GROUPING() Function 238
The COMPUTE and COMPUTE BY Clauses 241
Summary 243
Exercises 244
Exercise 1 244
Exercise 2 244
Exercise 3 244
Chapter 8: Multi-Table Queries 245
Understanding Subqueries and Joins 248
Joining Tables in the WHERE Clause 248
Joining Tables in the FROM Clause 249
Types of Joins 250
Inner Joins 250
Outer Joins 251
Multicolumn Joins 256
Non-Equijoins 257
Special-Purpose Join Operations 258
Union Queries 266
Summary 271

Exercises 272
Exercise 1 272
Exercise 2 272
Exercise 3 272
Chapter 9: Advanced Queries and Scripting 273
Subqueries 274
Scalar Expressions 274
Alternate Join Operations 277
Correlated Subqueries 283
Business Cases for Subqueries 286
Common Table Expressions 289
Cursors 291
Rowset Versus Cursor Operations 292
ftoc.indd xivftoc.indd xiv 10/31/08 6:35:25 PM10/31/08 6:35:25 PM
www.it-ebooks.info
Contents
xv
Creating and Navigating a Cursor 292
Summary 295
Exercises 295
Exercise 1 295
Exercise 2 296
Exercise 3 296
Chapter 10: Transactions 297
Introducing Transactions 298
Transaction Types 298
The ACID Test 299
The Transaction Log 299
Logged Operations 300
Let’s Do CRUD with Data 300

Adding Records 301
Modifying Records 310
Removing Records 316
Automating Inserts, Updates, and Deletes with the MERGE Command 322
Explicit Transactions 325
Summary 327
Exercises 328
Exercise 1 328
Exercise 2 328
Exercise 3 328
Chapter 11: Advanced Capabilities 329
Pivoting Data 329
The PIVOT Operator 331
The UNPIVOT Operator 334
Full-Text Queries and Approximation Matching 336
Microsoft Search Service 337
Soundex Matching 337
The DIFFERENCE() Function 339
Managing and Populating Catalogs 340
Full-Text Query Expressions 347
Summary 353
Exercises 354
Exercise 1 354
Exercise 2 354
Exercise 3 354
ftoc.indd xvftoc.indd xv 10/31/08 6:35:25 PM10/31/08 6:35:25 PM
www.it-ebooks.info
Contents
xvi
Chapter 12: T-SQL Programming Objects 355

Views 356
Virtual Tables 356
Creating a View 357
Securing Data 369
Hiding Complexity 369
Modifying Data Through Views 371
Stored Procedures 372
Stored Procedures as Parameterized Views 372
Using Parameters 373
Returning Values 374
Record Maintenance 376
Handling and Raising Errors 381
Error Messages 381
Processing Business Logic 391
Looping 397
User-Defined Functions 399
Scalar Functions 399
Inline Table-Valued Functions 402
Multi-Statement Table-Valued Functions 403
Transaction Management 405
Locking Options 405
Summary 407
Exercises 408
Exercise 1 408
Exercise 2 408
Exercise 3 408
Chapter 13: Creating and Managing Database Objects 409
Data Definition Language 409
Creating Objects 410
Altering Objects 410

Dropping Objects 410
Naming Objects 410
Creating DDL Scripts 413
CREATE TABLE 414
Unique Identifiers 421
Constraints 422
CREATE VIEW 429
Indexed Views 432
ftoc.indd xviftoc.indd xvi 10/31/08 6:35:25 PM10/31/08 6:35:25 PM
www.it-ebooks.info
Contents
xvii
CREATE PROCEDURE 434
CREATE FUNCTION 438
IF EXISTS 438
Securing Database Objects 439
Managing Security Objects 440
Data Control Language 440
Summary 441
Exercises 442
Exercise 1 442
Exercise 2 442
Chapter 14: Analyzing and Optimizing Query Performance 443
Data Retrieval 443
Analyzing Queries 445
Session Options 447
Graphical Execution Plans 460
Writing Efficient T-SQL (Best Practices) 471
Writing Efficient Filters 472
Summary 476

Exercises 476
Exercise 1 476
Chapter 15: T-SQL in Applications and Reporting 477
Application Programming Models 477
Selecting a Model 478
Desktop Database Applications 481
Client/Server Database Solutions 481
n-tier Component Solutions 485
Web Server Applications 490
Multi-Tier Web Service Solutions 491
Multi-System Integrated Solutions 492
SQL Server 2008 Reporting Services 494
Reporting Services Architecture 494
SQL Server 2008 Report Design 495
Deploying the Report 518
Viewing the Report with Report Manager 519
Report Data Caching 519
Business Intelligence and Business Reporting 520
Report Application Integration 522
Summary 524
ftoc.indd xviiftoc.indd xvii 10/31/08 6:35:25 PM10/31/08 6:35:25 PM
www.it-ebooks.info
Contents
xviii
Appendix A: Command Syntax Reference 527
Appendix B: System Variables and Functions Reference 549
Appendix C: System Stored Procedure Reference 573
Appendix D: Information Schema Views Reference 595
Appendix E: FileStream Objects and Syntax 609
Appendix F: Answers to Exercises 613

Index 625
ftoc.indd xviiiftoc.indd xviii 10/31/08 6:35:26 PM10/31/08 6:35:26 PM
www.it-ebooks.info
Introduction
Welcome to the world of Transact - Structured Query Language programming with SQL Server 2005 and
2008. Transact - SQL, or T - SQL, is Microsoft Corporation ’ s powerful implementation of the ANSI standard
SQL database query language, which was designed to retrieve, manipulate, and add data to relational
database management systems (RDBMS).
You may already have a basic idea of what SQL is used for, but you may not have a good understanding
of the concepts behind relational databases and the purpose of SQL. This book will help you build a
solid foundation of understanding, beginning with core relational database concepts and continuing to
reinforce those concepts with real - world T - SQL query applications.
If you are familiar with relational database concepts but are new to Microsoft SQL Server or the T - SQL
language, this book will teach you the basics from the ground up. If you ’ re familiar with earlier versions
of SQL Server, it will get you up to speed on the newest features. And if you know SQL Server 2005,
you ’ ll learn about some exciting new capabilities in SQL Server 2008.
A popular online encyclopedia lists about 800 distinct programming languages in use today. These
languages are used to develop different types of applications for different types of computer systems
and specialized devices. Needless to say, we have a lot of software in our information - rich society.
Programming languages rapidly evolve and come and go, but one of few constants in the industry is
that most business applications read, store, and manipulate data — data stored in relational databases.
If you use Microsoft SQL Server in any capacity, the need to learn and use T - SQL is inescapable.
Amazing things are possible with just a few keystrokes of powerful SQL script.
Indeed, SQL is one of the few standard languages in the industry that doesn ’ t come and go and has
remained constant over the decades. The capabilities of T - SQL expand as features are added to each
version of the SQL Server product. The concepts and exercises in this book will help you to understand
and use the core language and its latest features.
Who This Book Is For
Information Technology professionals in many different roles use T - SQL. Our goal is to provide a guide
and a reference for IT pros across the spectrum of operational database solution design, database

application development, and reporting and business intelligence solutions.
Database solution designers will find this book to be a thorough introduction and comprehensive
reference for all aspects of database modeling, design, object management, query design, and advanced
query concepts.
Application developers who write code to manage and consume SQL Server data will benefit from our
thorough coverage of basic data management and simple and advanced query design. Several examples
of ready - to - use code are provided to get you started and to continue to support applications with
embedded T - SQL queries.
flast.indd xixflast.indd xix 10/31/08 6:33:25 PM10/31/08 6:33:25 PM
www.it-ebooks.info
Introduction
xx
Report designers will find this book to be a go - to reference for report query design. You will build on a
thorough introduction to basic query concepts and learn to write efficient queries to support business
reports and advanced analytics.
Finally, database administrators who are new to SQL Server will find this book to be an all - inclusive
introduction and reference of mainstream topics. This can assist you as you support the efforts of other
team members. Beyond the basics of database object management and security concepts, we recommend
Beginning SQL Server 2005 Administration and Beginning SQL Server 2008 Administration from Wrox,
co - authored in part by the same authors.
What This Book Covers
This book introduces the T - SQL language and its many uses, and serves as a comprehensive guide at a
beginner through intermediate level. Our goal in writing this book was to cover all the basics thoroughly
and to cover the most common applications of T - SQL at a deeper level. Depending on your role and skill
level, this book will serve as a companion to the other Wrox books in the Microsoft SQL Server Beginning
and Professional series. Check the back cover of this book for a road map of other complementary books
in the Wrox series.
This book will help you to learn:
How T - SQL provides you with the means to create tools for managing databases of different
size, scope, and purpose

Various programming techniques that use views, user - defined functions, and stored procedures
Ways to optimize query performance
How to create databases that will be an essential foundation to applications you develop later
How This Book Is Structured
Each section of this book organizes topics into logical groups so the book can be read cover - to - cover or
used as a reference guide for specific topics.
We start with an introduction to the T - SQL language and data management systems, and then continue
with the SQL Server product fundamentals. This first section teaches the essentials of the SQL Server
product architecture and relational database design principles. This section (Chapters 1 – 3) concludes
with an introduction to the SQL Server administrator and developer tools.
The next section, encompassing Chapters 4 through 9, introduces the T - SQL language and teaches the
core components of data retrieval, SQL functions, aggregation and grouping, and multi - table queries. We
start with the basics and build on the core structure of the SQL
SELECT statement, progressing to
advanced forms of
SELECT queries.




flast.indd xxflast.indd xx 10/31/08 6:33:25 PM10/31/08 6:33:25 PM
www.it-ebooks.info
Introduction
xxi
Chapter 10 introduces transactions and data manipulation. You will learn how the INSERT , UPDATE , and

DELETE statements interact with the relational database engine and transaction log to lock and modify
data rows with guaranteed consistency. You will not only learn to use correct SQL syntax but will
understand how this process works in simple terms.
More advanced topics in the concluding section will teach you to create and manage T - SQL

programming objects, including views, functions, and stored procedures. You learn to optimize query
performance and use T - SQL in application design, applying the query design basics to real - world
business solutions. Chapter 15 contains a complete tutorial on using SQL Server 2008 Reporting Services
to visualize data from the T - SQL queries you create.
The book concludes with a comprehensive set of reference appendixes for command syntax, system
stored procedures, information schema views, file system commands, and system management
commands.
What You Need to Use This Book
The material in this book applies to all editions of Microsoft SQL Server 2005 and 2008. To use all the
features discussed, we recommend that you install the Developer Edition, although you can also use
the Enterprise, Standard, or Workgroup editions.
SQL Server 2005 Developer Edition or SQL Server 2008 Developer Edition can be installed on a desktop
computer running Windows 2000, Windows XP, or Windows Vista. You can also use Windows 2000
Server, Windows Server 2003, or Windows Server 2008 with the Enterprise or Standard edition. The
SQL Server client tools must be installed on your desktop computer and the SQL Server relational
database server must be installed on either your desktop computer or on a remote server with network
connectivity and permission to access.
Consult
www.microsoft.com/sql for information about the latest service packs, specific
compatibilities, and minimum recommend system requirements.
The examples throughout this book use the following sample databases, which are available to
download from Microsoft: the sample database for SQL Server 2005 is called AdventureWorks , and the
sample database for SQL Server 2008 is called AdventureWorks2008 . Because the structure of these
databases differs significantly, separate code samples are provided throughout the book for these two
version - specific databases.
An example using the AdventureWorks2008DW database for SQL Server 2008 is also used in Chapter 15.
To download and install these sample databases, browse
www.codeplex.com .
Conventions
To help you get the most from the text and keep track of what ’ s happening, we ’ ve used a number of

conventions throughout the book.
flast.indd xxiflast.indd xxi 10/31/08 6:33:26 PM10/31/08 6:33:26 PM
www.it-ebooks.info
Introduction
xxii
Try It Out
The Try It Out is an exercise you should work through, following the text in the book.
1. They usually consist of a set of steps.
2. Each step has a number.
3. Follow the steps through with your copy of the database.
Notes, tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.
As for styles in the text:
W e highlight new terms and important words when we introduce them.
We show keyboard strokes like this: Ctrl+A.
We show filenames, URLs, and code within the text like so:
persistence.properties .
We present code in two different ways:

We use a monofont type with no highlighting for most code examples.
We use gray highlighting to emphasize code that’s particularly important in
the present context.

Source Code
As you work through the examples in this book, you may choose either to type in all the code manually
or to use the source code files that accompany the book. All the source code used in this book is available
for download at
www.wrox.com . Once at the site, simply locate the book ’ s title (either by using the
Search box or by using one of the title lists) and click the Download Code link on the book ’ s detail page
to obtain all the source code for the book.
Because many books have similar titles, you may find it easiest to search by ISBN; this book ’ s ISBN is

978 - 0 - 470 - 25703 - 6.




Boxes like this one hold important, not - to - be forgotten information that is directly
relevant to the surrounding text.
flast.indd xxiiflast.indd xxii 10/31/08 6:33:26 PM10/31/08 6:33:26 PM
www.it-ebooks.info
Introduction
xxiii
Once you download the code, just decompress it with your favorite compression tool. Alternatively, you
can go to the main Wrox code download page at
www.wrox.com/dynamic/books/download.aspx
to see the code available for this book and all other Wrox books.
Errata
We make every effort to ensure that there are no errors in the text or in the code. However, no one is
perfect, and mistakes do occur. If you find an error in one of our books, like a spelling mistake or faulty
piece of code, we would be very grateful for your feedback. By sending in errata you may save another
reader from hours of frustration and at the same time you will be helping us provide even higher quality
information.
To find the errata page for this book, go to
www.wrox.com and locate the title using the Search box or
one of the title lists. Then, on the book details page, click the Book Errata link. On this page you can
view all errata that has been submitted for this book and posted by Wrox editors. A complete book list
including links to each book ’ s errata is also available at
www.wrox.com/misc - pages/booklist.shtml .
If you don ’ t spot “ your ” error on the Book Errata page, go to
www.wrox.com/contact/techsupport
.shtml

and complete the form there to send us the error you have found. We ’ ll check the information
and, if appropriate, post a message to the book ’ s errata page and fix the problem in subsequent editions
of the book.
p2p.wrox.com
For author and peer discussion, join the P2P forums at p2p.wrox.com . The forums are a Web - based
system for you to post messages relating to Wrox books and related technologies and interact with other
readers and technology users. The forums offer a subscription feature to e - mail you topics of interest of
your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts,
and your fellow readers are present on these forums.
At
you will find a number of different forums that will help you not only as
you read this book, but also as you develop your own applications. To join the forums, just follow
these steps:
1. Go to p2p.wrox.com and click the Register link.
2. Read the terms of use and click Agree.
3. Complete the required information to join as well as any optional information you wish to
provide, and click Submit.
4. You will receive an e - mail with information describing how to verify your account and complete
the joining process.
You can read messages in the forums without joining P2P but in order to post your own messages, you
must join.
flast.indd xxiiiflast.indd xxiii 10/31/08 6:33:26 PM10/31/08 6:33:26 PM
www.it-ebooks.info

×