Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xx
Contents
Cursor scope 585
Cursors and transactions
585
Cursor Strategies
586
Refactoring Complex-Logic Cursors
587
Update query with user-defined function
588
Multiple queries
589
Query with case expression
590
Summary
591
Chapter 23: T-SQL Error Handling 593
Legacy Error Handling 593
@@error system function
594
@@rowcount system function
595
Raiserror
596
Thesimpleraiserrorform
596
The improved raiserror form
596
Error severity
597
Stored messages
597
Try Catch
600
Catch block
601
Nested try/catch and rethrown errors
603
T-SQL Fatal Errors
604
Summary
605
Chapter 24: Developing Stored Procedures 607
Managing Stored Procedures 608
Create, alter, and drop
609
Executing a stored procedure
609
Returning a record set
610
Compiling stored procedures
610
Stored procedure encryption
610
System stored procedures
611
Using stored procedures within queries
611
Executing remote stored procedures
612
Passing Data to Stored Procedures
613
Input parameters
613
Parameter defaults
614
Table-valued parameters
616
Returning Data from Stored Procedures
618
Output parameters
619
Using the Return Command
620
Path and scope of returning data
621
Summary
622
Chapter 25: Building User-Defined Functions 623
Scalar Functions 625
Limitations
625
Creating a scalar function
625
Calling a scalar function
627
Inline Table-Valued Functions
627
Creating an inline table-valued function
628
Calling an inline table-valued function
628
Using parameters
629
Correlated user-defined functions
630
Creating functions with schema binding
632
Multi-Statement Table-Valued Functions
632
Creating a multi-statement table-valued function
633
Calling the function
634
Summary
634
Chapter 26: Creating DML Triggers 635
Trigger Basics 635
Transaction flow
636
Creating triggers
637
After triggers
638
Instead of triggers
639
xx
www.getcoolebook.com
Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxi
Contents
Trigger limitations 640
Disabling triggers
641
Listing triggers
641
Triggers and security
642
Working with the Transaction
642
Determining the updated columns
642
Inserted and deleted logical tables
644
Developing multi-row-enabled triggers
645
Multiple-Trigger Interaction
647
Trigger organization
647
Nested triggers
647
Recursive triggers
648
Instead of and after triggers
650
Multiple after triggers
651
Transaction-Aggregation Handling
651
The inventory-transaction trigger
652
The inventory trigger
654
Summary
655
Chapter 27: DDL Triggers 657
Managing DDL Triggers 658
Creating and altering DDL triggers
658
Trigger scope
659
DDL triggers and security
660
Enabling and disabling DDL triggers
661
Removing DDL triggers
661
Developing DDL Triggers
662
EventData()
662
Preventing database object changes
663
Summary
664
Chapter 28: Building Out the Data Abstraction Layer 665
CRUD Stored Procedures 666
Google-Style Search Procedure
667
Summary
672
Chapter 29: Dynamic SQL and Code Generation 673
Executing Dynamic SQL 674
sp_executeSQL
674
Parameterized queries
674
Developing dynamic SQL code
675
Code generation
677
Preventing SQL Injection
680
Appending malicious code
680
Or
1 = 1 680
Password? What password?
681
Preventing SQL Server injection attacks
681
Summary
682
Part V Data Connectivity
Chapter 30: Bulk Operations 685
Bulk Insert 686
Bulk Insert Options
687
BCP
689
Summary
689
Chapter 31: Executing Distributed Queries 691
Distributed Query Concepts 691
Accessing a Local SQL Server Database
693
Linking to External Data Sources
694
Linking to SQL Server with Management Studio
694
Linking to SQL Server with T-SQL
697
Linking with non–SQL Server data sources
700
xxi
www.getcoolebook.com
Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxii
Contents
Developing Distributed Queries 704
Distributed queries and Management Studio
704
Distributed views
704
Local-distributed queries
705
Pass-through distributed queries
708
Distributed Transactions
710
Distributed Transaction Coordinator
711
Developing distributed transactions
711
Monitoring distributed transactions
713
Summary
714
Chapter 32: Programming with ADO.NET 3.5 715
An Overview of ADO.NET 716
ADO
716
The ADO object model
720
ADO.NET
729
ADO.NET in Visual Studio 2008
740
Server Explorer
740
Debugging ADO.NET
741
Application tracing
741
Application Building Basics
742
Connecting to SQL Server
743
What’s new in ADO.NET 3.5
743
Stored procedures vs. parameterized/ad-hoc queries
744
Data adapters
746
DataReaders and Recordsets
747
Streams
748
Asynchronous execution
748
Using a single database value
748
Data modification
749
Binding to controls
750
Summary
750
Chapter 33: Sync Framework 751
Sync Framework example 752
Sync Framework overview
760
Sync services for ADO.NET 2.0
764
Summary
773
Chapter 34: LINQ 775
LINQ Overview 776
What Is LINQ?
777
Standard Query Operators
777
Query expression syntax
778
LINQ to SQL
784
Example 1: Manually applying the mappings
786
Example 2: The easy way
788
LINQ to XML
790
LINQ to XML example
792
Traversing XML
796
LINQ to DataSet
796
Querying a DataSet using LINQ to DataSet
796
Data binding with LINQ to DataSet
800
LINQ to Entities
801
Creating and querying entities using LINQ
802
Querying multiple tables using LINQ to Entities and the Entity Framework
805
Summary
806
Chapter 35: Asynchronous Messaging with Service Broker 807
Configuring a Message Queue 808
Working with Conversations
809
Sending a message to the queue
809
Receiving a message
810
Monitoring Service Broker
812
Summary
812
xxii
www.getcoolebook.com
Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxiii
Contents
Chapter 36: Replicating Data 813
Replication Concepts 815
Types of replication
816
Replication agents
816
Transactional consistency
817
Configuring Replication
818
Creating a publisher and distributor
818
Creating a snapshot/transactional publication
819
Creating a push subscription to a transactional/snapshot publication
823
Creating a pull subscription to a transactional/snapshot publication
824
Creating a peer-to-peer topology
825
Creating a merge publication
825
Web synchronization
826
Summary
827
Chapter 37: Performing ETL with Integration Services 829
Design Environment 830
Connection managers
832
Variables
833
Configuring elements
834
Event handlers
839
Executing a package in development
839
Integration Services Package Elements
840
Connection managers
840
Control flow elements
843
Data flow components
850
Maintainable and Manageable Packages
860
Logging
861
Package configurations
863
Checkpoint restart
864
Deploying Packages
864
Installing packages
865
Executing packages
865
Summary
866
Chapter 38: Access as a Front End to SQL Server 867
Access–SQL Server Use Case Scenarios 868
Access projects or ODBC linked tables?
868
Migrating from Access to SQL Server
868
Designing Your Access Front End
870
Connecting to SQL Server
870
Linking to tables and views
871
Caching data in local tables using pass-through queries
873
Extending the power of pass-through queries using table-valued parameters (TVPs)
874
Monitoring and Troubleshooting
876
Ad Hoc Querying and Reporting
876
Pre-aggregating data on the server
877
Sorting and filtering data
877
Creating forms and reports
880
Exporting and publishing data
880
Managing your SQL Server databases
880
Summary
880
Part VI Enterprise Data Management
Chapter 39: Configuring SQL Server 883
Setting the Options 883
Configuring the server
884
Configuring the database
887
Configuring the connection
887
Configuration Options
889
Displaying the advanced options
889
Start/Stop configuration properties
892
xxiii
www.getcoolebook.com
Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxiv
Contents
Memory-configuration properties 895
Processor-configuration properties
899
Security-configuration properties
906
Connection-configuration properties
909
Advanced server-configuration properties
914
Configuring database auto options
919
Cursor-configuration properties
921
SQL ANSI–configuration properties
923
Trigger configuration properties
929
Database-state-configuration properties
931
Recovery-configuration properties
933
Summary
937
Chapter 40: Policy-Based Management 939
Defining Policies 940
Management facets
941
Health conditions
944
Policies
946
Evaluating Policies
949
Summary
951
Chapter 41: Recovery Planning 953
Recovery Concepts 954
Recovery Models
955
Simple recovery model
956
The full recovery model
957
Bulk-logged recovery model
959
Setting the recovery model
960
Modifying recovery models
960
Backing Up the Database
961
Backup destination
961
Backup rotation
961
Performing backup with Management Studio
962
Backing up the database with code
965
Verifying the backup with code
967
Working with the Transaction Log
967
Inside the transaction log
967
Backing up the transaction log
969
Truncating the log
970
The transaction log and simple recovery model
971
Recovery Operations
971
Detecting the problem
971
Recovery sequences
972
Performing the restore with Management Studio
972
Restoring with T-SQL code
975
System Databases Recovery
980
Master database
980
MSDB system database
981
Performing a Complete Recovery
982
Summary
982
Chapter 42: Maintaining the Database 985
DBCC Commands 985
Database integrity
988
Index maintenance
993
Database file size
997
Miscellaneous DBCC commands
1001
Managing Database Maintenance
1002
Planning database maintenance
1002
Maintenance plan
1002
Command-line maintenance
1009
Monitoring database maintenance
1009
Summary
1010
Chapter 43: Automating Database Maintenance with SQL Server Agent 1011
Setting Up SQL Server Agent 1011
Understanding Alerts, Operators, and Jobs
1016
xxiv
www.getcoolebook.com
Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxv
Contents
Managing Operators 1016
Managing Alerts
1017
Creating user-defined errors
1018
Creating an alert
1018
Managing Jobs
1021
Creating a job category
1023
Creating a job definition
1024
Setting up the job steps
1025
Configuring a job schedule
1027
Handling completion-, success-, and failure-notification messages
1028
Database Mail
1028
Configuring database mail
1030
Summary
1037
Chapter 44: Transferring Databases 1039
Copy Database Wizard 1039
Working with SQL Script
1048
Detaching and Attaching
1052
Import and Export Wizard
1054
Summary
1058
Chapter 45: Database Snapshots 1059
How Database Snapshots Work 1060
Creating a Database Snapshot
1061
Using Your Database Snapshots
1064
Performance Considerations and Best Practices
1067
Summary
1068
Chapter 46: Log Shipping 1069
Availability Testing 1070
Warm Standby Availability
1071
Defining Log Shipping
1072
Configuring log shipping
1074
Checking Log Shipping Configuration
1085
Monitoring Log Shipping
1085
Modifying or Removing Log Shipping
1087
Switching Roles
1089
Returning to the original primary server
1089
Summary
1090
Chapter 47: Database Mirroring 1091
Database Mirroring Overview 1091
Defining Database Mirroring
1094
Configuring database mirroring
1098
Checking a Database Mirroring Configuration
1108
Monitoring Database Mirroring
1111
Monitoring using Database Mirroring Monitor
1111
Monitoring using System Monitor
1112
Monitoring using SQL Server Profiler
1115
Pausing or Removing Database Mirroring
1115
Role Switching
1116
Summary
1118
Chapter 48: Clustering 1119
SQL Server 2008 Failover Clustering Basics 1119
How SQL Server 2008 failover clustering works
1122
SQL Server 2008 failover clustering topologies
1123
Enhancements in SQL Server 2008 Failover Clustering
1124
SQL Server 2008 Failover Clustering Setup
1126
Planning SQL Server 2008 failover clustering
1127
SQL Server 2008 prerequisites
1130
Creating a single-node SQL Server 2008 failover cluster
1131
Adding a node to an existing SQL Server 2008 failover cluster
1144
Post-installation tasks
1146
Uninstalling a SQL Server 2008 failover cluster
1148
xxv
www.getcoolebook.com
Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxvi
Contents
Installing a failover cluster using a command prompt 1149
Rolling upgrade and patching
1151
Maintaining a SQL Server 2008 failover cluster
1161
Troubleshooting a SQL Server 2008 failover cluster
1163
Summary
1164
Part VII Security
Chapter 49: Authenticating Principals 1169
Server-Level Security 1171
Database-Level Security
1171
Windows Security
1172
Using Windows Security
1172
SQL Server login
1172
Server Security
1172
SQL Server authentication mode
1173
Windows Authentication
1174
SQL Server logins
1178
Database Security
1182
Guest logins
1182
Granting access to the database
1182
Fixed database roles
1184
Assigning fixed database roles with Management Studio
1185
Application roles
1185
Summary
1186
Chapter 50: Authorizing Securables 1187
Object Ownership 1187
Object Security
1188
Standard database roles
1188
Object permissions
1188
Granting object permissions with code
1189
Revoking and denying object permission with code
1190
The public role
1190
Managing roles with code
1190
Hierarchical role structures
1191
Object security and Management Studio
1192
Stored procedure execute as
1193
A Sample Security Model Example
1194
Views and Security
1195
Summary
1196
Chapter 51: Data Cryptography 1197
Introduction to Cryptography 1197
Types of encryption
1197
The hierarchy of keys
1198
Encrypting Data
1199
Encrypting with a passphrase
1199
Encrypting with a symmetric key
1200
Using asymmetric keys
1202
Using certificates
1202
Transparent Data Encryption
1203
Summary
1203
Chapter 52: Row-Level Security 1205
The Security Table 1206
Assigning Permissions
1207
Assigning security
1207
Handling security-level updates
1212
Checking Permissions
1214
The security-check stored procedure
1214
The security-check function
1215
Using the NT login
1216
The security-check trigger
1218
Summary
1219
xxvi
www.getcoolebook.com
Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxvii
Contents
Part VIII Monitoring and Auditing
Chapter 53: Data Audit Triggers 1223
AutoAudit 1224
Installing AutoAudit
1224
The audit table
1225
Running AutoAudit
1225
_Modified trigger
1226
Auditing changes
1227
Viewing and undeleting deleted rows
1228
Viewing row history
1229
Backing out AutoAudit
1229
Auditing Complications
1229
Auditing related data
1230
Auditing select statements
1230
Data auditing and security
1230
Data auditing and performance
1230
Summary
1231
Chapter 54: Schema Audit Triggers 1233
SchemaAudit Table 1234
SchemaAudit Trigger
1234
Summary
1236
Chapter 55: Performance Monitor 1237
Using Performance Monitor 1238
System monitor
1238
Counter Logs
1241
Summary
1242
Chapter 56: Tracing and Profiling 1243
Running Profiler 1244
Defining a new trace
1244
Selecting events and data columns
1246
Filtering events
1247
Organizing columns
1248
Running the trace
1249
Using the trace file
1249
Integrating Performance Monitor data
1249
Using SQL Trace
1250
Preconfigured traces
1252
Summary
1253
Chapter 57: Wait States 1255
Observing Wait State Statistics 1256
Querying wait states
1256
Activity Monitor
1257
Analyzing Wait States
1258
Summary
1258
Chapter 58: Extended Events 1261
XE Components 1261
Packages
1262
Objects
1263
XE Sessions
1263
Summary
1265
Chapter 59: Change Tracking 1267
Configuring Change Tracking 1268
Enabling the database
1268
Auto cleanup
1269
Enabling tables
1270
Enabling all tables
1272
Internal tables
1272
xxvii
www.getcoolebook.com
Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxviii
Contents
Querying Change Tracking 1273
Version numbers
1273
Changes by the row
1275
Coding a synchronization
1276
Change Tracking Options
1280
Column tracking
1280
Determining latest version per row
1281
Capturing application context
1281
Removing Change Tracking
1282
Summary
1283
Chapter 60: Change Data Capture 1285
Enabling CDC 1286
Enabling the database
1286
Enabling tables
1287
Working with Change Data Capture
1288
Examining the log sequence numbers
1289
Querying the change tables
1290
Querying net changes
1292
Walking through the change tables
1294
Removing Change Data Capture
1294
Summary
1295
Chapter 61: SQL Audit 1297
SQL Audit Technology Overview 1297
Creating an Audit
1298
Defining the target
1299
Using T-SQL
1300
Enabling/disabling the audit
1300
Server Audit Specifications
1300
Adding actions
1301
Creating with T-SQL
1301
Modifying Server Audit Specifications
1302
Database Audit Specifications
1302
Viewing the Audit Trail
1302
Summary
1304
Chapter 62: Management Data Warehouse 1305
Configuring MDW 1306
Configuring a data warehouse
1306
Configuring a data collection
1306
The MDW Data Warehouse
1308
Summary
1309
Part IX Performance Tuning and Optimization
Chapter 63: Interpreting Query Execution Plans 1313
Viewing Query Execution Plans 1313
Estimated query execution plans
1314
The Query Editor’s execution plan
1314
Returning the plan with showplans
1316
SQL Profiler’s execution plans
1317
Examining plans using dynamic management views
1317
Interpreting the Query Execution Plan
1318
Summary
1320
Chapter 64: Indexing Strategies 1321
Zen and the Art of Indexing 1321
Indexing Basics
1322
The b-tree index
1322
Clustered indexes
1323
Non-clustered indexes
1324
Composite indexes
1324
Unique indexes and constraints
1325
xxviii
www.getcoolebook.com
Nielsen ftoc.tex V4 - 07/21/2009 3:36pm Page xxix
Contents
The page split problem 1325
Index selectivity
1326
Unordered heaps
1326
Query operations
1327
Path of the Query
1327
Query Path 1: Fetch All
1329
Query Path 2: Clustered Index Seek
1329
Query Path 3: Range Seek Query
1332
Query Path 4: Filter by non-key column
1334
Query Path 5: Bookmark Lookup
1335
Query Path 6: Covering Index
1338
Query Path 7: Filter by 2 x NC Indexes
1340
Query Path 8: Filter by Ordered Composite Index
1342
Query Path 9: Filter by Unordered Composite Index
1344
Query Path 10: Non-SARGable Expressions
1344
A Comprehensive Indexing Strategy
1346
Identifying key queries
1346
Table CRUD analysis
1347
Selecting the clustered index
1349
Creating base indexes
1350
Specialty Indexes
1350
Filtered indexes
1351
Indexed views
1352
Summary
1354
Chapter 65: Query Plan Reuse 1357
Query Compiling 1357
The Query Optimizer
1358
Viewing the Plan Cache
1358
Plan lifetime
1359
Query plan execution
1359
Query Recompiles
1360
Summary
1361
Chapter 66: Managing Transactions, Locking, and Blocking 1363
The ACID Properties 1365
Atomicity
1365
Consistency
1365
Isolation
1365
Durability
1365
Programming Transactions
1366
Logical transactions
1366
Xact_State()
1367
Xact_Abort
1368
Nested transactions
1368
Implicit transactions
1369
Save points
1370
Default Locking and Blocking Behavior
1370
Monitoring Locking and Blocking
1373
Viewing blocking with Management Studio reports
1373
Viewing blocking with Activity Monitor
1373
Using Profiler
1373
Querying locks with DMVs
1376
Deadlocks
1377
Creating a deadlock
1378
Automatic deadlock detection
1381
Handling deadlocks
1381
Minimizing deadlocks
1382
Understanding SQL Server Locking
1383
Lock granularity
1383
Lock mode
1384
Controlling lock timeouts
1386
Lock duration
1387
Index-level locking restrictions
1387
Transaction Isolation Levels
1388
Setting the transaction isolation level
1389
Level 1 — Read Uncommitted and the dirty read
1390
xxix
www.getcoolebook.com