How do you create SQL Server 2005 stored procedure templates in SQL Server 2005 Management Studio?

50,600

Solution 1

Another little nugget that I think will help people developing and being more productive in their database development. I am a fan of stored procedures and functions when I develop software solutions. I like my actual CRUD methods to be implemented at the database level. It allows me to balance out my work between the application software (business logic and data access) and the database itself. Not wanting to start a religious war, but I want to allow people to develop stored procedures more quickly and with best practices through templates.

Let’s start with making your own templates in the SQL Server 2005 management Studio. First, you need to show the Template Explorer in the Studio.

alt text http://www.cloudsocket.com/images/image-thumb10.png

This will show the following:

alt text http://www.cloudsocket.com/images/image-thumb11.png

alt text http://www.cloudsocket.com/images/image-thumb12.png

alt text http://www.cloudsocket.com/images/image-thumb13.png

The IDE will create a blank template. To edit the template, right click on the template and select Edit. You will get a blank Query window in the IDE. You can now insert your template implementation. I have here the template of the new stored procedure to include a TRY CATCH. I like to include error handling in my stored procedures. With the new TRY CATCH addition to TSQL in SQL Server 2005, we should try to use this powerful exception handling mechanism through our code including database code. Save the template and you are all ready to use your new template for stored procedure creation.

-- ======================================================
-- Create basic stored procedure template with TRY CATCH
-- ======================================================

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 TRY
        BEGIN TRANSACTION    -- Start the transaction

        SELECT @p1, @p2

        -- If we reach here, success!
        COMMIT
    END TRY
    BEGIN CATCH
        -- there was an error
        IF @@TRANCOUNT > 0
        ROLLBACK

        -- Raise an error with the details of the exception
        DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
        SELECT @ErrMsg = ERROR_MESSAGE(), @ErrSeverity = ERROR_SEVERITY()

        RAISERROR(@ErrMsg, @ErrSeverity, 1)
    END CATCH
GO

Solution 2

You bring up Template Explorer using Ctrl+Alt+T or trough View > Template Explorer. Then you can right click tree nodes to add new Templates or new folders to organize your new templates.

Solution 3

Database=>Table=>Programmability=>Procedures=>Right Clik Select New procedures

Share:
50,600
Chris Woodruff
Author by

Chris Woodruff

Chris Woodruff (or Woody as he is commonly known as) has a degree in Computer Science from Michigan State University’s College of Engineering. Woody has been developing and architecting software solutions for over 20 years and has worked in many different platforms and tools. He is a community leader, helping such events as GRDevNight, GRDevDay, West Michigan Day of .NET and CodeMash. He was also instrumental in bringing the popular Give Camp event to Western Michigan where technology professionals lend their time and development expertise to assist local non-profits. As a speaker and podcaster, Woody has spoken and discussed a variety of topics, including database design and open source. He has been a Microsoft MVP in Visual C#, Data Platform and SQL and was recognized in 2010 as one of the top 20 MVPs world-wide. Woody owns and leads Woodruff Solutions LLC to provide solutions involving mobile apps and the services that drive mobile solutions.

Updated on February 05, 2020

Comments

  • Chris Woodruff
    Chris Woodruff over 4 years

    How do you create SQL Server 2005 stored procedure templates in SQL Server 2005 Management Studio?

  • dykzei eleeot
    dykzei eleeot about 15 years
    is there a way to make that template a default for sproc template?