Cover image for Microsoft SQL Server 2005 stored procedure programming in T-SQL & .NET
Title:
Microsoft SQL Server 2005 stored procedure programming in T-SQL & .NET
Personal Author:
Edition:
3rd ed.
Publication Information:
San Francisco, CA : McGraw-Hill, 2006
ISBN:
9780072262285

Available:*

Library
Item Barcode
Call Number
Material Type
Item Category 1
Status
Searching...
30000010159150 QA76.9.C55 S964 2006 Open Access Book Book
Searching...

On Order

Summary

Summary

Publisher's Note: Products purchased from Third Party sellers are not guaranteed by the publisher for quality, authenticity, or access to any online entitlements included with the product.


Create and Use Stored Procedures for Optimal Database Performance

Develop complex stored procedures to retrieve, manipulate, update, and delete data. Microsoft SQL Server 2005 Stored Procedure Programming in T-SQL & .NET identifies and describes the key concepts, techniques, and best practices you need to master in order to take full advantage of stored procedures using SQL Server's native Transact-SQL and .NET CLR languages. You'll learn to incorporate effective Transact-SQL stored procedures into client or middleware code, and produce CLR methods that will be compiled into CLR stored procedures. This is a must-have resource for all SQL Server 2005 developers.

Essential Skills for Database Professionals

Group and execute T-SQL statements using batches, scripts, and transactions Create user-defined, system, extended, temporary, global temporary, and remote stored procedures Develop and manage stored procedures using C# and Visual Basic .NET Implement database access using ADO.NET Create CLR user-defined functions and triggers Implement reliable debugging and error handling techniques and security measures Manage source code in a repository such as Visual SourceSafe Create stored procedures for web search engines Use system and extended stored procedures to interact with the SQL Server environment


Author Notes

Dejan Sunderić, MCDBA, is the principal consultant at Trigon Blue, Inc., and the president of the Toronto SQL Server User Group. He specializes in database and application development on the SQL Server platform.


Table of Contents

