
Transact-SQL Cookbook By Jonathan Gennick, Ales Spetic Publisher: O'Reilly Pub Date: March 2002 ISBN: 1-56592-756-7 Pages: 302 Table of • Slots: 1 Contents • Index • Reviews • Examples This cookbook contains a wealth of solutions to problems that SQL Reader programmers face all the time. Recipes inside range from how to perform • Reviews simple tasks, like importing external data, to ways of handling issues that are • Errata more complicated, like set algebra. Each recipe includes a discussion that explains the logic and concepts underlying the solution. The book covers audit logging, hierarchies, importing data, sets, statistics, temporal data, and data structures. Transact-SQL Cookbook By Jonathan Gennick, Ales Spetic Publisher: O'Reilly Pub Date: March 2002 ISBN: 1-56592-756-7 Pages: 302 Table of • Slots: 1 Contents • Index • Reviews • Examples Reader • Reviews • Errata Copyright Dedication Preface Why We Wrote This Book Audience for This Book Which Platform and Version? Structure of This Book Conventions Used in This Book About the Code Comments and Questions Acknowledgments Chapter 1. Pivot Tables Section 1.1. Using a Pivot Table Chapter 2. Sets Section 2.1. Introduction Section 2.2. The Students Example Section 2.3. Implementing Set Difference Section 2.4. Comparing Two Sets for Equality Section 2.5. Implementing Partial Intersection Section 2.6. Implementing Full Intersection Section 2.7. Classifying Subsets Section 2.8. Summarizing Classes of Sets Section 2.9. Aggregating Aggregates Section 2.10. Summarizing Aggregated Classes Section 2.11. Including Nonaggregated Columns Section 2.12. Finding the Top N Values in a Set Section 2.13. Reporting the Size of a Set's Complement Section 2.14. Finding the Complement of a Set Section 2.15. Finding the Complement of a Missing Set Section 2.16. Finding Complements of Sets with Different Universes Section 2.17. Comparing a Set with Its Universe Section 2.18. Dynamic Classification System Chapter 3. Data Structures Section 3.1. Types of Data Structures Section 3.2. Working Example Section 3.3. Finding Regions Section 3.4. Reporting Region Boundaries Section 3.5. Limiting Region Size Section 3.6. Ranking Regions by Size Section 3.7. Working with Sequences Section 3.8. Working with Runs Section 3.9. Cumulative Aggregates in Lists Section 3.10. Implementing a Stack Section 3.11. Implementing Queues Section 3.12. Implementing Priority Queues Section 3.13. Comparing Two Rows in an Array Section 3.14. Printing Matrices and Arrays Section 3.15. Transposing a Matrix Section 3.16. Calculating a Matrix Trace Section 3.17. Comparing Two Matrices for Size Section 3.18. Adding and Subtracting Matrices Section 3.19. Multiplying Matrices Chapter 4. Hierarchies in SQL Section 4.1. Types of Hierarchies Section 4.2. Creating a Permission Hierarchy Section 4.3. Changing Individual Permissions Section 4.4. Adding New Individual Permissions Section 4.5. Centralizing Authorization Logic Section 4.6. Implementing General Hierarchies Section 4.7. Traversing Hierarchies Recursively Section 4.8. Manipulating Hierarchies Recursively Section 4.9. Aggregating Hierarchies Section 4.10. Preparing Multilevel Operations Section 4.11. Aggregating Hierarchies Revised Chapter 5. Temporal Data Section 5.1. Introduction Section 5.2. The Schedules Example Section 5.3. Enforcing Granularity Rules Section 5.4. Storing Out-of-Range Temporal Values Section 5.5. Deriving the First and Last Dates of the Month Section 5.6. Printing Calendars Section 5.7. Calculating Durations Section 5.8. Reporting Durations Section 5.9. Querying Periods Section 5.10. Querying Periods and Respecting Granularity Section 5.11. Finding Available Periods Section 5.12. Finding Common Available Periods Section 5.13. Excluding Recurrent Events Section 5.14. Excluding Nonrecurring Events Section 5.15. Finding Continuous Periods Section 5.16. Using Calendar Information with Periods Section 5.17. Using Calendar Information with Durations Chapter 6. Audit Logging Section 6.1. Audit Logs Section 6.2. The Warehouse Example Section 6.3. Row-Level Logging Section 6.4. Reporting Log Events Section 6.5. Generating Current Snapshots Section 6.6. Generating Time-Restricted Snapshots Section 6.7. Undoing Table Changes Section 6.8. Minimizing Audit-Log Space Consumption Section 6.9. Online Account Balancing Section 6.10. Activity-Level Logging Section 6.11. Partitioning Large Log Tables Section 6.12. Server Push Chapter 7. Importing and Transforming Data Section 7.1. Considerations When Importing Data Section 7.2. Working Examples Section 7.3. Importing External Data Section 7.4. Importing Data into a Live System Section 7.5. Importing with a Single Procedure Section 7.6. Hiding the Import Procedure Section 7.7. Folding Tables Section 7.8. Pivoting Tables Section 7.9. Joining Arrays with Tables Section 7.10. Joining Arrays with Master Tables Section 7.11. Joining Arrays with Multiple Master Records Section 7.12. Extracting Master Records from Tables Section 7.13. Generating Master Records Online Section 7.14. Working with Duplicates Chapter 8. Statistics in SQL Section 8.1. Statistical Concepts Section 8.2. The Light-Bulb Factory Example Section 8.3. Calculating a Mean Section 8.4. Calculating a Mode Section 8.5. Calculating a Median Section 8.6. Calculating Standard Deviation, Variance, and Standard Error Section 8.7. Building Confidence Intervals Section 8.8. Calculating Correlation Section 8.9. Exploring Patterns with Autocorrelation Section 8.10. Using a Simple Moving Average Section 8.11. Extending Moving Averages Appendix A. The T-Distribution Table Colophon Index Copyright © 2002 O'Reilly & Associates, Inc. All rights reserved. Printed in the United States of America. Published by O'Reilly & Associates, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O'Reilly & Associates books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles ( http://safari.oreilly.com). For more information contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Nutshell Handbook, the Nutshell Handbook logo, and the O'Reilly logo are a registered trademark of O'Reilly & Associates, Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and O'Reilly & Associates, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. The association between the image of a tree swift and the topic of Transact-SQL is a trademark of O'Reilly & Associates, Inc. While every precaution has been taken in the preparation of this book, the publisher and the authors assume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. Dedication To my family: Helena, Peter, Mojca, and Bostjan -Ales To Donna, Jenny, and Jeff -Jonathan Preface SQL in general, and Transact-SQL in particular, is so deceptively simple that few people take the time to master it. Many of the SQL books currently on the market contribute to this state of affairs by not taking their readers very far beyond the simple SELECT column_name FROM table_name WHERE conditions pattern of writing SQL SELECT statements. In this book, we hope to take you far beyond the level of writing simple SQL statements and open your eyes to the full power of SQL and Transact-SQL. For many years, SQL has been the domain of large corporations using high-end databases such as Oracle, Sybase, Informix, and DB2. Microsoft's entry into the market with Microsoft SQL Server brought Transact-SQL to the workgroup server and desktop level, thereby broadening the market for Transact-SQL programmers. More systems are being programmed in SQL and Transact-SQL, and if you're a Transact- SQL programmer, you'll want to leverage Transact-SQL to its fullest. Getting the most out of SQL and Transact-SQL requires a particular way of thinking. SQL is a set-oriented language in which you focus on end results, not on the procedures used to accomplish those results. If you're used to thinking procedurally, this shift to set-oriented thinking can be jarring. The SQL solutions to problems that are easily described procedurally are often not obvious. So why bother with the set-oriented SQL solutions? It's true that Transact-SQL provides you with procedural capabilities, but use of those capabilities is no excuse for not utilizing the core SQL language to its fullest. SQL solutions are often magnitudes more efficient than the corresponding procedural solutions. Why We Wrote This Book We have many years experience working in SQL environments, and over the years we've developed, or stumbled across, a number of useful SQL design patterns that we apply to problems that we encounter in our day-to-day work. Knowing these patterns gives us an edge and allows us to accomplish things with ease that might otherwise be difficult. Looking back over our careers, we remember the "magic" of discovering a new and innovative SQL-based solution to a programming problem. There was the sudden flash of light and a rush of adrenalin as a whole new vista of possibilities opened up before us. It's our hope that we can share some of that magic with you. For that matter, we each learned a few pieces of magic from the other while collaborating on this book. Audience for This Book We have two target audiences in mind for this book: those who have just recently learned SQL and those who have been using SQL for several years. This is not a learning-SQL type of book. We assume that you have a working knowledge of the basic SQL statements: INSERT, UPDATE, DELETE, and SELECT. We also assume that you are passingly familiar with Transact-SQL's procedural capabilities.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages453 Page
-
File Size-