I am working on an Enterprise Database Management course project and cannot seem to get my error handling to perform correctly on a transaction. My instructor advised me to take a look at the error "Msg 266, Level 16, State 2, Procedure pInsertVolunteer, Line 28 Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. " but I have not yet found any solutions that are working for this transaction.
CREATE PROCEDURE [dbo].[pInsertVolunteer]
@LastName varchar(255),
@FirstName varchar(255),
@DateOfBirth date,
@Note varchar(MAX),
@ModifiedBy nvarchar(50),
@Success bit Output,
@ErrorStatus Nvarchar(50) Output
AS
SET NOCOUNT ON;
SET XACT_ABORT ON;
BEGIN
DECLARE
@TempVolunteerID int,
@VolunteerID int,
@VolunteerPersonID int;
SET @TempVolunteerID = 0;
SET @Success = 0;
SET @ErrorStatus = '';
END
BEGIN TRY
BEGIN TRANSACTION;
SELECT @TempVolunteerID = VolunteerID
FROM Volunteer.VolunteerInfo
WHERE FirstName = @FirstName AND LastName = @LastName AND DateOfBirth = @DateOfBirth
If @TempVolunteerID <>0
BEGIN
SELECT @ErrorStatus = 'Person Already Exists';
SET @Success = 0;
RAISERROR (@ErrorStatus, 16,1);
END
IF @@ERROR <>0
BEGIN
SELECT @ErrorStatus = CONVERT (nvarchar(50),@@ERROR) + '-1000';
SET @Success = 0;
RAISERROR(@ErrorStatus, 16,1);
END
INSERT INTO Volunteer.VolunteerInfo (LastName, FirstName, DateOfBirth)
values (@LastName, @FirstName, @DateofBirth)
SET @Success = 1;
SET @ErrorStatus = 0;
SELECT @VolunteerPersonID = VolunteerID
FROM Volunteer.VolunteerInfo
If @VolunteerPersonID = 0 OR @VolunteerPersonID IS NULL
BEGIN
SET @Success =0;
SET @ErrorStatus = '8008: PersonID must be valid';
RAISERROR(@ErrorStatus,16,1);
END
Insert into Volunteer.VolunteerNotes (Note,ModifiedDate, ModifiedBy)
Values (@Note,SYSDATETIME(), @ModifiedBy)
SET @VolunteerPersonID = @@IDENTITY;
SET @Success = 1;
SET @ErrorStatus = 0;
SET NOCOUNT OFF;
COMMIT TRANSACTION
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
SELECT @ErrorStatus = @ErrorStatus + CONVERT (nvarchar(50), @@ERROR);
END
SET @Success = 0;
SET @ErrorStatus = @ErrorStatus + '-1001';
RAISERROR(@ErrorStatus, 15,1)
ROLLBACK TRANSACTION;
END CATCH
GO
Any help would be greatly appreciated.
Thank you!
IF @@ERROR <>0after the first SELECT statement. That is logical nonsense and will never be reached. Any exception will be caught in a catch block (if it exists). No idea why you need to catch errors only to re-raise them and set an output parameter - but maybe you have very atypical requirements. Generally you shouldn't be trying to undermine the exception logic by using your own. - SMorTHROWtoo, as the documentation onRAISERRORtells you. - Larnu