single SELECT statement before THROW not terminated with semicolon

Discussion of open issues, suggestions and bugs regarding database management and development tools for SQL Server
Post Reply
Rivanni
Posts: 4
Joined: Sun 03 May 2015 15:17

single SELECT statement before THROW not terminated with semicolon

Post by Rivanni » 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.

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.

AlexandrS
Devart Team
Posts: 20
Joined: Mon 22 Jan 2018 11:27

Re: single SELECT statement before THROW not terminated with semicolon

Post by AlexandrS » Fri 14 Sep 2018 07:55

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.

Rivanni
Posts: 4
Joined: Sun 03 May 2015 15:17

Re: single SELECT statement before THROW not terminated with semicolon

Post by Rivanni » Fri 14 Sep 2018 20:35

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.

alexa
Devart Team
Posts: 2503
Joined: Fri 24 Jun 2011 14:17

Re: single SELECT statement before THROW not terminated with semicolon

Post by alexa » Mon 17 Sep 2018 13:34

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.

Post Reply