The Guru's Guide to Transact-SQL



Similar documents
FHE DEFINITIVE GUIDE. ^phihri^^lv JEFFREY GARBUS. Joe Celko. Alvin Chang. PLAMEN ratchev JONES & BARTLETT LEARN IN G. y ti rvrrtuttnrr i t i r

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

MOC 20461C: Querying Microsoft SQL Server. Course Overview

Instant SQL Programming

AV-005: Administering and Implementing a Data Warehouse with SQL Server 2014

Querying Microsoft SQL Server

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Course ID#: W 35 Hrs. Course Content

Oracle Database: SQL and PL/SQL Fundamentals

DBMS / Business Intelligence, SQL Server

Oracle Database 10g: Introduction to SQL

Querying Microsoft SQL Server 20461C; 5 days

Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008

LearnFromGuru Polish your knowledge

Oracle Database: SQL and PL/SQL Fundamentals NEW

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

MOC QUERYING MICROSOFT SQL SERVER

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

SQL Server. 1. What is RDBMS?

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Querying Microsoft SQL Server 2012

Oracle Database 12c: Introduction to SQL Ed 1.1

Oracle Database: SQL and PL/SQL Fundamentals

Course 10774A: Querying Microsoft SQL Server 2012

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

Oracle 10g PL/SQL Training

Beginning SQL Server Administration. Apress. Rob Walters Grant Fritchey

Oracle SQL. Course Summary. Duration. Objectives

SQL Server 2012 Query. Performance Tuning. Grant Fritchey. Apress*

Introduction. Part I: Finding Bottlenecks when Something s Wrong. Chapter 1: Performance Tuning 3

Database Programming with PL/SQL: Learning Objectives

Demystified CONTENTS Acknowledgments xvii Introduction xix CHAPTER 1 Database Fundamentals CHAPTER 2 Exploring Relational Database Components

SQL Server 2008 Administration

BIRT: A Field Guide to Reporting

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

Microsoft SQL Server 2008 Bible

The Data Access Handbook

Netezza SQL Class Outline

SQL SERVER DEVELOPER Available Features and Tools New Capabilities SQL Services Product Licensing Product Editions Will teach in class room

David Dye. Extract, Transform, Load

Oracle Database: SQL and PL/SQL Fundamentals NEW

Expert Oracle. Database Architecture. Techniques and Solutions. 10gr, and 11g Programming. Oracle Database 9/, Second Edition.

Oracle8/ SQLJ Programming

Development and Management

PL/SQL Programming Workbook

SQL Server Developer Training Program. Topics Covered

Course -Oracle 10g SQL (Exam Code IZ0-047) Session number Module Topics 1 Retrieving Data Using the SQL SELECT Statement

Querying Microsoft SQL Server 2012

Oracle Database: Introduction to SQL

Teradata Utilities Class Outline

Querying Microsoft SQL Server (20461) H8N61S

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

SQL SERVER TRAINING CURRICULUM

Oracle Database: Introduction to SQL

Design and Implementation

Saskatoon Business College Corporate Training Centre

Oracle Database 11 g Performance Tuning. Recipes. Sam R. Alapati Darl Kuhn Bill Padfield. Apress*

Oracle Database 11g SQL

ORACLE 9I / 10G / 11G / PL/SQL COURSE CONTENT

Choosing a Data Model for Your Database

How To Improve Performance In A Database

Database Migration from MySQL to RDM Server

NUTECH COMPUTER TRAINING INSTITUTE 1682 E. GUDE DRIVE #102, ROCKVILLE, MD WEB: TEL:

SQL Server An Overview

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

Information Systems SQL. Nikolaj Popov

Data Tool Platform SQL Development Tools

Guide to SQL Programming: SQL:1999 and Oracle Rdb V7.1

Migrating from Sybase to SQL Server

SQL Server 2008 Core Skills. Gary Young 2011

Oracle Database: Introduction to SQL

CHAPTER 2 DATABASE MANAGEMENT SYSTEM AND SECURITY

