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?
精彩评论