簡體   English   中英

如何在SQL Server 2005 Management Studio中創建SQL Server 2005存儲過程模板?

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

如何在SQL Server 2005 Management Studio中創建SQL Server 2005存儲過程模板?

我認為另一個小塊子將幫助人們在數據庫開發中發展並提高工作效率。 在開發軟件解決方案時,我是存儲過程和函數的粉絲。 我喜歡在數據庫級實現的實際CRUD方法。 它允許我平衡應用軟件(業務邏輯和數據訪問)與數據庫本身之間的工作。 不想發起宗教戰爭,但我希望允許人們通過模板更快地開發存儲過程並使用最佳實踐。

讓我們從在SQL Server 2005管理工作室中創建自己的模板開始。 首先,您需要在Studio中顯示模板資源管理器。

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

這將顯示以下內容:

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

IDE將創建一個空白模板。 要編輯模板,請右鍵單擊模板,然后選擇“編輯”。 您將在IDE中獲得一個空白的查詢窗口。 您現在可以插入模板實現。 我在這里有新存儲過程的模板,包括一個TRY CATCH。 我喜歡在我的存儲過程中包含錯誤處理。 隨着SQL Server 2005中對TSQL的新TRY CATCH的增加,我們應該嘗試通過包括數據庫代碼在內的代碼使用這種強大的異常處理機制。 保存模板,您就可以使用新模板來創建存儲過程了。

-- ======================================================
-- 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

你帶了模板資源管理器使用Ctrl + Alt + T或槽查看>模板資源管理器 然后,您可以右鍵單擊樹節點以添加新模板或新文件夾以組織新模板。

Database => Table => Programmability => Procedures => Right Clik選擇New procedures

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM