CONTENTS Acknowledgments When I was asked to write this book, I jumped at the chance to work with the great bunch of folks at Apress again. I am particularly lucky to have the assistance once more of two hugely talented individuals, in the form of Jonathan Gennick and Evan Terry. As my editor, Jonathan has encouraged, taught, and mentored me through the authoring process, and has never wavered in his support even when the going got a bit tough (which, as in any publication schedule, at times it did!). Evan not only provided the benefit of his wealth of technical knowledge, but also his authoring expertise, and at times he simply provided a sensible voice of reason, all of which helped to improve the book significantly. I would also like to thank Mary Tobin, who managed to keep track of all the deadlines and project management issues, Damon Larson, for correcting my wayward use of the English language, and all the other individuals who helped get this book into the form that you are now holding in your hands. Thank you all. My family have once again had to endure me spending long hours typing away at the keyboard, and I thank them for their tolerance, patience, and support. I couldn’t do anything without them. And thankyou to you, the reader, for purchasing this book. I hope that you find the content interesting, useful, and above all, enjoyable to read. xviii xix Preface I’ve worked with Microsoft SQL Server for nearly ten years now, and I’ve used SQL Server 2008 since the very first preview version was made available to the public. One thing I have noticed is that, with every new release, SQL Server grows ever more powerful, and ever more complex. There is now a huge array of features that go way beyond the core functionality expected from a database system and, with so many different facets to cover, it is becoming ever harder to be a SQL Server "expert". SQL Server developers are no longer simply expected to be proficent in writing T-SQL code, but also in XML and SQLCLR (and knowing when to use each). You no longer execute a query to get a single result set from an isolated database, but handle multiple active result sets derived from queries across distributed servers. The types of information stored in modern databases represent not just character, numeric, and binary data, but complex data such as spatial, hierarchical, and filestream data. Attempting to comprehensively cover any one of these topics alone would easily generate enough material to fill an entire book, so I'm not even going to try doing so. Instead, I’m going to concentrate on what I believe you need to know to create high-quality database applications, based on my own practical experience. I’m not going to waste pages discussing the ins and outs of some obscure or little-used feature, unless I can show you a genuine use case for it. Nor will I insult your intelligence by laboriously explaining the basics – I'll assume that you're already familiar with the straightforward examples covered in Books Online, and now want to take your knowledge further. All of the examples used in this book are based on real-life scenarios that I've encountered, and they show you how to deal with problems that you're likely to face in most typical SQL Server environments. I promise not to show you seemingly perfect solutions, which you then discover only work in the artificially-cleansed "AdventureWorks" world; as developers we work with imperfect data, and I'll try to show you examples that deal with the warts and all. The code examples were tested using the SQL Server 2008 Developer Edition with Service Pack 1 installed, but should work on all editions of SQL Server 2008 unless explicitly stated otherwise. Finally, I hope that you enjoy reading this book and thinking about the issues discussed. The reason why I enjoy database development is that it presents a never-ending set of puzzles to solve – and even when you think you have found the optimum answer to a problem, there is always the possibility of finding an even better solution in the future. While you shouldn't let this search for perfection detract you from the job at hand (sometimes, "good enough" is all you need), there are always new techniques to learn, and alternative methods to explore. I hope that you might learn some of them in the pages that follow. . CONTENTS Acknowledgments When I was asked to write this book, I jumped at the chance. the content interesting, useful, and above all, enjoyable to read. xviii xix Preface I’ve worked with Microsoft SQL Server for nearly ten years now, and