开发者

tsql transaction

开发者 https://www.devze.com 2023-02-16 18:08 出处:网络
In order to wrap stored procedure in a transaction I add the following: CREATE PROCEDURE [dbo].[P_ORD_InsertTextField]

In order to wrap stored procedure in a transaction I add the following:

CREATE PROCEDURE [dbo].[P_ORD_InsertTextField]
    //PARAMS
AS
BEGIN
    BEGIN TRY
    BEGIN TRANSACTION

    //STP BODY

    COMMIT
    END TRY
    BEGIN CATCH
      IF @@TRANCOUNT > 0
         ROLLBACK

      DECLARE @ErrMsg nvarchar(4000), @ErrSeverity int
      SELECT @ErrMsg = ERROR_MESSAGE(),
             @ErrSeverity = ERROR_SEVERITY()

      RAISERROR(@ErrMsg, @ErrSeverity, 1)
    EN开发者_高级运维D CATCH
END
GO

Is there any shorter way that does the same? this is a huge code block for "just" handle a transaction..


No, this is pretty much it.

You can hide the @ErrMsg processing behind a stored proc or UDF, and you don't need @ErrSeverity processing. It is normally 16 which is "user defined error"

See my answer here too please: Nested stored procedures containing TRY CATCH ROLLBACK pattern?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号