A stored procedure is a block
of T-SQL or .NET code that is stored within a database. This code is
similar to other procedures in other languages because it accepts
parameters as inputs and can provide output to the calling application
or user. Stored procedures make building database applications easy.
Stored procedures contain a header and a body.
In the header, you define the input and output parameters. In the body
is the code. For example, the following CREATE PROCEDURE
statement creates a procedure that will take a pet name and return the microchip ID for that pet:
CREATE PROCEDURE GetID
@name VARCHAR(50)
AS
BEGIN
SELECT MicroChipID FROM Pets WHERE pet_name=@name
END
Parameters are optional. In this example, you
need to specify a name. To execute this procedure, you can issue the
following statement:
EXEC GetID 'Roxy'
The return the value is 1A8AF59
, which is the value of Roxy’s microchip.
Creating Stored Procedures Using Templates
SSMS provides a template to use when creating a
new stored procedure. To view this template, navigate to the Stored
Procedures node within the Programmability node of the VetClinic
database. Select New Stored Procedure from the context menu. This will
open a Query Editor window with the following template:
-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters
-- command (Ctrl-Shift-M) to fill in the parameter
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName>
-- Add the parameters for the stored procedure here
<@Param1, sysname, @p1> <Datatype_For_Param1, , int> =
<Default_Value_For_Param1, , 0>,
<@Param2, sysname, @p2> <Datatype_For_Param2, , int> =
<Default_Value_For_Param2, , 0>
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO
Having the syntax already present is helpful,
but SSMS makes working with templates easy by giving you a dialog box
to use to fill in the values. To launch the Specify Values for Template
Parameters dialog box, select this option from the Query menu in SSMS. Figure 1 shows this dialog box.
Figure 1. Input dialog box for templates
Once you fill the values in the dialog box and click OK, the template automatically updates the CREATE STORED PROCEDURE
text in the Query Editor with the data you provided in the dialog box.
Templates make it very easy to create
procedures. Many templates are available for SQL Server. To access the
list of templates, select Template Explorer from the View menu in SSMS.
This will launch a Template Browser window, shown in Figure 2.
Figure 2. Template Browser window in SSMS
Note that there are two
buttons on the top of this dialog box; the first one shows SQL Server
templates, and the second shows Analysis Services templates. To use a
template, simply double-click it, and the template will load in a new
Query Editor window.
Modifying Stored Procedures
SSMS does a great job of enumerating all the
stored procedures within the database. They are all listed under the
Programmability node, which is under a specific database. To modify a
stored procedure, you need to issue an ALTER STORED PROCEDURE
statement. To modify the GetID
procedure created earlier using SSMS, select Modify from the context menu of the GetID
stored procedure. This will launch a Query Editor window with the ALTER STORED PROCEDURE
text in it. The actual script generated by SSMS is as follows:
USE [VetClinic]
GO
/** Object: StoredProcedure [dbo].[GetID] Script Date: 04/29/2009 **/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[GetID]
@name VARCHAR(50)
AS
BEGIN
SELECT MicroChipID FROM Pets WHERE pet_name=@name
END
SSMS saves you from a lot of typing when you are working with stored procedures.
System Stored Procedures
If you navigate around SSMS and the Programmability node as described earlier, you may notice a System Stored Procedures
folder. There are quite a number of these, and each serves its own
purpose within SQL Server. For example, the SQL Server Agent job
scheduler does not have native DDL statements for creating jobs, so it
uses system stored procedures to create and manage jobs.
Note System stored procedures usually start with the characters sp_
. Thus, it is a good idea to not start your stored procedures with the same three characters.