A05T619239.fm Page vii Tuesday, May 31, 2005 9:13 PM

Table of Contents

Acknowledgements ...... xix Introduction ...... xxiii Who This Book Is For ...... xxiv How This Books is Organized ...... xxv System Requirements...... xxvi Using the Samples ...... xxvii Support for This Book ...... xxix Questions and Comments...... xxix

Part I Design Fundamentals and Core Technologies 1 Overview ...... 3 A Tough Act to Follow ...... 3 The Software Industry and Disruptive Change ...... 3 Industry Trends, SQL Server Features, and a Book to Show You the Way ...... 4 Programming the Server...... 4 Application Code and SQL Server: Extending Your ’s Reach ...... 5 It’s the Strategy, Stupid ...... 7 A Collaborative Effort for, and by, Developers...... 9 2 Exploring the T-SQL Enhancements in SQL Server 2005 ...... 11 Introducing SQL Server Management Studio ...... 12 Common Table Expressions...... 13 Recursive Queries with CTEs ...... 16 The PIVOT and UNPIVOT Operators ...... 20 Using UNPIVOT ...... 21 Dynamically Pivoting Columns...... 22 The APPLY Operator...... 24 TOP Enhancements ...... 25 Ranking Functions ...... 26 ROW_NUMBER()...... 26 RANK() ...... 30 DENSE_RANK() and NTILE(n) ...... 32 Using All the Ranking Functions Together ...... 34 Ranking over Groups: PARTITION BY ...... 35

Microsoft is interested in hearing your feedback about this publication so we can What do you think of this book? continually improve our books and learning resources for you. To participate in a brief We want to hear from you! online survey, please visit: www..com/learning/booksurvey/

vii A05T619239.fm Page viii Tuesday, May 31, 2005 9:13 PM

viii Table of Contents

Exception Handling in Transactions ...... 38 New Data Types ...... 40 varchar(max) Data Type ...... 40 xml Data Type...... 40 The WAITFOR Command ...... 41 DDL Triggers and Notifications ...... 42 SNAPSHOT Isolation ...... 43 Statement-Level Recompile ...... 44 Summary ...... 44 3 An Overview of SQL CLR...... 45 Getting Started: Enabling CLR Integration...... 46 Visual Studio/SQL Server Integration ...... 48 SQL Server Projects in Visual Studio ...... 49 Automated Deployment...... 51 SQL CLR Code Attributes ...... 52 Your First SQL CLR ...... 53 CLR Stored Procedures and Server-Side Data Access...... 55 Piping Data with SqlDataRecord and SqlMetaData ...... 57 Deployment ...... 59 Deploying Your Assembly...... 59 Deploying Your Stored Procedures ...... 61 Testing Your Stored Procedures...... 63 CLR Functions ...... 65 CLR Triggers ...... 69 CLR Aggregates ...... 74 CLR Types ...... 78 Security ...... 84 Examining and Managing CLR Types in a Database ...... 85 Best Practices for SQL CLR Usage ...... 91 Summary ...... 92 4 XML and the Relational Database ...... 93 XML in SQL Server 2000 ...... 95 The XML Data Type ...... 96 Working with the XML Data Type as a Variable ...... 96 Working with XML in Tables ...... 98 XML Schemas ...... 99 XML Indexes ...... 102 FOR XML Commands ...... 105 FOR XML RAW ...... 106 FOR XML AUTO ...... 106 FOR XML EXPLICIT...... 108 A05T619239.fm Page ix Tuesday, May 31, 2005 9:13 PM

Table of Contents ix