SQL Server 2016 New Features!

Querying Microsoft SQL Server Querying Microsoft SQL Server D. Course 10774A: Course Det ails. Co urse Outline

BCA. Database Management System

Querying Microsoft SQL Server 2012

Discovering SQL. Wiley Publishing, Inc. A HANDS-ON GUIDE FOR BEGINNERS. Alex Kriegel WILEY

Programming with SQL

IT2305 Database Systems I (Compulsory)

Top 10 Oracle SQL Developer Tips and Tricks

Topics Advanced PL/SQL, Integration with PROIV SuperLayer and use within Glovia

Duration Vendor Audience 5 Days Oracle Developers, Technical Consultants, Database Administrators and System Analysts

MS Designing and Optimizing Database Solutions with Microsoft SQL Server 2008

ICAB4136B Use structured query language to create database structures and manipulate data

How To Create A Table In Sql (Ahem)

IT2304: Database Systems 1 (DBS 1)

Using SQL Server Management Studio

Introduction and Overview for Oracle 11G 4 days Weekends

ATTACHMENT 6 SQL Server 2012 Programming Standards

Oracle Database 11g: SQL Tuning Workshop Release 2

Upon completion of the program, students are given a full support to take and pass Microsoft certification examinations.

W I S E. SQL Server 2008/2008 R2 Advanced DBA Performance & WISE LTD.

50439B: Basics of Transact SQL with SQL Server 2008 R2

Physical Database Design and Tuning


Oracle Database 11g: Administer a Data Warehouse

OPTIMIZING QUERIES IN SQL SERVER 2008

"Charting the Course... MOC AC SQL Server 2014 Performance Tuning and Optimization. Course Summary

Transcription:

The Guru's Guide to Transact-SQL Ken Henderson HLuHB Darmstadt TT 15169877 ADDISON-WESLEY Boston San Francisco New York Toronto Montreal London Munich Paris Madrid Capetown Sydney Tokyo Singapore Mexico City

Contents Foreword xvi Preface xvii About the Sample Databases Results Abridged xviii On Formality xviii Acknowledgments xix xviii Chapter 1: Introductory Transact-SQL 1 Choosing a SQL Editor 2 Creating a Database 3 Creating Tables 3 Inserting Data 5 Updating Data 6 Deleting Data 7 Querying Data 7 Column Lists 8 SELECTing Variables and Expressions Functions 9 Converting Data Types 10 CASE 10 Aggregate Columns 11 Filtering Data 11 Joins 13 Outer Joins 14 Other Types of Joins 16 Subqueries 18 Grouping Data 18 HAVING 19 Ordering Data 19 Column Aliases 20 Table Aliases 21

vi Contents Managing Transactions 21 Summary 22 Chapter 2: Transact-SQL Data Type Nuances 23 Dates 23 Y2K and Other Date Problems 24 Date Functions 25 Dates and Simple Arithmetic 26 Determining Time Gaps 27 Building Calendars 31 Strings 34 Concatenation 35 Char vs. Varchar 35 SET ANSI_PADDING 36 String Functions 37 CHARINDEX() 37 SOUNDEX() 37 A Better SOUNDEX() 38 DIFFERENCEO 46 Xp_sprintf 47 Xp_sscanf 47 Masks 48 Executing Strings 49 Unicode 54 Numerics 54 Floating Point Fun 55 Division by Zero 57 Funny Money 58 Formatting Numeric Data 58 BLOBs 59 Caveats 59 Retrieving BLOB Data 60 Updating BLOB Data 63 BLOB Updates and the Transaction Log 65 Bits 66 UNIQUEIDENTIFIER 67 Cursor Variables 68 Timestamps 73 Summary 75 Chapter 3: Missing Values 77 NULL and Expressions 78 NULL and Functions 78 NULL and ANSI SQL 79

