The difference in behavior is because the first batch of ALTER TABLE
statements is a compilation error whereas the second batch of DROP INDEX
statements is a runtime error.
When a compilation error occurs on a batch, no code executes and only the compilation error is returned. Also, since no code executes with a compilation error, the error cannot even be caught with structured error handling:
BEGIN TRY
ALTER TABLE TABLE1 ALTER COLUMN AA INT -- Error Here
ALTER TABLE TABLE1 ALTER COLUMN BB INT
PRINT('CONTINUE AFTER ERROR')
END TRY
BEGIN CATCH
PRINT 'CAUGHT ERROR';
END CATCH;
Msg 4902, Level 16, State 1, Line 4 Cannot find the object "TABLE1"
because it does not exist or you do not have permissions.
When compilation is successful and a runtime error happens, subsequent statements in the same batch may or may not execute after an error depending the error severity and XACT_ABORT setting
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…