single SELECT statement before THROW not terminated with semicolon
Posted: Sat 08 Sep 2018 10:38
When I choose the option Insert Semicolons, SELECT statements before THROW don't get an semicolon. This means that the keyword THROW is seen as an alias and does not function anymore. When you run the code with and without the semicolon you notice the difference.
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.
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.