I need to add error handling to my stored procedure. I believe it is not usually required to use BEGIN TRAN/COMMIT TRAN when there is just one insert statement. Also what is the significance of using SET XACT_ABORT, NOCOUNT ON statement. Please sugget best/standard way to add the error handling to below SP. I also need to call dbo.usp_get_error_info in the catch segment if it errors . Please suggest.
USE [TEST]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[UspSdtSync]
AS
BEGIN
DECLARE @return_value INT
,@RetCode INT
,@RunID INT
,@IntraDayID INT
SET @RunID = NULL
SET @IntraDayID = NULL
EXEC @return_value = [DST].[SD].[STG].[API_GenerateTempView]
@SchemaName = N'TEST_A',
@ViewName = N'vw_TEST_KB_CGSE',
@ColumnList = N'statusE, statusF, statusG, statusH, LastModifiedDate, LastModifiedBy, LastReviewedBy, statusI, statusJ, Email, Mobile, HomePhone, WorkPhone, statusK, statusL, Dob',
@OrderByList = NULL,
@ResultSet = 1,
@RunID = @RunID,
@IntraDayID = @IntraDayID,
@RetCode = @RetCode OUTPUT
MERGE INTO AeoiSdtTemp AS t
USING (SELECT statusE, statusF, statusG, statusH, LastModifiedDate, LastModifiedBy, LastReviewedBy, statusI, statusJ, Email, Mobile, HomePhone, WorkPhone, statusK, statusL, Dob
FROM [DST].[SD].[TEST_KB_KTA].[vw_SDT_TEST_KB_CGSE_Temp]) AS s ON ( t.statusE = s.statusE) AND (t.statusF = s.statusF) AND (t.statusG = s.statusG) AND (t.statusH = s.statusH)
/*** Insert records directly into local KTA table ***/
WHEN NOT MATCHED THEN
INSERT (statusE, statusF, statusG, statusH, LastModifiedDate, StatusCode, LastModifiedBy, LastReviewedBy, CreatedDate, statusI, statusJ, Email, Mobile, HomePhone, WorkPhone, statusK, statusL, Dob)
VALUES(s.statusE, s.statusF, s.statusG, s.statusH, s.LastModifiedDate, '11', s.LastModifiedBy, s.LastReviewedBy, GETDATE(), s.statusI, s.statusJ, s.Email, s.Mobile, s.HomePhone, s.WorkPhone, s.statusK, s.statusL, s.Dob)
/*** Update records that exist ***/
WHEN MATCHED THEN
UPDATE SET LastModifiedDate = s.LastModifiedDate, LastModifiedBy = s.LastModifiedBy, LastReviewedBy = s.LastReviewedBy, statusI = s.statusI, statusJ = s.statusJ, Email = s.Email, Mobile = s.Mobile, HomePhone = s.HomePhone, WorkPhone = s.WorkPhone, statusK = s.statusK, statusL = s.statusL, Dob = s.Dob;
END
GO