In Jump Start, "Stored Procedures", I explained the basics of creating and executing stored procedures. Another way to easily create stored procedures--especially for those who aren't very familiar with them and with T-SQL--is by using the Template Explorer feature in SQL Server Management Studio Express (SSMSE).

To use Template Explorer, open SSMSE and select the View, Template Explorer option. In the Template Explorer window, expand the Stored Procedures node, then double-click Create Procedure Basic Template. SSMSE will generate the following CREATE PROCEDURE T-SQL code:

-- ========================================<h4><a name="_Create_a_basic_stored_procedure_template_">
-- Create a basic stored procedure template.
-- </a></h4>===================================<h4><a name="_Drop_stored_procedure_if_it_already_exists_IF_EXISTS_SELECT_FROM_INFORMATION_SCHEMA_ROUTINES_WHERE_SPECIFIC_SCHEMA_N_#8249_Schema_Name_sysname_Schema_Name_#8250_AND_SPECIFIC_NAME_N_#8249_Procedure_Name_sysname_Procedure_Name_#8250_DROP_PROCEDURE_#8249_Schema_Name_sysname_Schema_Name_#8250_#8249_Procedure_Name_sysname_Procedure_Name_#8250_GO_CREATE_PROCEDURE_#8249_Schema_Name_sysname_Schema_Name_#8250_#8249_Procedure_Name_sysname_Procedure_Name_#8250_#8249_param1_sysname_p1_#8250_#8249_datatype_for_param1_int_#8250_#8249_default_value_for_param1_0_#8250_#8249_param2_sysname_p2_#8250_#8249_datatype_for_param2_int_#8250_#8249_default_value_for_param2_0_#8250_AS_SELECT_p1_p2_GO_">

-- Drop stored procedure if it already exists.
IF EXISTS (
  SELECT *
    FROM INFORMATION_SCHEMA.ROUTINES
   WHERE SPECIFIC_SCHEMA = N'‹Schema_Name, sysname, Schema_Name›'
     AND SPECIFIC_NAME = N'‹Procedure_Name, sysname, Procedure_Name›'
)
   DROP PROCEDURE ‹Schema_Name, sysname, Schema_Name›.‹Procedure_Name, sysname, Procedure_Name›
GO

CREATE PROCEDURE ‹Schema_Name, sysname, Schema_Name›.‹Procedure_Name, sysname, Procedure_Name›
        ‹@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
        SELECT @p1, @p2
GO

-- </a></h4>===================================<h4><a name="_Execute_the_stored_procedure_">
-- Execute the stored procedure.
-- </a></h4>========================================
EXECUTE ‹Schema_Name, sysname, Schema_Name›.‹Procedure_Name, sysname, Procedure_Name› ‹value_for_param1, , 1›, ‹value_for_param2, , 2›
GO
To use the generated stored procedure template, the first thing you need to do is replace the placeholder codes that are in angle brackets with the names you want to use. The first placeholder code,

<p>‹Schema_Name, sysname, Schema_Name›</p>

is replaced in its entirety with the name of the schema you want to use. If you don't specify a schema, the dbo schema will be used by default. The second placeholder code,

<p>‹Procedure_Name, sysname, Procedure_Name›</p>

is replaced in its entirety with the name of your stored procedure--for example, usp_MyStoredProc.

You use the lines beneath the CREATE PROCEDURE statement for optional parameters, as I'll explain in a later column. For a simple stored procedure that doesn't use parameters, you can safely delete those lines.

Finally, you replace the line

<p>SELECT @p1, @p2</p> with the custom code that your stored procedure will use. You can then use Query Editor to execute the stored procedure.

I'll discuss more details of this template in my next Jump Start column.