www.it-ebooks.info Professional Microsoft ® SQL Server ® 2008 Programming Professional Microsoft SQL Server 2008 Integration Services 978-0-470-24795-2 This book shows developers how to master the 2008 release of SSIS, covering topics including data warehousing with SSIS, new methods of managing the SSIS platform, and improved techniques for ETL operations. Professional SQL Server 2008 Reporting Services 978-0-470-24201-8 This book teaches solutions architects, designers, and developers how to use Microsoft’s reporting platform to create reporting and business intelligence solutions. Professional Microsoft SQL Server 2008 Analysis Services 978-0-470-24798-3 Professional Microsoft SQL Server 2008 Analysis Services shows readers how to build data warehouses and multidimensional databases, query databases, and use Analysis Services and other components of SQL Server to provide end-to-end solutions. Professional Microsoft SQL Server 2008 Programming 978-0-470-25702-9 This updated new edition of Wrox’s best-selling SQL Server book has been expanded to include coverage of SQL Server 2008’s new datatypes, new indexing structures, manageability features, and advanced time-zone handling. Professional Microsoft SQL Server 2008 Administration 978-0-470-24796-9 A how-to guide for experienced database administrators, this book is loaded with unique tips, tricks, and workarounds for handling the most difficult SQL Server administration issues. The authors discuss data capture, performance studio, Query Governor, and new techniques for monitoring and policy management. Beginning Microsoft SQL Server 2008 Programming 978-0-470-25701-2 This comprehensive introduction to SQL Server covers the fundamentals and moves on to discuss how to create and change tables, manage keys, write scripts, work with stored procedures, and much more. Beginning T-SQL with Microsoft SQL Server 2005 and 2008 978-0-470-25703-6 Beginning T-SQL with Microsoft SQL Server 2005 and 2008 provides a comprehensive introduction to the T-SQL programming language, with concrete examples showing how T-SQL works with both SQL Server 2005 and SQL Server 2008. Beginning Database Design Solutions 978-0-470-38549-4 Beginning Database Design Solutions introduces IT professionals—both DBAs and database developers—to database design. It explains what databases are, their goals, and why proper design is necessary to achieve those goals. It tells how to decide what should be in a database to meet the application’s requirements. It tells how to structure the database so it gives good performance while minimizing the chance for error. Enhance Your Knowledge Advance Your Career Get more out of WROX.com Programmer to Programmer ™ Interact Take an active role online by participating in our P2P forums Wrox Online Library Hundreds of our books are available online through Books24x7.com Wrox Blox Download short informational pieces and code to keep you up to date and out of trouble! Chapters on Demand Purchase individual book chapters in pdf format Join the Community Sign up for our free monthly newsletter at newsletter.wrox.com Browse Ready for more Wrox? We have books and e-books available on .NET, SQL Server, Java, XML, Visual Basic, C#/ C++, and much more! Contact Us. We always like to get feedback from our readers. Have a book idea? Need community support? Let us know by e-mailing wrox-partnerwithus@wrox.com spine=1.872" www.it-ebooks.info Professional Microsoft ® SQL Server ® 2008 Programming Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxxv Chapter 1: Being Objective: Re-Examining Objects in SQL Server . . . . . . . . . . . . 1 Chapter 2: Tool Time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Chapter 3: Asking a Better Question: Advanced Queries . . . . . . . . . . . . . . . . . . . . 37 Chapter 4: XML Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 Chapter 5: Daring to Design . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Chapter 6: Core Storage and Index Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147 Chapter 7: More Advanced Index Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 Chapter 8: Views . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237 Chapter 9: Scripts and Batches . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 Chapter 10: Advanced Programmability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 Chapter 11: Transactions and Locks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335 Chapter 12: Triggers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 Chapter 13: SQL Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 Chapter 14: Reporting Services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435 Chapter 15: Buying in Bulk: The Bulk Copy Program (BCP) and Other BasicBulkOperations 473 Chapter 16: Getting Integrated . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493 Chapter 17: Replication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513 Chapter 18: Looking at Things in Full: Full-Text Search . . . . . . . . . . . . . . . . . . . . 555 Chapter 19: Feeling Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 579 Chapter 20: A Grand Performance: Designing a Database That Performs Well . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 617 Chapter 21: What Comes After: Forensic Performance Tuning . . . . . . . . . . . . . 637 Chapter 22: Administration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 655 Chapter 23: SMO: SQL Management Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 705 Chapter 24: Data Warehousing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 727 Chapter 25: Being Well Connected . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751 Appendix A: System Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 753 Appendix B: Going Meta: Getting Data About Your Data . . . . . . . . . . . . . . . . . . . 815 Appendix C: The Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 841 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 861 www.it-ebooks.info www.it-ebooks.info Professional Microsoft ® SQL Server ® 2008 Programming www.it-ebooks.info www.it-ebooks.info Professional Microsoft ® SQL Server ® 2008 Programming Rob Vieira Wiley Publishing, Inc. www.it-ebooks.info Professional Microsoft ® SQL Server ® 2008 Programming 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-25702-9 Manufactured in the United States of America 10987654321 Library of Congress Cataloging-in-Publication Data Vieira, Robert. Professional Microsoft SQL server 2008 programming / Rob Vieira. p. cm. Includes index. ISBN 978-0-470-25702-9 (paper/website) 1. SQL server. 2. Client/server computing. 3. Database management. I. Title. QA76.9.C55V535 2009 005.75’85–dc22 2009010578 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 http;//www.wiley.com/go/permissions . 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 Web site 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 Web site may provide or recommendations it may make. Further, readers should be aware that Internet Web sites 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. www.it-ebooks.info This book is dedicated with all my heart to Ashley, Addy,andAnna. It’s hard to believe that Ashley was still in elementary school when I wrote my first book (she’ll graduate from college later this year) and didn’t have to share this dedication with other kids (heck, she isn’t much of a kid anymore — eeek!). Then there’s Addy, who literally dreads me writing, yet continues to put up with me ‘‘disappearing’’ into my home office during the lengthy period of time I work on a book. Finally, there’s Anna, who came in the middle of this round of writing, and didn’t have any real history on what to expect from my writing disappearances. A dedication of this book would most certainly not be complete without a particularly special thank you to Deb, who has leveraged my efforts in this and my recent Beginning title to redefine for me what the terms ‘‘love’’ and ‘‘support’’ mean. As I’ve said for many releases now, I wish Wrox would let me print a picture of the women in my life on the cover of this book rather than my ugly mug — I’m certain that you, the reader, would appreciate it too! www.it-ebooks.info www.it-ebooks.info [...]... Chapter 2: Tool Time Books Online The SQL Server Configuration Manager Service Management Network Configuration The Protocols On to the Client The SQL Server Management Studio xxxv 1 1 2 2 6 6 7 7 8 8 10 10 10 11 11 11 11 12 17 17 17 17 18 19 20 20 21 21 22 24 26 www.it-ebooks.info Contents Getting Started Query Editor SQL Server Business Intelligence Development Studio SQL Server Integration Services (SSIS)... Objective: Re-Examining Objects in SQL Server So, What Exactly Do We Have Here? An Overview of Database Objects The Database Object The Transaction Log The Most Basic Database Object: Table Schemas Filegroups Diagrams Views Stored Procedures User-Defined Functions Users and Roles Rules Defaults User-Defined Data Types Full-Text Catalogs SQL Server Data Types NULL Data SQL Server Identifiers for Objects What... Errors Table-Valued Parameters (TVPs) Debugging Starting the Debugger Parts of the Debugger Using the Debugger Once It’s Started Understanding the SQLCLR and NET Programming in SQL Server Assemblies 101 Compiling an Assembly Uploading Your Assembly to SQL Server Creating Your Assembly-Based Stored Procedure Creating Scalar User-Defined Functions from Assemblies Creating Table-Valued Functions Creating... computer kits, and programming in BASIC as well as Z80 and 6502 assembly In 1983, he began studies for a degree in Computer Information Systems, but found the professional mainframe environment too rigid for his tastes and dropped out in 1985 to pursue other interests Later that year, he caught the ‘‘PC bug’’ and began the long road of programming in database languages from dBase to SQL Server Rob completed... 695 Trying Out PowerShell Navigating in PowerShell A Final Word on PowerShell 696 701 703 Policy-Based Management Summary 703 703 Chapter 23: SMO: SQL Management Objects The History of SQL Server Management Object Models SQL Distributed Management Objects SQL Namespaces Windows Management Instrumentation SMO The SMO Object Model Walking through Some Examples 705 706 706 706 707 707 708 709 Getting Started... wish he and his wife Rebecca great fortunes and good health in the year ahead Richard Waymire — Who has been such a pillar of the SQL Server community for oh so long Richard is one of the true ‘‘nice guys’’ who isn’t just nice — he just plain ‘‘gets it’’ when it comes to SQL Server Richard has provided the answer to many a question over the years This book has been in development for so long and touched... Number of Tries to Log In Storage of User and Password Information Security Options SQL Server Security Creating and Managing Logins Windows Authentication User Permissions Granting Access to a Specific Database Granting Object Permissions within the Database User Rights and Statement-Level Permissions Server and Database Roles Server Roles Database Roles Application Roles Creating Application Roles Adding... Things Up The High Price of Reusability 136 136 137 De-Normalization Partitioning for Scalability The SQL Server Diagramming Tools 137 138 139 Tables Dealing with Constraints Regarding Date Columns Summary 141 143 146 146 xvii www.it-ebooks.info Contents Chapter 6: Core Storage and Index Structure 147 SQL Server Storage 148 The Database The File The Extent The Page Rows Full-Text Catalogs File Streams... certified as a Certified Management Accountant (CMA); Microsoft Certified as a Solutions Developer (MCSD), Trainer (MCT), and Database Administrator (MCDBA); and even had a brief stint certified as an Emergency Medical Technician (EMT) Rob is currently the Database Team Lead for Stockamp, a Huron Consulting Group practice He has published six books on SQL Server development He resides in Vancouver, Washington,... Assembly Accessing Your Complex Data Type Dropping Data Types Summary Chapter 11: Transactions and Locks Transactions BEGIN TRAN COMMIT TRAN ROLLBACK TRAN SAVE TRAN How the SQL Server Log Works Using the CHECKPOINT Command At Normal Server Shutdown xx 268 269 273 276 278 278 281 283 283 284 287 298 301 301 302 306 307 308 308 311 312 314 317 320 325 330 331 331 332 333 335 335 337 337 338 338 343 343 . www.it-ebooks.info Professional Microsoft ® SQL Server ® 2008 Programming Professional Microsoft SQL Server 2008 Integration Services 978-0-470-24795-2 This book shows developers how to master the 2008 release. more. Beginning T -SQL with Microsoft SQL Server 2005 and 2008 978-0-470-25703-6 Beginning T -SQL with Microsoft SQL Server 2005 and 2008 provides a comprehensive introduction to the T -SQL programming. 861 www.it-ebooks.info www.it-ebooks.info Professional Microsoft ® SQL Server ® 2008 Programming www.it-ebooks.info www.it-ebooks.info Professional Microsoft ® SQL Server ® 2008 Programming Rob Vieira Wiley