Contents vii NULL and Stored Procedures 81 NULL If You Must 83 Chapter 4: DDL Insights 85 CREATE TABLE 86 Some Thoughts on Referential Integrity 86 Foreign Keys 86 ANSI Referential Actions 87 The NULL Exception 87 Unique Index Requirement 87 No TRUNCATE TABLE 87 Default Constraints 87 Dropping Objects 89 CREATE INDEX 90 No Bit or BLOB Indexes 90 No Computed Column Indexes 90 PADJNDEX 90 DROP_EXISTING 91 TEMPORARY OBJECTS 91 No More Unusable Temporary Objects 91 Can't Create Objects in Other Databases 91 Temporary Stored Procedures 92 Increased Temporary Table Name Length 92 Global Temporary Status Tables 92 Object Naming and Dependencies 93 Changing the Database Context Temporarily 94 Temporary Table Indexes 94 Be Wary of Unusable Views 94 Object Dependencies 94 Summary 95 Chapter 5: DML Insights 97 INSERT 97 DEFAULT and NULL 98 SET IDENTITYJNSERT 99 INSERT...DEFAULT VALUES 100 INSERT...SELECT 100 INSERT...EXEC 101 Extended Procedures 102 INSERT and Errors 103 Using INSERT to Remove Duplicate Rows 104 INSERT and Clustered Indexes 105 BULK INSERT 105 BULK INSERT and Triggers 106

BULK INSERT and Constraints 106 BULK INSERT and Identity Columns 106. UPDATE 107 The Halloween Problem 108 UPDATE and CASE 109 Using UPDATE to Check Constraints 110 Limiting the Number of Rows Affected by an UPDATE 111 Swapping Column Values with UPDATE 112 UPDATE and Cursors 113 DELETE 114 DELETE and Cursors 116 TRUNCATE TABLE 117 Detecting DML Errors 118 Summary 118 Chapter 6: The Mighty SELECT Statement 119 Simple SELECTS 119 Computational and Derived Fields 120 SELECT TOP 121 Derived Tables 122 Joins 126 Outer Joins and Join Order 126 Predicates 128 BETWEEN 128 LIKE 131 EXISTS 133 NULLs 134 EXISTS and IN 135 Joins 136 Result Set Emptiness 137 EXISTS Outside WHERE and HAVING 137 IN 138 Optimizing IN 138 ANY and ALL 140 Subqueries 140 WHERE and Subqueries 141 Correlated Subqueries 141 Relational Division 145 Aggregate Functions 147 GROUP BY and HAVING 150 Pivot Tables 152 CUBE and ROLLUP 153 HAVING 157 UNION 157 ORDER BY 159 Summary 161

Contents ix Chapter 7: Views 163 Restrictions 163 DML Restrictions 164 ANSI SQL Schema VIEWs 165 Getting a VIEW'S Source Code 166 Updatable VIEWs 166 WITH CHECK OPTION 167 Derived Tables 167 Dynamic VIEWs 168 Partitioning Data Using Views 170 Summary 172 Chapter 8: Statistical Functions 173 The Case for CASE 174 Efficiency Concerns 176 Variance and Standard Deviation 176 Medians 177 The Identity Column Technique 177 The CASE Technique 181 Vector Medians 183 Duplicate Values 184 Clipping 185 Returning the Top n Rows 186 SETROWCOUNT 188 Rankings 190 Modes 193 Histograms 193 Stratified Histograms 194 Cumulative and Sliding Aggregates 195 Sliding Aggregates 196 Extremes 197 Determining Extreme Attributes 198 Summary 199 Chapter 9: Runs and Sequences 201 Sequences 201 Time Series Fluctuation 202 Sampling Every nth Value 203 Regions 203 Relative Condition Regions 204 Constraining Region Sizes 205 Region Boundaries 206 Runs 207 Regions 207 Region Boundaries 208 Constrained Regions 209

