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

后端 未结 3 1987
旧巷少年郎
旧巷少年郎 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:        <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
    
    0 讨论(0)
  • 2021-02-04 18:31

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

    0 讨论(0)
  • 2021-02-04 18:42

    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.

    0 讨论(0)
提交回复
热议问题