OPENXML Enhancements in SQL Server 2005 ...... 120 XML Bulk Load ...... 121 Querying XML Data Using XQuery ...... 122 XQuery Defined ...... 122 SQL Server 2005 XQuery in Action ...... 125 XML DML...... 134 Converting a Column to XML...... 135 Summary ...... 137 5 Introducing SQL Server Management Studio ...... 139 The New Management Studio Interface...... 140 Overview of New Features ...... 140 Window Types...... 141 Positioning a Docking Window ...... 142 Window Customization Options ...... 143 Connecting to a Database Server ...... 144 Using Object Explorer ...... 145 Object Explorer Filters ...... 146 Management Studio Solutions, Projects, and Files ...... 149 Code and Text Editor ...... 150 Track Changes Indicator ...... 152 Bookmarks...... 153 Creating Objects ...... 154 Creating Tables ...... 154 Creating Table-Related Objects ...... 156 Creating Indexes...... 156 Setting Properties for New Users...... 157 Generating Scripts from Objects ...... 158 Creating Queries...... 158 Executing Queries...... 160 Using Templates ...... 161 Maintenance Features ...... 163 Using a Maintenance Plan ...... 164 Performance Tools ...... 166 SQL Server Profiler ...... 166 Database Engine Tuning Advisor ...... 167 Summary ...... 167 6 Using SQL Server Management Objects (SMO) ...... 169 What Is SMO? ...... 170 What About SQL-DMO? ...... 170 New Features in SMO ...... 174 Working with SMO in Visual Studio...... 174 A05T619239.fm Page x Tuesday, May 31, 2005 9:13 PM

x Table of Contents

Iterating Through Available Servers ...... 177 Retrieving Server Settings ...... 178 Creating Backup-and-Restore Applications...... 182 Performing Programmatic DBCC Commands with SMO ...... 188 Summary ...... 190 7 SQL Server 2005 Security ...... 191 Four Themes of the Security Framework ...... 192 Secure by Design ...... 192 Secure by Default...... 192 Secure by Deployment ...... 192 Communications ...... 192 SQL Server 2005 Security Overview ...... 193 SQL Server Logins ...... 194 Database Users...... 194 The Guest User Account ...... 195 Authentication and Authorization ...... 196 How Clients Establish a Connection ...... 196 Password Policies ...... 197 User-Schema Separation...... 199 Execution Context ...... 201 Encryption Support in SQL Server 2005...... 204 Encrypting Data on the Move ...... 205 Encrypting Data at Rest ...... 206 Protecting SQL Server 2005 ...... 211 Reducing the Surface Area for Attack ...... 211 How Hackers Attack SQL Server ...... 213 Direct Connection to the Internet...... 213 Weak SA Passwords ...... 213 SQL Server Browser Service ...... 213 SQL Injection ...... 214 Intelligent Observation ...... 214 Summary ...... 215

Part II Applications Development and Reach Technologies 8 ADO.NET 2.0, Typed DataSet Objects, and .NET Data Binding...... 219 A Brief History of Data Access Object Models ...... 220 DAO: A Golden Oldie ...... 220 RDO: A Thin API Wrapper ...... 220 Enter OLE DB and ADO “Classic”...... 220 ADO + .NET = ADO.NET...... 221 What’s New in ADO.NET 2.0?...... 221 A05T619239.fm Page xi Tuesday, May 31, 2005 9:13 PM

Table of Contents xi

New Typed DataSet Members ...... 222 Other Enhancements ...... 222 Typed DataSet Enhancements ...... 223 DataTable Objects in the Typed DataSet Designer ...... 225 TableAdapter Objects...... 226 Connection String Management ...... 226 Using the TableAdapter Configuration Wizard ...... 228 More on Queries and Parameters ...... 230 Adding Query Objects...... 231 DBDirect Methods and Connected Use of Typed DataSet Objects ...... 232 Standalone DataTable Objects ...... 233 “Pure” ADO.NET: Working in Code ...... 233 Querying 101 ...... 234 Keeping Data Up to Date ...... 235 Responsive UIs: Executing Queries Asynchronously ...... 237 Not for Servers Only: Client-Side Bulk Copy ...... 242 It’s Not Just Text: Processing XML Columns with ADO.NET and the System.Xml Namespace ...... 244 Nothing but .NET: Consuming SQL CLR UDT Data as Native .NET Objects ...... 245 Back to the Drawing Board...... 246 Embedding SQL CLR Objects in Typed DataSet Objects ...... 247 Adding a CLR Stored Procedure to a Typed DataSet ...... 247 TVFs: Easy Living...... 249 Aggregates and UDTs ...... 250 Data Binding ...... 251 DataGridView Binding ...... 252 Details View Binding ...... 253 Smart Defaults ...... 253 Binding to Stored Procedures and Views ...... 254 SQL CLR Binding...... 254 Master-Detail and Lookup Binding ...... 254 Parameterized Query Data Binding...... 257 Data Binding on the Web ...... 258 Typed DataSet Objects and the Web.config File...... 258 The Data Source Configuration Wizard, the ObjectDataSource Control, and the New Data-Bound Controls ...... 259 Summary ...... 260 9 Debugging ...... 261 About the Sample Code ...... 262 Ad Hoc Debugging ...... 264 Creating Database Connections...... 264 A05T619239.fm Page xii Tuesday, May 31, 2005 9:13 PM

