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

后端 未结 3 1996
旧巷少年郎
旧巷少年郎 2021-02-04 17:45

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

3条回答
  •  遇见更好的自我
    2021-02-04 18:15

    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:        
    -- Create date: 
    -- Description:    
    -- =============================================
    CREATE PROCEDURE 
        -- Add the parameters for the stored procedure here
        <@Param1, sysname, @p1>  = ,
        <@Param2, sysname, @p2>  = 
    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
    

提交回复
热议问题