SET XACT_ABORT ON
BEGIN TRY
BEGIN TRAN
INSERT INTO dbo.Student
(FirstName, LastName)
VALUES
('Jon','Ye')
IF XACT_STATE() = 0
COMMIT TRAN
END TRY
BEGIN CATCH
IF XACT_STATE() <> 1
ROLLBACK TRAN
ELSE
COMMIT TRANSACTION
END CATCH
RETURN
GO
Error Message:
There are uncommitted transactionS.
I only see results when I close SQL Server.
the problem is here
XACT_STATE() only returns 0 if no active user transaction exists. Due to the open transaction started by BEGIN TRAN, XACT_STATE must be returning 1 and the COMMIT TRAN consequently does not execute. You should be checking for a return value of 1