xii Table of Contents

T-SQL “Step Into” Debugging ...... 267 Application Debugging...... 276 Entering Debugging Mode ...... 279 Debugging SQL CLR Code ...... 280 Breakpoints and Context Switching ...... 283 Mixing SQL CLR and T-SQL Code ...... 284 Test Script Debugging...... 289 Debugging Queries External to Visual Studio ...... 289 Remote Debugging ...... 292 Server Configuration...... 292 Server Firewall Considerations...... 294 Back to the Client ...... 295 Client Firewall Configuration ...... 295 Attaching to a Remote Process ...... 297 Summary ...... 299 10 SQL Server 2005 Native XML Web Services...... 301 Understanding Native XML Web Services ...... 302 Comparing Native XML Web Services and SQLXML ...... 302 Exposing SQL Programmability as Web Services ...... 303 Stored Procedures and User-Defined Functions ...... 303 SQL Batch ...... 303 Reserving URLs with Http.sys...... 303 Creating and Managing Endpoints...... 304 Granting Endpoint Permissions ...... 309 Calling Native XML Web Service Endpoints from Client Applications . . . . . 310 Example Native XML Web Services Project ...... 312 Creating the SQL Server Functionality ...... 312 Registering the URL with Http.sys...... 316 Exposing the Endpoints ...... 316 Granting Security Access to the Endpoints ...... 317 Creating the Client Application...... 319 Best Practices for Using Native XML Web Services ...... 324 Advantages of Native XML Web Services ...... 325 Limitations of Native XML Web Services ...... 325 Security Recommendations ...... 326 Performance Recommendations ...... 326 When to Avoid Native XML Web Services...... 326 When to Use Native XML Web Services...... 327 Summary ...... 328 A05T619239.fm Page xiii Tuesday, May 31, 2005 9:13 PM

Table of Contents xiii

11 Transactions ...... 329 What Is a Transaction? ...... 330 Understanding the ACID Properties ...... 330 Local Transactions Support in SQL Server 2005...... 332 Autocommit Transaction Mode ...... 333 Explicit Transaction Mode ...... 333 Implicit Transaction Mode ...... 336 Batch-Scoped Transaction Mode ...... 337 Using Local Transactions in ADO.NET ...... 338 Transaction Terminology ...... 340 Isolation Levels ...... 341 Isolation Levels in SQL Server 2005...... 341 Isolation Levels in ADO.NET ...... 346 Distributed Transactions ...... 347 Distributed Transaction Terminology ...... 348 Rules and Methods of Enlistment ...... 349 Distributed Transactions in SQL Server 2005 ...... 351 Distributed Transactions in the .NET Framework ...... 352 Using a Resource Manager in a Successful Transaction ...... 360 Transactions in SQL CLR (CLR Integration) ...... 364 Putting It All Together ...... 368 Summary ...... 369 12 SQL Server Service Broker: The New Middleware...... 371 What Is Middleware? ...... 371 What Is SQL Server Service Broker?...... 372 Comparing Service Broker and MSMQ...... 372 What Is a SQL Server Service Broker Application? ...... 373 Service Broker Architecture...... 374 Integrated Management and Operation ...... 377 Routing and Load Balancing...... 377 Service Broker Programming in T-SQL ...... 379 A Word About Programming Languages...... 380 Enabling Service Broker...... 380 Defining Service Broker Objects...... 380 The Sending Service Program ...... 382 The Receiving Service Program ...... 383 Running the Application ...... 385 A More Robust, Real-World Application...... 386 Service Broker and Query Notification ...... 387 Service Broker’s Place in the Middleware World ...... 391 Summary ...... 392 A05T619239.fm Page xiv Tuesday, May 31, 2005 9:13 PM