Acknowledgmentsp. xix
Introductionp. xxi
Chapter 1 The SQL Server 2005 Environment and Toolsp. 1
SQL Server 2005 Toolsp. 2
SQL Server Configuration Managerp. 3
SQL Server Management Studiop. 6
SQLCMD Utilityp. 11
SQL Server Profilerp. 12
The Help Subsystem and SQL Server Books OnLinep. 12
Basic Operations in Management Studiop. 13
What Are Stored Procedures?p. 14
Execution of Stored Procedures in Management Studiop. 14
Editing Stored Proceduresp. 17
Creating Stored Proceduresp. 18
Editing Stored Procedures in the Query Windowp. 19
Syntax Errorsp. 22
Viewing and Editing Tablesp. 23
Changing Table Structurep. 26
Chapter 2 Stored Procedure Design Conceptsp. 27
Anatomy of a Stored Procedurep. 28
Compositionp. 28
Functionalityp. 31
Syntaxp. 38
Types of Stored Proceduresp. 40
Managing Stored Proceduresp. 41
Listing Stored Proceduresp. 41
Viewing Code of Stored Proceduresp. 43
Renaming Stored Proceduresp. 44
Deleting Stored Proceduresp. 45
Listing Dependent and Depending Objectsp. 45
The Role of Stored Procedures in the Development of Database Applicationsp. 47
Enforcement of Data Integrityp. 47
Consistent Implementation of Complex Business Rules and Constraintsp. 48
Modular Designp. 48
Maintainabilityp. 48
Reduced Network Trafficp. 49
Faster Executionp. 49
Enforcement of Securityp. 49
Chapter 3 Basic Transact-SQL Programming Constructsp. 51
T-SQL Identifiersp. 52
Database Object Qualifiersp. 54
Data Typesp. 55
Character Stringsp. 57
Unicode Character Stringsp. 58
Date and Time Data Typesp. 59
Integer Numbersp. 60
Approximate Numbersp. 61
Exact Numbersp. 62
Monetary Data Typesp. 62
Binary Data Typesp. 63
Special Data Typesp. 64
Transact-SQL User-defined Data Typesp. 67
Variablesp. 68
Local Variablesp. 69
Global Variablesp. 73
Table Variablesp. 76
Flow-control Statementsp. 77
Commentsp. 78
Statement Blocks: Begin...Endp. 80
Conditional Execution: The If Statementp. 81
Looping: The While Statementp. 86
Unconditional Execution: The GoTo Statementp. 89
Scheduled Execution: The WaitFor Statementp. 90
Cursorsp. 91
Transact-SQL Cursorsp. 92
Cursor-related Statements and Functionsp. 95
Problems with Cursorsp. 97
The Justified Uses of Cursorsp. 97
Chapter 4 Functionsp. 101
Using Functionsp. 102
In Selection and Assignmentp. 102
As Part of the Selection Criteriap. 103
In Expressionsp. 103
As Check and Default Constraintsp. 104
Instead of Tablesp. 104
Types of Functionsp. 105
Scalar Functionsp. 106
Rowset Functionsp. 136
Chapter 5 Composite Transact-SQL Constructs: Batches, Scripts, and Transactionsp. 143
Batchesp. 144
Using Batchesp. 145
Batches and Errorsp. 146
DDL Batchesp. 150
Self-sufficient Contentp. 151
Scriptsp. 152
Database Scriptingp. 152
Trasactionsp. 155
Autocommit Transactionsp. 155
Explicit Transactionsp. 157
Implicit Transactionsp. 158
Transaction Processing Architecturep. 159
Nested Transactionsp. 160
Named Transactionsp. 165
Savepointsp. 165
Lockingp. 169
Distributed Transactionsp. 173
Typical Transaction-related Problemsp. 175
Chapter 6 Error Handlingp. 179
Raiserror()p. 180
Using Error Handlingp. 183
Why Bother?p. 183
Error Handling Based on @@Errorp. 184
Error Handling Architecture: Based on @@Errorp. 186
Try - Catch Statementp. 186
What Errors Are Trappedp. 187
Functions of the Catch Blockp. 190
Try - Catch Statement with Explicit Transactionsp. 191
Deadlocksp. 194
Deadlock Retriesp. 194
Try - Catch Statement Nestingp. 198
Error Handling Architecture: With Try - Catch Statementsp. 199
Xact_Abortp. 199
Error Handling Architecture: Based on Set Xact_Abort Onp. 201
Error Handling Architecture: Xact_Abort + No Transaction Nestingp. 205
Chapter 7 Special Types of Stored Proceduresp. 213
User-defined Stored Proceduresp. 214
System Stored Proceduresp. 214
CLR Stored Proceduresp. 216
Extended Stored Proceduresp. 216
Design of Extended Stored Proceduresp. 217
Registering the Extended Stored Procedurep. 222
Temporary Stored Proceduresp. 223
Global Temporary Stored Proceduresp. 224
Remote Stored Proceduresp. 225
Chapter 8 Viewsp. 227
Design of Standard SQL Viewsp. 228
Syntaxp. 229
Design View in Enterprise Managerp. 230
Securityp. 230
Standard SQL Views in Execution Plansp. 231
Limitations of Viewsp. 232
Editing Data Using Viewsp. 233
Dynamic Viewsp. 234
Temporary Views-Common Table Expressionsp. 234
Limitations of Nonrecursive CTEsp. 235
Recursive CTEsp. 236
Information_Schema Viewsp. 237
Indexed Viewsp. 239
Indexed View Limitationsp. 239
Indexed Views in Execution Plansp. 240
Nonclustered Indexes on Viewsp. 241
Performance Implicationsp. 241
Partitioned Viewsp. 241
Horizontal and Vertical Partitioningp. 241
Distributed Partitioned Viewsp. 244
Execution Plans of Distributed Partitioned Viewsp. 248
Updateable Distributed Partitioned Viewsp. 253
Scalability and Performance of Distributed Systemsp. 255
A Poor Man's Federated Serverp. 255
Using SQL Viewsp. 256
Export and Importp. 256
Security Implementationp. 256
Reduce Complexityp. 257
Performance Improvementp. 259
Chapter 9 Triggersp. 261
DML Triggersp. 262
Physical Design of After Triggersp. 262
Inserted and Deleted Virtual Tablesp. 264
What Triggers a Trigger?p. 266
Full Syntax of After Triggersp. 267
Handling Changes on Multiple Recordsp. 270
Nested and Recursive Triggersp. 272
After Trigger Restrictionsp. 272
Instead-of Triggersp. 274
Triggers on Viewsp. 275
DML Trigger Order of Executionp. 278
DDL Triggersp. 279
Scope and Events of DDL Triggersp. 280
EventData()p. 281
Syntax of DDL Triggersp. 283
Managing Triggersp. 283
Managing DML Triggers in Management Studiop. 283
Managing DDL Triggers from Management Studiop. 284
Managing Triggers Using Transact-SQL Statementsp. 284
Trigger Design Recommendationsp. 289
Go Out ASAPp. 289
Make It Simplep. 290
Divide and Conquerp. 290
Do Not Use Select and Print Inside a Triggerp. 290
Do Not Use Triggers at Allp. 291
Transaction Management in Triggersp. 291
Using Triggersp. 292
Cascading Deletesp. 294
Aggregatesp. 296
Enforce Schema Integrity Among Objects on Different Servers or Databasesp. 299
Chapter 10 User-defined Functionsp. 303
Design of Scalar User-defined Functionsp. 304
Side Effectsp. 306
Use of Built-in Functionsp. 306
Encryptionp. 307
Schema-bindingp. 307
Table-valued User-defined Functionsp. 308
Inline Table-valued User-defined Functionsp. 311
Managing User-defined Functions in Management Studiop. 313
Chapter 11 Fundamentals of .NET Programming in SQL Server 2005p. 315
Unmanaged vs. Managed Codep. 316
Common Language Runtime (CLR)p. 316
CLR Integration in SQL Server 2005p. 318
Pros and Consp. 318
Supported and Not Supportedp. 319
Minimally Required Namespacesp. 320
Development, Deployment, and Usage of CLR Database Objects in Visual Studio 2005p. 320
Development and Deployment of CLR Database Objects Without Visual Studio 2005p. 324
Develop Codep. 325
Compile Programp. 326
Load Assembly to SQL Server Databasep. 328
Create SQLCLR Database Objectsp. 329
Cataloging Assemblies and CLR Database Objectsp. 330
Cataloging Assembliesp. 330
Cataloging Proceduresp. 332
Version of .NET Frameworkp. 332
Chapter 12 Fundamentals of CLR Stored Procedure Developmentp. 335
Structure of a CLR Stored Procedurep. 336
Database Access from CLR Proceduresp. 337
CLR Data Types Mappingp. 338
Basic Operations with Stored Proceduresp. 339
Returning Valuep. 339
Connection Contextp. 340
Returning a Resultp. 341
Returning Custom Recordsp. 343
Parameters in CLR Stored Proceduresp. 346
Operations with Regular ADO.NET Objectsp. 346
SqlCommandp. 346
SqlDataReaderp. 348
Managing LOB Parametersp. 349
Chapter 13 CLR Functions and Triggersp. 355
CLR Functionsp. 356
Scalar-valued CLR Functionsp. 356
SqlFunction Attributep. 359
Table-valued CLR Functionsp. 360
CLR Triggersp. 372
Supported Featuresp. 373
Creation of DML Triggersp. 373
Creation of DDL Triggersp. 379
Chapter 14 CLR Database Objects Advanced Topicsp. 385
CLR User-defined Typesp. 386
Structure of Managed UDTp. 386
Deploying CLR UDT Without Visual Studio 2005p. 395
Using CLR User-defined Typesp. 395
Cross-database CLR User-defined Typesp. 403
To UDT or Not to UDTp. 403
User-defined Aggregate CLR Functionsp. 404
Structure of Managed Aggregatesp. 404
Transactions in CLR Codep. 416
Using TransactionScope Classp. 417
Transaction Optionsp. 419
Distributed Transactionsp. 420
Explicit Transactionsp. 423
Benefits of New Transaction Paradigmp. 424
To CLR or Not to CLR (Design and Performance Considerations)p. 425
Don't Say I Didn't Warn Youp. 426
Chapter 15 Advanced Stored Procedure Programmingp. 427
Dynamically Constructed Queriesp. 428
Executing a Stringp. 428
Query by Formp. 430
Data Script Generatorp. 432
Using the sp_executesql Stored Procedurep. 437
Security Implicationsp. 439
Optimistic Locking Using timestamp Valuesp. 443
timestampp. 444
TSEqual() Functionp. 446
Full-text Search and Indexesp. 446
Nested Stored Proceduresp. 448
Using Temporary Tables to Pass a Recordset to a Nested Stored Procedurep. 449
Using a Cursor to Pass a Recordset to a Nested Stored Procedurep. 451
How to Process the Result Set of a Stored Procedurep. 453
Using Identity Valuesp. 459
A Standard Problem and Solutionp. 459
Identity Values and Triggersp. 460
GUIDsp. 462
A While Loop with Min() or Max() Functionsp. 464
Looping with sp_MSForEachTable and sp_MSForEachDbp. 466
Property Managementp. 467
Chapter 16 Debuggingp. 471
What Is a "Bug"?p. 472
The Debugging Processp. 472
Identificationp. 473
Resolutionp. 474
Debugging Tools and Techniquesp. 475
Transact-SQL Debugger in Visual Studio 2005p. 476
Debugging CLR Database Objectsp. 480
Poor Man's Debuggerp. 484
Execution in the Production Environmentp. 488
Nested Stored Proceduresp. 488
Output Clausep. 488
SQL Profilerp. 490
Using SQL Profilerp. 490
Typical Errorsp. 494
Handling Nullp. 495
Assignment of Variable from the Result Setp. 495
No Records Affectedp. 496
Wrong Size or Data Typep. 496
Default Lengthp. 496
Rollback of Triggersp. 497
Warnings and Lower-priority Errorsp. 497
Return Codes vs. Raiserrorp. 498
Nested Commentsp. 498
Deferred Name Resolutionp. 498
Cursorsp. 499
Overconfidencep. 499
Chapter 17 Source Code Managementp. 501
Introduction to Microsoft Visual SourceSafep. 502
Administering the Visual SourceSafe Databasep. 504
Adding Database Objects to Visual SourceSafe in Visual Studio .NETp. 505
Managing Create Scripts in Visual Studio .NETp. 507
Visual SourceSafe Explorerp. 509
Historyp. 510
Labels and Versionsp. 511
Adding Database Objects to Visual SourceSafe: Traditional Approachp. 513
Creating Scripts Using Script Wizardp. 513
Creating Scripts Using TbDbScriptp. 516
Putting Scripts to Visual SourceSafe Using TbDir2Vss.vbsp. 517
Chapter 18 Database Deploymentp. 519
Deployment of a Complete Database: Traditional Approachp. 521
Detach and Reattach the Database in Transact-SQLp. 521
Attach and Detach in Management Studiop. 522
Backup and Restorep. 523
Potential Problemsp. 523
Deployment of Individual Objectsp. 525
Deployment Scripts: Traditional Approachp. 525
Scripting Data: Traditional Approachp. 525
Scripting Data in Visual Studio .NETp. 526
Deploying Scripts: Traditional Approachp. 527
Deploying Create Scripts in Visual Studio .NETp. 535
Incremental Build: Traditional Approachp. 537
Incremental Build in Visual Studio .NETp. 538
Chapter 19 Securityp. 539
Security Architecturep. 540
Authentication and Authorizationp. 540
Principalsp. 540
Securablesp. 541
Access Levelsp. 542
Rolesp. 546
Ownership Chainsp. 549
Cross-database Ownership Chainsp. 549
Switching of Execution Contextp. 550
Implementing Securityp. 551
Selection of Authentication Modep. 551
Managing Loginsp. 552
Granting Database Accessp. 554
Assigning Permissionsp. 556
Synchronization of Login and Usernamesp. 559
Managing Application Security Using Stored Procedures, User-defined Functions, and Viewsp. 562
Managing Application Security Using a Proxy Userp. 565
Managing Application Security Using Application Rolesp. 565
Chapter 20 Stored Procedures for Web Search Enginesp. 567
Characteristics of the Environmentp. 568
A Simple Solution...p. 568
... and Its Disadvantagesp. 570
Available Solutionsp. 571
Result Splittingp. 572
Quick Queriesp. 581
Advanced Queriesp. 585
Fancy New Solution: Row Versioningp. 589
Chapter 21 Interaction with the SQL Server Environmentp. 597
Running Programsp. 598
Running Windows Script Filesp. 599
Execution of OLE Automation/COM Objectsp. 600
Data Type Conversionp. 603
Running SQL Server 2005 Integration Services (SSIS) Packagesp. 604
Running DTS Packagesp. 604
Running/Looping Through DTS Packagesp. 604
Interacting with the NT Registryp. 606
xp_regreadp. 607
xp_regwritep. 608
Jobsp. 608
Administration of Jobsp. 608
An Alternative to Job Schedulerp. 614
Stored Procedures for Maintaining Jobsp. 614
Operators and Alertsp. 617
E-mailp. 618
Database Mailp. 619
Configuring Database Mailp. 619
Sending E-mailsp. 621
Check Statusp. 622
Exposing Stored Procedures As Web Servicesp. 622
Endpointsp. 623
Appendix A Naming Conventionsp. 625
Why Bother?p. 626
Naming Objects and Variablesp. 627
Entity Descriptionp. 627
Name Lengthp. 628
Abbreviationsp. 629
Name Formattingp. 630
Suggested Conventionp. 630
Variablesp. 630
Database Objectsp. 632
Triggersp. 633
Stored Proceduresp. 634
Appendix B Stored Procedure Compilation, Storage, and Reusep. 637
The Compilation and Execution Processp. 638
Parsingp. 638
Get Statisticsp. 638
Compilationp. 639
Executionp. 639
Reuse of Execution Plansp. 639
Levels of Execution Plansp. 640
Caching Levelsp. 640
Reuse of Query Execution Plansp. 641
Parameterized Queriesp. 641
Autoparameterizationp. 641
Reuse of Stored Procedure Execution Plansp. 644
Recompiling Stored Proceduresp. 646
Storing Stored Proceduresp. 648
Appendix C Data Types in SQL Server 2005p. 651
Indexp. 655