Code: Select all
USE tempdb;
GO
DROP TABLE IF EXISTS TestRethrow;
CREATE TABLE dbo.TestRethrow
(ID INT PRIMARY KEY);
BEGIN TRY
INSERT dbo.TestRethrow(ID) VALUES(1);
-- Force error 2627, Violation of PRIMARY KEY constraint to be raised.
INSERT dbo.TestRethrow(ID) VALUES(1);
END TRY
BEGIN CATCH
SELECT ID FROM TestRethrow; -- is terminated
SELECT 'In catch block.' -- no semicolon inserted here
THROW;
END CATCH;
When the SELECT is replaced by PRINT the semicolon is added. Without it, SQL Server won't run the code anyway due to incorrect syntax. (The statement before the THROW statement must be followed by the semicolon (;) statement terminator.)
The semicolon is also added with UPDATE and DELETE statements, even single lined ones.
The semicolon is added when the SELECT statement contains clauses like WHERE and ORDER BY.
So the problem only occurs with a single SELECT only. A bug IMO.