xiv Table of Contents

13 Using SQL Server 2005 Notification Services ...... 393 What Is a Notification Application? ...... 393 Notification Services Components ...... 394 Notification Services Deployment Strategies ...... 396 Working with Notification Services...... 397 Creating Notification Applications ...... 398 A Sample Notification Application ...... 400 Flight Price Notification Sample Application ...... 400 Summary ...... 424 14 Developing Desktop Applications with SQL Server Express Edition ...... 427 What Is SQL Server Express Edition?...... 427 Licensing ...... 429 Feature Review ...... 429 SQL Server 2005 Express Edition with Advanced Services ...... 431 Configuration ...... 435 Working with SQL Server Express Edition ...... 437 SQLCMD Command-Line Tool...... 442 User Instances...... 446 SSEUTIL ...... 449 Installing SQL Server Express Edition ...... 451 Using the Setup Wizard to Manually Install Express Edition...... 452 Installing via Command-Line Parameters or a Configuration File ...... 456 Deploying Express Edition Applications Using a Wrapper ...... 460 Deploying Express Edition Applications Using ClickOnce...... 471 Updating ClickOnce Deployments That Use Express Edition ...... 475 Summary ...... 485 15 Developing Applications with SQL Server 2005 Everywhere Edition and SQL Server Merge Replication ...... 487 SQL Everywhere Integration with SQL Server 2005 ...... 488 Working with SQL Everywhere in Management Studio ...... 489 Working with SQL Everywhere Data in Management Studio...... 493 Creating a SQL Everywhere Application with SQL Server Replication and Visual Studio 2005 ...... 496 Creating a Publication...... 498 Installing and Configuring SQL Everywhere Server Components for IIS ...... 505 Creating a Subscription Using Management Studio ...... 508 Creating a Mobile Application Using Visual Studio 2005 ...... 513 Summary ...... 520 A05T619239.fm Page xv Tuesday, May 31, 2005 9:13 PM

Table of Contents xv

Part III Reporting and Business Intelligence 16 Using SQL Server 2005 Integration Services ...... 523 History of Data Transfer in SQL Server ...... 523 DTS Packages ...... 524 Working with Integration Services Packages ...... 524 Control Flow ...... 524 Data Flow...... 526 Using Integration Services Packages ...... 532 Creating Packages Using the Import And Export Wizard ...... 532 Creating Packages Using BI Development Studio...... 533 Managing Packages Using Management Studio ...... 533 Using the Command Line to Execute and Manage Packages ...... 533 Scheduling Packages Using SQL Server Agent ...... 534 Configuring and Deploying Packages...... 535 Overview of Programming Package Extension ...... 536 Security ...... 537 Dealing with Sensitive Information and Assets ...... 537 Considerations for Working on a Single Development Machine ...... 538 Considerations for Workgroups ...... 538 Programming Integration Services ...... 539 Programming in Visual Studio ...... 539 Loading and Executing Packages in Applications ...... 539 Creating Packages Programmatically ...... 540 Extensibility ...... 554 Script Tasks ...... 554 Custom Tasks...... 555 Custom Components ...... 556 Script Components...... 566 Custom Connection Managers...... 571 Log Providers ...... 572 Foreach Enumerator ...... 572 Summary ...... 572 17 Basic OLAP ...... 573 Wherefore BI? ...... 573 OLAP 101...... 575 OLAP Vocabulary ...... 576 Dimensions, Axes, Stars, and Snowflakes ...... 576 Building Your First Cube ...... 578 Preparing Star Schema Objects ...... 579 A Tool by Any Other Name...... 579 A05T619239.fm Page xvi Tuesday, May 31, 2005 9:13 PM

xvi Table of Contents

