As we know, ending each statement with a semicolon is good practice. Suppose we have old code that uses RAISERROR to iterate over exceptions, and we want to exchange it for THROW .
From: THROW :
A statement before the THROW statement must be followed by a semicolon (;).
Using only ROLLBACK :
BEGIN TRY -- some code SELECT 1; END TRY BEGIN CATCH -- some code ROLLBACK THROW; END CATCH
and we get Incorrect syntax near 'THROW'. which works great.
But using ROLLBACK TRANSACTION works without a semicolon:
BEGIN TRY -- some code SELECT 1; END TRY BEGIN CATCH -- some code ROLLBACK TRANSACTION THROW; END CATCH
LiveDemo
Finally, using ROLLBACK TRANSACTION @variable :
DECLARE @TransactionName NVARCHAR(32) = 'MyTransactionName'; BEGIN TRY -- some code SELECT 1; END TRY BEGIN CATCH -- some code ROLLBACK TRANSACTION @TransactionName THROW; END CATCH
and get Incorrect syntax near 'THROW'. again Incorrect syntax near 'THROW'. .
Is there any special reason why the second example works (backward compatiblity / ...)?
source share