Page 1 of 1

single SELECT statement before THROW not terminated with semicolon

Posted: Sat 08 Sep 2018 10:38
by Rivanni
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.

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.

Re: single SELECT statement before THROW not terminated with semicolon

Posted: Fri 14 Sep 2018 07:55
by AlexandrS
Hello,

We have checked the issue and it turned out to be fine as the server returns no error. And the following statement:
SELECT 'In catch block.' -- no semicolon inserted here
THROW;
inserts semicolons after THROW expression because of being understood as the alias by the server.

Re: single SELECT statement before THROW not terminated with semicolon

Posted: Fri 14 Sep 2018 20:35
by Rivanni
The case someone using THROW as an alias is very very small. Using THROW as a keyword will be the default. So not inserting a semicolon after the SELECT and treating THROW as alias is bad practica. At least make it an option.

Re: single SELECT statement before THROW not terminated with semicolon

Posted: Mon 17 Sep 2018 13:34
by alexa
You could add a suggestion on the UserVoice forum where other users can vote for it https://devart.uservoice.com/forums/87893-sql-complete

We collect and analyze the information from this forum in order to make a proper roadmap for the future product releases.