Creating the Project ...... 581 Adding a Data Source View ...... 582 Creating a Cube with the Cube Wizard ...... 586 Using the Cube Designer ...... 589 Using the Dimension Wizard ...... 591 Using the Dimension Designer ...... 594 Working with the Properties Window and Solution Explorer...... 596 Processing the Cube ...... 597 Running Queries ...... 597 Summary ...... 599 18 Advanced OLAP ...... 601 What We’ll Cover in This Chapter ...... 602 MDX in Context ...... 602 And Now a Word from Our Sponsor…...... 603 Advanced Dimensions and Measures...... 603 Keys and Names...... 604 Changing the All Member ...... 606 Adding a Named Query to a Data Source View...... 607 Parent-Child Dimensions ...... 609 Member Grouping...... 614 Server Time Dimensions ...... 615 Fact Dimensions...... 617 Role-Playing Dimensions ...... 620 Advanced Measures ...... 621 Calculations...... 622 Calculated Members ...... 623 Named Sets...... 629 More on Script View ...... 629 Key Performance Indicators ...... 633 KPI Visualization: Status and Trend ...... 634 A Concrete KPI ...... 635 Testing KPIs in Browser View ...... 637 KPI Queries in Management Studio ...... 639 Other BI Tricks in Management Studio ...... 643 Actions...... 645 Actions Simply Defined...... 645 Designing Actions ...... 645 Testing Actions ...... 647 Partitions, Aggregation Design, Storage Settings, and Proactive Caching ...... 648 Editing and Creating Partitions ...... 649 Aggregation Design ...... 651 Partition Storage Options...... 652 A05T619239.fm Page xvii Tuesday, May 31, 2005 9:13 PM

Table of Contents xvii

Proactive Caching...... 652 Additional Features and Tips ...... 654 Perspectives ...... 654 Translations ...... 657 Roles ...... 662 Summary ...... 665 19 OLAP Application Development ...... 667 Using Excel...... 668 Working Within Excel ...... 669 Using PivotTables and Charts in Applications and Web Pages ...... 676 Beyond OWC: Full-On OLAP Development ...... 691 MDX and Analysis Services APIs...... 691 Moving to MDX ...... 692 Management Studio as an MDX Client...... 692 OLAP Development with ADO MD.NET ...... 706 XMLA at Your (Analysis) Service...... 718 Analysis Services CLR Support: Server-Side ADO MD.NET...... 729 Summary ...... 739 20 Extending Your Database System with Data Mining ...... 741 Why Mine Your Data? ...... 742 Getting Started ...... 745 Preparing Your Source Data ...... 745 Creating Training and Test Samples...... 747 Adding an SSAS Project...... 752 Using the Data Mining Wizard and Data Mining Designer ...... 752 Creating a Mining Structure ...... 755 Creating a Mining Model ...... 755 Editing and Adding Mining Models ...... 758 Deploying and Processing Data Mining Objects...... 763 Viewing Mining Models ...... 765 Validating and Comparing Mining Models ...... 773 Nested Tables ...... 776 Using Data Mining Extensions ...... 782 Data Mining Modeling Using DMX...... 782 Data Mining Predictions Using DMX ...... 792 DMX Templates...... 799 Data Mining Applied ...... 800 Data Mining and API Programming ...... 801 Using the WinForms Model Content Browser Controls ...... 801 Executing Prediction Queries with ADO MD.NET ...... 804 A05T619239.fm Page xviii Tuesday, May 31, 2005 9:13 PM

xviii Table of Contents

Model Content Queries ...... 804 ADO MD.NET and ASP.NET ...... 805 Using the Data Mining Web Controls ...... 805 Developing Managed Stored Procedures ...... 806 XMLA and Data Mining ...... 808 Data Mining and Reporting Services ...... 810 Summary ...... 817 21 Reporting Services...... 819 Report Definition and Design ...... 820 Data Sources...... 820 Report Layouts ...... 822 Report Designer...... 826 Report Builder ...... 838 Report Definition Language...... 843 Report Management ...... 843 Publishing ...... 843 Report Manager...... 844 SQL Server Management Studio ...... 851 Command-Line Utilities ...... 852 Programming: Management Web Services...... 852 Report Access and Delivery ...... 852 Delivery on Demand ...... 852 Subscriptions ...... 855 Presentation Formats ...... 857 Programming: Rendering ...... 859 Report Server Architecture ...... 862 Deployment Modes...... 865 Extensibility...... 865 Report Integration ...... 867 Summary ...... 867

Index...... 869

Microsoft is interested in hearing your feedback about this publication so we can What do you think of this book? continually improve our books and learning resources for you. To participate in a brief We want to hear from you! online survey, please visit: www.microsoft.com/learning/booksurvey/