Can anybody please tell me what the issue is with my code? I have tried for a long time but the code is just not working.
The Select
statement is not getting executed and throws an error:
Msg 266, Level 16, State 2, Procedure HW5INS2, Line 14
Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.
What to do?
ALTER PROCEDURE HW5INS2
-----LEVEL-2:ShipmentBoxes
@boxID VARCHAR(16),
@boxWeight NUMERIC(6,2),
@boxHeight NUMERIC(5,2),
@boxWidth NUMERIC(5,2),
@boxLength NUMERIC(5,2),
@packager SMALLINT,
@shipDate SMALLDATETIME ='2018-03-02' ----NOT REQUIRED
AS
BEGIN TRANSACTION
---------Test For Existence Of Parent Row: LOOK INTO Employees TABLE
IF EXISTS (SELECT employeeID FROM Employees WHERE employeeID = @packager)
BEGIN
BEGIN TRANSACTION
BEGIN TRY
INSERT INTO ShipmentBoxes (boxID, boxWeight, boxHeight, boxWidth, boxLength, packager, shipDate)
VALUES (@boxID, @boxWeight, @boxHeight, @boxWidth, @boxLength, @packager, @shipDate)
SELECT * FROM ShipmentBoxes
COMMIT
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION
PRINT 'Error occurred while trying to insert the ShipmentBoxes table'
SELECT
ERROR_NUMBER() as errornumber,
XACT_state() as xstate
END CATCH
END
ELSE
BEGIN
-----PARENT DOESNOT EXIST, RETURN ERROR
ROLLBACK TRANSACTION
PRINT 'Error: PARENT does not exist'
END
RETURN