Microsoft SQL Server 2005: The Complete Reference: Full Coverage of all New and Improved Features

Overview

Stored procedures and functions are the most important objects of an RDBMS, outside of the engines and the database tables. The stored procedures and functions of an RDBMS, and how a product like SQL Server 2005 supports them, are what make the product. How procedures and functions are stored, compiled, optimized, and executed by a DBMS is what sets a DBMS apart from its competitors. They help us meet the rules of deployment we discussed in Chapter 13.

This chapter covers both legacy stored procedures and functions written in T-SQL as well as the procs and functions you can now install as part of the .NET Framework’s common language runtime (CLR) support (see Chapter 11). We have not cover trigger creation using the .NET Framework because the process for writing the code, compiling and installing the assembly and installing the trigger to SQL Server is identical for all “objects” CLR.

For the most part we will be discussing stored procedures because you will be creating and using them more. If you are unfamiliar with the concept of a stored procedure, you will find that the following list sheds some light on these critical SQL Server features:

Database developers need intimate knowledge of the workings of stored procedures. For all intents and purposes, they are to the DBMS and its databases what classes are to languages like C# and Java. Stored procedures are not inherited, derived, or cloned, nor do they sport inherited properties, methods, and the like, but they share many other valuable attributes of object-based programming such as code isolation, reuse, and sharing (by both developers and users). You cannot build any form of effective application that relies on SQL Server, nor can you be an effective DBA, without having an intimate knowledge of how to code and manage stored procedures.

Types of Stored Procedures

The several types of stored procedure supported by SQL Server are as follows:

How Stored Procedures Are Processed by SQL Server

Stored procedures are processed in two stages. In the first stage the procedure is first parsed by the SQL Server database engine (see Chapter 2) upon creation, after which two things happen. SQL Server stores the definition of the procedure, name and code, in the catalog. It also pushes the code through the Query Optimizer, as discussed in Chapter 4, and determines the best execution plan for the code.

Next the code is compiled and placed in the procedure cache. The only time the plan is flushed from the cache is when an explicit recompile is called by the client connection or the plan no longer exists in the cache, which means it had aged and had to be expelled. The cache can also be flushed via the DBCC freeproccache command discussed in Chapter 10.

In the second stage the query plan is retrieved when the stored procedure’s name is referenced in code. The procedure code is then executed in the context of each connection that called the procedure. Any result sets or return values are returned to each connection.

Категории