SQL Server 2012 T-SQL Recipes......Page 745 Contents......Page 747 About the Authors......Page 791 About the Technical Reviewers......Page 793 Acknowledgments......Page 794 Downloading the Code......Page 5 Solution......Page 6 Solution......Page 7 How It Works......Page 8 How It Works......Page 9 How It Works......Page 11 Solution......Page 12 How It Works......Page 13 Solution......Page 14 Problem......Page 15 Solution......Page 16 Solution......Page 17 Solution......Page 18 How It Works......Page 19 How It Works......Page 20 Solution......Page 21 Solution......Page 22 Solution......Page 23 Solution......Page 24 How It Works......Page 25 Solution......Page 27 Solution......Page 28 How It Works......Page 29 Solution......Page 30 How It Works......Page 31 Solution......Page 32 Solution......Page 33 Solution......Page 34 How It Works......Page 35 Problem......Page 36 Solution #2: Exit and Provide a Value......Page 37 Solution......Page 38 How It Works......Page 39 How It Works......Page 40 Solution......Page 41 How It Works......Page 42 Problem......Page 44 How It Works......Page 45 Solution......Page 46 Solution......Page 47 Solution......Page 50 How It Works......Page 51 How It Works......Page 53 Solution......Page 54 Solution......Page 56 Problem......Page 57 How It Works......Page 58 Problem......Page 59 How It Works......Page 60 How It Works......Page 62 Solution......Page 63 Solution......Page 64 How It Works......Page 65 Solution......Page 66 How It Works......Page 67 Problem......Page 68 Solution......Page 69 How It Works......Page 70 How It Works......Page 71 Solution......Page 72 Solution......Page 73 How It Works......Page 74 Solution......Page 76 Solution......Page 77 How It Works......Page 79 How It Works......Page 81 How It Works......Page 82 Solution......Page 83 Solution......Page 84 Solution......Page 85 Solution......Page 86 Problem......Page 87 Solution......Page 88 How It Works......Page 89 How It Works......Page 90 Solution......Page 91 How It Works......Page 92 Solution #1......Page 95 How It Works......Page 96 Solution......Page 97 Solution......Page 98 How It Works......Page 99 Solution......Page 100 Solution......Page 101 Solution......Page 102 How It Works......Page 103 How It Works......Page 105 How It Works......Page 106 Problem......Page 108 How It Works......Page 109 How It Works......Page 110 Problem......Page 112 Solution......Page 113 How It Works......Page 114 Aggregate Functions......Page 116 Solution......Page 117 Solution......Page 118 Summary......Page 119 Solution......Page 120 How It Works......Page 121 Solution......Page 122 Windowing Functions......Page 123 Windowed Aggregate Functions......Page 125 Solution......Page 126 How It Works......Page 127 Solution......Page 128 Solution......Page 129 Problem......Page 131 How It Works......Page 132 Solution......Page 133 How It Works......Page 134 Problem......Page 135 Solution......Page 136 How It Works......Page 137 Analytic Functions......Page 139 Solution......Page 140 How It Works......Page 141 How It Works......Page 142 Solution......Page 143 Solution......Page 144 How It Works......Page 145 Solution......Page 146 How It Works......Page 147 Problem......Page 148 How It Works......Page 149 Solution......Page 150 Problem......Page 151 How It Works......Page 152 Solution......Page 153 Problem......Page 154 Solution......Page 155 Solution......Page 156 Solution......Page 158 Solution......Page 159 How It Works......Page 160 Solution......Page 161 Solution......Page 162 Problem......Page 163 How It Works......Page 164 Solution......Page 165 How It Works......Page 166 Solution......Page 169 How It Works......Page 170 How It Works......Page 171 How It Works......Page 172 Solution......Page 173 How It Works......Page 177 Chapter 9: Working with Strings......Page 180 Solution......Page 181 Solution......Page 182 Solution......Page 183 Solution......Page 184 Solution......Page 185 How it Works......Page 186 Solution......Page 187 Problem......Page 188 Solution......Page 189 How it Works......Page 190 How it Works......Page 191 Solution......Page 192 Problem......Page 193 Solution......Page 194 Solution......Page 195 How it Works......Page 196 Solution......Page 197 Solution......Page 198 How It Works......Page 199 How It Works......Page 200 How It Works......Page 201 Solution......Page 202 Solution......Page 203 How It Works......Page 204 Solution......Page 205 Solution......Page 206 Problem......Page 207 Solution #1......Page 208 Solution #3......Page 209 How It Works #3......Page 210 Solution......Page 211 How It Works......Page 212 Solution......Page 213 Solution......Page 215 Solution......Page 216 How It Works......Page 217 Solution......Page 218 How It Works......Page 219 How It Works......Page 220 Solution #2......Page 221 Solution......Page 222 How It Works......Page 223 Problem......Page 224 How It Works......Page 225 How It Works......Page 227 Solution......Page 229 How It Works......Page 230 How It Works......Page 231 Solution......Page 232 How It Works......Page 233 How It Works......Page 235 Solution......Page 237 How It Works......Page 238 Transaction Control......Page 240 Solution......Page 242 How It Works......Page 243 Problem......Page 244 How It Works......Page 245 Solution......Page 246 Locking......Page 248 Problem......Page 250 How It Works......Page 251 Solution......Page 252 Transaction, Locking, and Concurrency......Page 254 Solution......Page 256 How It Works......Page 259 Blocking......Page 260 Solution......Page 261 Solution......Page 263 Deadlocking......Page 264 Solution......Page 265 How It Works......Page 268 Solution......Page 269 How It Works......Page 270 Solution......Page 271 Problem......Page 272 Problem......Page 273 Problem......Page 274 Solution......Page 275 How It Works......Page 276 Solution......Page 278 How It Works......Page 279 How It Works......Page 281 How It Works......Page 282 How It Works......Page 285 How It Works......Page 286 How It Works......Page 287 Solution......Page 288 How It Works......Page 289 Problem......Page 290 How It Works......Page 291 Problem......Page 292 How It Works......Page 293 How It Works......Page 294 How It Works......Page 296 Chapter 14: Managing Views......Page 299 Solution......Page 300 How It Works......Page 301 How It Works......Page 302 How It Works......Page 303 Problem......Page 304 How It Works......Page 305 How It Works......Page 306 How It Works......Page 308 How It Works......Page 309 Solution......Page 311 How It Works......Page 312 How It Works......Page 316 Chapter 15: Managing Large Tables and Databases......Page 317 How It Works......Page 318 How It Works......Page 321 How It Works......Page 322 Solution......Page 323 Solution......Page 324 Solution......Page 325 Solution......Page 326 Solution......Page 327 How It Works......Page 328 Solution......Page 329 Problem......Page 330 How It Works......Page 331 Solution......Page 332 How It Works......Page 333 How It Works......Page 334 Solution......Page 337 How It Works......Page 338 Index Overview......Page 339 Solution......Page 341 Solution......Page 343 Problem......Page 344 Solution......Page 345 Solution......Page 346 How It Works......Page 347 How It Works......Page 348 Solution......Page 349 Problem......Page 350 Solution......Page 351 Index Options......Page 352 How It Works......Page 353 How It Works......Page 354 How It Works......Page 355 Solution......Page 356 Solution......Page 357 Solution......Page 358 Problem......Page 359 How It Works......Page 360 Solution......Page 361 Problem......Page 362 How It Works......Page 363 Solution......Page 364 How It Works......Page 365 Solution......Page 366 Solution......Page 367 Solution......Page 368 Solution......Page 369 Problem......Page 370 How It Works......Page 371 Solution......Page 372 Problem......Page 373 Problem......Page 374 Problem......Page 375 How It Works......Page 376 How It Works......Page 377 How It Works......Page 378 Solution......Page 379 How It Works......Page 380 UDF Basics......Page 381 Solution......Page 382 How It Works......Page 385 Solution......Page 387 How It Works......Page 388 Solution......Page 389 How It Works......Page 390 Solution......Page 392 Problem......Page 393 Benefitting from UDFs......Page 394 Solution......Page 395 Solution......Page 397 How It Works......Page 399 Solution......Page 400 How It Works......Page 402 UDT Basics......Page 403 Solution......Page 404 How It Works......Page 405 Solution......Page 406 Solution......Page 407 How It Works......Page 409 Problem......Page 410 How It Works......Page 411 Problem......Page 412 Solution......Page 413 How It Works......Page 417 Solution......Page 419 How It Works......Page 421 Solution......Page 422 Solution......Page 425 Solution......Page 426 Problem......Page 427 Solution......Page 428 How It Works......Page 430 Solution......Page 431 How It Works......Page 432 Solution......Page 433 How It Works......Page 434 Solution......Page 435 Solution......Page 436 Solution......Page 438 Solution......Page 439 Solution......Page 440 Solution......Page 442 How It Works......Page 443 Solution......Page 444 Problem......Page 446 How It Works......Page 447 How It Works......Page 448 How It Works......Page 450 Solution......Page 451 How It Works......Page 452 Solution #1: Use RAISERROR to throw an error......Page 454 How It Works......Page 455 How It Works......Page 456 Solution: Use sp_addmessage to create user defined error message.......Page 458 How It Works......Page 459 Solution: Use sp_dropmessage to remove the user defined error message......Page 460 How It Works......Page 461 Chapter 21: Query Performance Tuning......Page 462 Query Performance Tips......Page 463 Capturing and Evaluating Query Performance......Page 464 How It Works......Page 465 Solution......Page 466 How It Works......Page 468 Solution......Page 469 How It Works......Page 471 Solution......Page 472 Solution......Page 473 Solution......Page 474 How It Works......Page 476 Solution......Page 477 Solution......Page 478 How It Works......Page 479 Solution......Page 480 Solution......Page 482 How It Works......Page 484 Solution......Page 485 Problem......Page 489 Solution......Page 490 How It Works......Page 491 Solution......Page 492 Solution......Page 493 How It Works......Page 495 Solution......Page 496 How It Works......Page 503 Solution......Page 504 Problem......Page 506 Problem......Page 507 How It Works......Page 508 How It Works......Page 509 Solution......Page 510 How It Works......Page 511 How It Works......Page 512 Solution......Page 513 How It Works......Page 514 How It Works......Page 515 chapter 23: Index Tuning and Statistics......Page 516 Index Tuning......Page 517 Solution......Page 518 How It Works......Page 520 Solution......Page 521 Problem......Page 523 How It Works......Page 524 How It Works......Page 525 Solution......Page 526 Statistics......Page 527 Solution......Page 528 Solution......Page 529 Solution......Page 530 Solution......Page 531 Problem......Page 533 Solution......Page 534 How It Works......Page 535 Solution......Page 536 Problem......Page 537 How It Works......Page 538 Solution......Page 539 How It Works......Page 540 Solution......Page 542 How It Works......Page 543 Solution......Page 545 How It Works......Page 546 Solution......Page 547 How It Works......Page 548 How It Works......Page 552 Problem......Page 555 How It Works......Page 556 How It Works......Page 557 How It Works......Page 558 How It Works......Page 559 How It Works......Page 560 Solution......Page 561 How It Works......Page 562 Solution......Page 563 Solution #2......Page 564 How It Works......Page 565 Problem #1......Page 566 How It Works......Page 567 Solution #2......Page 568 How It Works......Page 569 Solution #1......Page 570 Solution #2......Page 571 How It Works......Page 572 How It Works......Page 575 Solution......Page 577 How It Works......Page 578 How It Works......Page 580 Solution......Page 582 How It Works......Page 583 Solution......Page 585 How It Works......Page 586 How It Works......Page 587 Solution......Page 589 Solution......Page 590 How It Works......Page 591 Solution......Page 592 Problem......Page 593 How It Works......Page 594 How It Works......Page 605 Solution #1: Restore Rows from a Backup......Page 606 Solution #2: Restore Rows from a Database Snapshot......Page 608 Solution #1: Perform a File Backup......Page 612 Problem......Page 613 How It Works......Page 614 Solution......Page 615 How It Works......Page 616 Solution......Page 617 How It Works......Page 620 Solution......Page 621 How It Works......Page 624 Solution......Page 625 Solution......Page 626 How It Works......Page 627 Solution......Page 628 Solution......Page 629 How It Works......Page 630 Solution......Page 631 How It Works......Page 633 Windows Principals......Page 634 Solution......Page 635 How It Works......Page 636 Problem......Page 637 How It Works......Page 638 How It Works......Page 639 SQL Server Principals......Page 640 Solution......Page 641 How It Works......Page 642 Problem......Page 643 Solution......Page 644 Solution......Page 645 How It Works......Page 646 Solution......Page 647 Solution......Page 648 Solution......Page 649 How It Works......Page 651 Solution......Page 652 How It Works......Page 653 Solution......Page 654 Problem......Page 655 Solution......Page 656 Solution......Page 658 Solution......Page 659 How It Works......Page 660 Solution......Page 661 Problem......Page 663 Solution......Page 664 How It Works......Page 666 Chapter 29: Securables, Permissions, and Auditing......Page 667 Permissions Overview......Page 668 Solution......Page 669 How It Works......Page 671 Server-Scoped Securables and Permissions......Page 672 Solution......Page 674 Solution......Page 675 How It Works......Page 676 Database-Scoped Securables and Permissions......Page 677 Solution......Page 678 How It Works......Page 679 Solution......Page 680 How It Works......Page 681 Schema-Scoped Securables and Permissions......Page 682 Solution......Page 685 Solution......Page 686 Object Permissions......Page 688 Solution......Page 689 Managing Permissions Across Securable Scopes......Page 690 Solution......Page 691 Solution......Page 692 How It Works......Page 695 Solution......Page 696 Solution......Page 698 Problem......Page 699 Solution......Page 700 How It Works......Page 703 Solution......Page 704 How It Works......Page 706 Solution......Page 707 How It Works......Page 709 Solution......Page 710 Problem......Page 714 Solution......Page 715 How It Works......Page 717 How It Works......Page 718 How It Works......Page 721 How It Works......Page 724 How It Works......Page 726 Solution 2......Page 728 How It Works......Page 729 Index......Page 730
SQL Server 2012 T-SQL Recipes is an example-based guide to the Transact-SQL language that is at the core of SQL Server 2012. It provides ready-to-implement solutions to common programming and database administration tasks. Learn to create databases, insert and update data, generate reports, secure your data, and more. Tasks and their solutions are broken down into a problem/solution format that is quick and easy to read so that you can get the job done fast when the pressure is on.
Solutions in this book are divided into chapters by problem domain. Each chapter is a collection of solutions around a single facet of the language such as writing queries, developing triggers, and applying aggregate functions. Each solution is presented code-first, giving you a working code example to copy from and implement immediately in your own environment. Following each example is an in-depth description of how and why the given solution works. Tradeoffs and alternative approaches are also discussed.
- Focused on solutions: Look up what you need to do. Learn how to do it. Do it.
- Current: Newly updated for SQL Server 2012
- Comprehensive: Covers all common T-SQL problem domains
What you’ll learn
- Create databases, tables, and indexes
- Query and manipulate data
- Store and manage XML inside the database
- Move business logic into the database
- Resolve common performance problems.
- Build reports that matter to your business.
- Perform common backup and recovery tasks.
Who this book is for SQL Server 2012 T-SQL Recipes is aimed at technically-oriented users of SQL Server desiring to extract the full power of the platform through it's powerful, built-in programming and scripting language. Target readers include developers who use Microsoft SQL Server 2012 as their backend database, and the database administrators who create, manage, and secure those databases.
Table of Contents
- Getting Started with SELECT
- Elementary Programming
- Nulls and Other Pitfalls
- Combining Data from Multiple Tables
- Grouping and Summarizing
- Advanced SELECT Techniques
- Query Techniques for Reporting
- Inserting, Updating, Deleting
- Working with Strings
- Working with Dates
- Working with Numbers
- Transactions, Locking, Blocking, Deadlocking
- Managing Tables
- Managing Views
- Managing Indexes
- Managing Large Tables
- Stored Procedures
- User-Defined Functions
- Triggers
- Error Handling
- Query Performance
- Hints
- Index Tuning and Statistics
- XML
- Files, Filegroups, and Integrity
- Backup
- Recovery
- Principals and Users
- Securables and Permissions
- Objects and Dependencies