我正在尝试编写此事务,我正在尝试使用 TRY..CATCH 和 TRANSACTION 将列添加到表中。这是我的代码。但是问题是表中已经存在该列并且应该执行catch块,但是catch块没有执行并且事务没有被回滚,并且select error_number语句也没有执行。
BEGIN TRY
BEGIN TRANSACTION;
ALTER TABLE ONADJ ADD BR_INIT CHAR (3) NULL REFERENCES BRANCH(BR_INIT)
COMMIT TRANSACTION;
END TRY
BEGIN CATCH
SELECT ERROR_NUMBER() AS ERRORNUMBER,ERROR_MESSAGE() AS ERRORMESSAGE;
IF (XACT_STATE()) = -1
BEGIN
PRINT
N'THE TRANSACTION IS IN AN UNCOMMITTABLE STATE. ROLLING BACK TRANSACTION.'
ROLLBACK TRANSACTION;
END;
IF (XACT_STATE()) = 1
BEGIN
PRINT
N'THE TRANSACTION IS COMMITTABLE. COMMITTING TRANSACTION.'
ROLLBACK TRANSACTION;
END;
END CATCH