x Contents Intervals 210 Partitioned Intervals 211 Summary 212 Chapter 10: Arrays 213 Arrays as Big Strings 213 Modifying Array Elements 219 Arrays as Tables 220 Sorting 221 Transposing Dimensions 223 Ensuring Array Integrity 225 Reshaping the Array 225 Comparing Arrays 226 Summary 228 Chapter 11: Sets 229 Unions 230 Differences 231 Intersections 234 Subsets 235 Returning Every nth Row 238 Summary 239 Chapter 12: Hierarchies 241 Simple Hierarchies 241 Multilevel Hierarchies 242 Indenting a Hierarchy 245 Another Approach 246 Listing Leaf Nodes 248 Indented Lists 249 Summary 250 Chapter 13: Cursors 251 On Cursors and ISAMs 251 Types of Cursors 253 Forward-Only Cursors 254 Dynamic Cursors 255 Static Cursors 256 Keyset Cursors 257 Appropriate Cursor Use 258 Dynamic Queries 259 Row-Oriented Operations 261 Scrollable Forms 263

Contents xi T-SQL Cursor Syntax 263 DECLARE CURSOR 264 Global vs. Local Cursors 267 OPEN 268 FETCH 269 CLOSE 272 DEALLOCATE 272 Configuring Cursors 272 Asynchronous Cursors 272 ANSI /ISO Automatic Cursor Closing 274 Defaulting to Global or Local Cursors 276 Updating Cursors 276 Cursor Variables 277 Cursor Stored Procedures 279 Optimizing Cursor Performance 279 Summary 281 Chapter 14: Transactions 283 Transactions Defined 283 The ACID Test 283 Atomicity 284 Consistency 284 Isolation 284 Durability 284 How SQL Server Transactions Work 284 Transactions and Nonlogged Operations 285 Transactions and Triggers 285 Types of Transactions 285 Automatic Transactions 285 Implicit Transactions 286 User-Defined Transactions 286 Distributed Transactions 287 Avoiding Transactions Altogether 287 Commands That Minimize Logging 287 Read-Only and Single-User Databases 287 Automatic Transaction Management 288 Transaction Isolation Levels 289 READ UNCOMMITTED 289 READ COMMITTED 290 REPEATABLE READ 291 SERIALIZABLE 292 Transaction Commands and Syntax 293 Nested Transactions 293 SAVE TRAN and Save Points 296

xii Contents Avoid Accidental ROLLBACKS 297 Invalid T-SQL Syntax in Transactions 299 Debugging Transactions 299 Optimizing Transactional Code 300 Summary 301 Chapter 15: Stored Procedures and Triggers 303 Stored Procedure Advantages 304 Internals 305 Creation 305 User Execution 305 Compilation 305 Server Execution 306 Creating Stored Procedures 307 Creation Tips 308 Temporary Procedures 312 System Procedures 312 Extended Procedures 314 Faux Procedures 316 Executing Stored Procedures 316 INSERT and EXEC 317 Environmental Concerns 317 Parameters 320 Output Parameters 321 Result Codes 322 Listing Procedure Parameters 323 General Parameter Notes 323 Important Automatic Variables 323 Flow Control Language 324 Errors 325 @ ERROR 328 xpjogevent 328 Nesting 328 Recursion 328 Autostart Procedures 330 Encryption 330 Triggers 330 Debugging Procedures 334 Summary 335 Chapter 16: Transact-SQL Performance Tuning 337 General Performance Guidelines 337 Database Design Performance Tips 338 Index Performance Tips 340

Contents xm SELECT Performance Tips 342 INSERT Performance Tips 344 Bulk Copy Performance Tips 344 DELETE and UPDATE Performance Tips 346 Cursor Performance Tips 346 Stored Procedure Performance Tips 347 SARGs 351 Denormalization 368 Basic Guidelines 369 Basic Techniques 369 Contrived Columns 370 Redundant Data 370 Summary Tables 371 Vertical Partitioning 374 Horizontal Partitioning 380 The Query Optimizer 384 Join Optimizations 385 Nested Loops 385 Merge Joins 385 Hash Joins 386 Index Optimizations 387 Index Joins 387 Index Merging and Intersection 388 Data Warehouse Optimizations 389 Semijoins 391 Grouping Optimizations 391 Predicate Clause Optimizations 391 The Index Tuning Wizard 394 Profiler 396 Perfmon 397 User Counters 398 Perfmon-Related DBCC Commands 398 Summary 399 Chapter 17: Administrative Transact-SQL 401 GUI Administration 401 System Stored Procedures 402 Administrative Transact-SQL Commands 402 Administrative System Functions 402 Administrative Automatic Variables 402 Where's the Beef? 404 Status Routines 405 sp_active_processes 405 sp_pss 410

xiv Contents sp_find_root_blocker 414 sp_lock_verbose 416 Catalog Procedures 419 sp_table 419 sp_dir 421 sp_object 427 Maintenance Routines 432 sp_update_stats_all 432 sp_updateusage_all 435 sp_rebuildindexes_all 437 sp_dbbackup 440 sp_copyfile 443 sp_make_portable 445 INIT_SERVER.SQL 448 sp_readtextfile 453 Scripting Routines 456 Summary 461 Chapter 18: Full-Text Search 463 Full-Text Predicates 468 The CONTAINS() Predicate 469 The FREETEXTO Predicate 471 Rowset Functions 471 The CONTAINSTABLE() Rowset Function 472 The FREETEXTTABLE() Rowset Function 474 Summary 474 Chapter 19: OLE Automation 475 sp_exporttable 476 sp_importtable 480 sp_getsqlregistry 484 Summary 486 Chapter 20: Undocumented T-SQL 487 Defining Undocumented 487 Undocumented DBCC Commands 488 DBCC ADDEXTENDEDPROC(procname,DLL) 488 DBCC ADDINSTANCE(object,instance) 488 DBCC BCPTABLOCK(dbid, tabid, setflag) 489 DBCC BUFFER(dbid[ > objid][,numberofbuffers][,printopt {01112}]) 489 DBCC BYTES(startingaddress,length) 490 DBCC CALLFULLTEXT(funcid[,catid][,objid]) 490 DBCC DBCONTROL(dbname,option) 491 DBCC DBINFO(dbname) 491 DBCC DBRECOVER(dbname) 492 DBCC DBTABLE(dbid) 492 DBCC DELETEINSTANCE(object,instance) 493

Contents xv DBCC DES(dbid,objid) 493 DBCC DETACHDB(dbname) 493 DBCC DROPCLEANBUFFERS 493 DBCC DROPEXTENDEDPROC(procname) 493 DBCC ERRORLOG 494 DBCC EXTENTINFO(dbname, tablename, indid) 494 DBCC FLUSHPROCINDB(dbid) 494 DBCC FREEPROCCACHE 494 DBCC IND(dbid, objid[,printopt {01112}]) 494 DBCC LOCKOBJECTSCHEMA (objname) 495 DBCC LOG(dbid) 495 DBCC PAGE (dbidldbnarne, filenum, pagenum [,printopt][,cacheopt]) 496 DBCC PRTIPAGE(dbid, objid, indexidf, printopt {01112}]) 497 DBCC RESOURCE 497 DBCC SETINSTANCE(object,counter,instance,val) 498 DBCC TAB(dbid,objid[,printopt {01112}}]) 499 DBCC UPGRADEDB(dbname) 499 Undocumented Functions and Variables 499 ENCRYPT(string) 499 GET_SID(usemame) 499 OBJECT_ID(..,'local') 500 PWDCOMPARE(str,pwd,oldenc) 500 PWDENCRYPT(str) 501 TSEQUAL(tsl,ts2) 501 @@MICROSOFTVERSION 502 Undocumented Trace Flags 502 Undocumented Procedures 503 Summary 509 Chapter 21: Potpourri 511 Obscure Functions 511 Status Functions 511 Property Functions 513 Identifier Functions 514 Index Functions 515 Data Functions 518 Unusual String Functions 519 Data Scrubbing 521 Removing Duplicates 524 Iteration Tables 525 Summary 526 Appendix: Suggested Resources 527 Books 527 Internet Resources 528 Index 529