Cover
Copyright Page
Dedication Page
Table of Contents
Foreword
Acknowledgments
Introduction
Hardware and Software Requirements
Companion Content and Sample Database
Find Additional Content Online
Support for These Books
Questions and Comments
Chapter 1: Views
What Are Views?
ORDER BY in a View
Refreshing Views
Modular Approach
Updating Views
View Options
ENCRYPTION
SCHEMABINDING
CHECK OPTION
VIEW_METADATA
Indexed Views
Conclusion
Chapter 2: User-Defined Functions
Some Facts About UDFs
Scalar UDFs
T-SQL Scalar UDFs
Performance Issues
UDFs Used in Constraints
CLR Scalar UDFs
SQL Signature
Table-Valued UDFs
Inline Table-Valued UDFs
Split Array
ORDER Option for CLR Table-Valued UDFs
Multistatement Table-Valued UDFs
Per-Row UDFs
Conclusion
Chapter 3: Stored Procedures
Types of Stored Procedures
User-Defined Stored Procedures
Special Stored Procedures
System Stored Procedures
Other Types of Stored Procedures
The Stored Procedure Interface
Scalar Input Parameters
Table-Valued Parameters
Output Parameters
Resolution
Dependency Information
Compilations, Recompilations, and Reuse of Execution Plans
Reuse of Execution Plans
Recompilations
Variable Sniffing
Plan Guides
EXECUTE AS
Parameterizing Sort Order
CLR Stored Procedures
Conclusion
Chapter 4: Triggers
AFTER Triggers
The inserted and deleted Special Tables
Identifying the Number of Affected Rows
Identifying the Type of Trigger
Not Firing Triggers for Specific Statements
Nesting and Recursion
UPDATE and COLUMNS_UPDATED
Auditing Example
INSTEAD OF Triggers
Per-Row Triggers
Used with Views
Automatic Handling of Sequences
DDL Triggers
Database-Level Triggers
Server-Level Triggers
Logon Triggers
CLR Triggers
Conclusion
Chapter 5: Transactions and Concurrency
What Are Transactions?
Locking and Blocking
Lock Escalation
Isolation Levels
Read Uncommitted
Read Committed
Repeatable Read
Serializable
Row Versioning–Based Isolation Levels
Savepoints
Deadlocks
Simple Deadlock Example
Deadlock Caused by Missing Indexes
Deadlock with a Single Table
Conclusion
Chapter 6: Error Handling
Error Handling without the TRY/CATCH Construct
Error Handling with the TRY/CATCH Construct
TRY/CATCH
Error-Handling Functions
Errors in Transactions
Conclusion
Chapter 7: Temporary Tablesand Table Variables
Temporary Tables
Local Temporary Tables
Global Temporary Tables
Table Variables
Limitations
tempdb
Scope and Visibility
Transaction Context
Statistics
Minimally Logged Inserts
tempdb Considerations
Table Expressions
Comparison Summary
Summary Exercises
Comparing Periods
Recent Orders
Relational Division
Conclusion
Chapter 8: Cursors
Using Cursors
Cursor Overhead
Dealing with Each Row Individually
Order-Based Access
Custom Aggregates
Running Aggregations
Maximum Concurrent Sessions
Matching Problems
Conclusion
Chapter 9: Dynamic SQL
EXEC
Simple EXEC Examples
EXEC Has No Interface
Concatenating Variables
EXEC AT
sp_executesql
The sp_executesql Interface
Statement Limit
Environmental Settings
Uses of Dynamic SQL
Dynamic Maintenance Activities
Storing Computations
Dynamic Filters
Dynamic PIVOT/UNPIVOT
SQL Injection
SQL Injection: Code Constructed Dynamically at Client
SQL Injection: Code Constructed Dynamically at Server
Protecting Against SQL Injection
Conclusion
Chapter 10: Working with Date and Time
Date and Time Data Types
Date and Time Manipulation
Date and Time Functions
Literals
Identifying Weekday
Handling Date-only or Time-only Data Prior to SQL Server 2008
Examples of Date and Time Calculations
Rounding Issues
Date- and Time-Related Querying Problems
Age Problems
Overlaps
Grouping by the Week
Working Days
Generating a Series of Dates
Conclusion
Chapter 11: CLR User-Defined Types
Theoretical Introduction to UDTs
Domains and Relations
Domains and Classes
Complex Domains
Why Do We Need Complex Classes?
Language for Creating UDTs
Programming a UDT
UDT Requirements
Creating a UDT
Deploying the UDT Using T-SQL
Conclusion
Chapter 12: Temporal Support in the Relational Model
Timestamped Predicates and Propositions
Time Points
Time Points Lookup Table
Semitemporal Problems
Semitemporal Constraints
Testing Semitemporal Constraints
Queries on Semitemporal Tables
Tables with Full Temporal Support
The IntervalCID UDT
Testing IntervalCID
Full Temporal Tables Using IntervalCID
Testing Full Temporal Constraints
Queries on Tables with Full Temporal Support
Unpack and Pack
Expanded and Collapsed Forms of Interval Sets
The UNPACK Operator
PACK Operator
Sixth Normal Form in Use
Horizontal and Vertical Decompositions
Sixth Normal Form
Conclusion
Chapter 13: XML and XQuery
Converting Relational Data to XML and Vice Versa
Introduction to XML
Producing XML from Relational Data
Shredding XML to Tables
The XQuery Language in SQL Server 2008
XQuery Basics
Navigation
Iteration and Returning Values
XML Data Type
XML Support in a Relational Database
When Should You Use XML Instead of Relational Representation?
XML Serialized Objects in a Database
XML as a Stored Procedure Parameter
Dynamic Relational Schema
Relational Solutions
Object-Oriented Solutions
Using the XML Data Type for Dynamic Schema
Conclusion
Chapter 14: Spatial Data
Introduction to Spatial Data
Basic Spatial Data Concepts
Vector Data and the OGC Simple Features Type Model
Planar and Geographic Coordinates
Spatial Reference Identifiers
Standards
Working on the Ellipsoid
Data
Common Forms of Data
Finding Spatial Data
Loading Spatial Data
Sample Spatial Data
Getting Started with Spatial Data
Creating a Table with a Spatial Column
Well-Known Text
Constructing Spatial Objects from Strings and Inserting into a Table
Basic Object Interaction Tests
Basic Spatial Operations
Proximity Queries
The GEOGRAPHY Type
Spatial Data Validity
Data Validity Issues with Geometry Data
Measuring Length and Area
Comparing Length Measurements between GEOMETRY and GEOGRAPHY Instances
Comparing Area Measurements between GEOMETRY and GEOMETRY Instances
Indexing Spatial Data
Spatial Index Basics
SQL Server Spatial Indexes
Using Spatial Indexes
Geography Indexes
Query Plans
Integration with Spatial Methods
Using Spatial Data to Solve Problems
Loading Spatial Data
Loading Spatial Data from Text Files
Finding Site Locations within Geographic Regions
Nearest Neighbor Searches
Spatial Joins
Extending Spatial Support with CLR Routines
Types on the Client
User Defined Aggregate: Union and Dissolve
Sinks and Builders: Linear Transformations
Conclusion
Chapter 15: Tracking Access and Changes to Data
Which Technology Do I Use?
Approaches Using Earlier SQL Server Versions
Technologies Added in SQL Server 2008
Extended Events Implementation
Extended Events Object Hierarchy
Implementing a Scenario Using Extended Events
Exploring Extended Events Concepts
SQL Server Audit Implementation
Auditing Object Hierarchy
Implementing a Scenario Using Auditing
Exploring SQL Server Audit Concepts
Change Tracking Implementation
Implementing a Scenario Using Change Tracking
Change Tracking Management Issues
Microsoft Sync Framework
Change Data Capture Implementation
Implementing a Scenario Using Change Data Capture
Change Data Capture Management Issues
Conclusion
Chapter 16: Service Broker
Dialog Conversations
Conversations
Reliable
Messages
DEFAULT Message Type
Queues
Beginning and Ending Dialogs
Conversation Endpoints
Conversation Groups
Sending and Receiving
Activation
Internal Activation
External Activation
Conversation Priority
Broker Priority Object
Sample Dialog
Poison Messages
Dialog Security
Asymmetric Key Authentication
Configuring Dialog Security
Routing and Distribution
Adjacent Broker Protocol
Service Broker Endpoints
Routes
Troubleshooting
Scenarios
Reliable SOA
Asynchronous Processing
Where Does Service Broker Fit?
What Service Broker Is
What Service Broker Isn’t
Service Broker and MSMQ
Service Broker and BizTalk
Service Broker and Windows Communication Foundation
Conclusion
Appendix A: Companion to CLR Routines
Create the CLRUtilities Database: SQL Server
Development: Visual Studio
Create a Project
Develop Code
Deployment and Testing: Visual Studio and SQL Server
Build and Deploy the Solution
Test the Solution
Index
About the Authors
Marketing Pages
Survey Page