当前位置:首页> 正文

如何在SQL Server 2005 Management Studio中创建SQL Server 2005存储过程模板?

如何在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方法。它使我能够平衡应用程序软件(业务逻辑和数据访问)与数据库本身之间的工作。不想发动一场宗教战争,但我想允许人们通过模板以最佳实践更快地开发存储过程。

Leta首先从在SQL Server 2005 Management Studio中创建自己的模板开始。首先,您需要在Studio中显示"模板资源管理器"。

替代文字/d/jc/2023041511/vlbspgptdns23.webp

这将显示以下内容:

替代文字/d/jc/2023041511/vwnpcn2gnmq23.webp

替代文字/d/jc/2023041511/lm5kls0qdew24.webp

替代文字/d/jc/2023041511/ckpeszykkb524.webp

IDE将创建一个空白模板。要编辑模板,请右键单击模板,然后选择编辑。您将在IDE中获得一个空白的"查询"窗口。现在,您可以插入模板实现。我这里有新存储过程的模板,其中包括TRY CATCH。我喜欢在存储过程中包括错误处理。通过在SQL Server 2005中为TSQL添加新的TRY CATCH,我们应该尝试通过包括数据库代码在内的代码来使用这种强大的异常处理机制。保存模板,您就可以准备使用新模板来创建存储过程了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
-- ======================================================
-- 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或低谷视图>模板资源管理器打开模板资源管理器。然后,您可以右键单击树节点以添加新模板或新文件夹以组织新模板。


数据库=>表=>可编程性=>过程=>右键单击选择新过程


展开全文阅读

相关内容