BUG: "Format SQL" introduces syntax errors
HugoKornelis
Posts: 40 Bronze 5
in SQL Prompt
Hi all,
The code below, though ill-formatted, is correct and works without error in SQL Server 2022:
SELECT frsx.ProductKey,<br> SUM (CAST (frsx.ShipDateKey AS bigint)) OVER w1 ,<br> MAX (frsx.RevisionNumber) OVER w1,<br> MIN (frsx.ShipDateKey) OVER w1 ,<br> COUNT (frsx.RevisionNumber) OVER w1,<br> COUNT (*) OVER w1 <br>FROM dbo.FactResellerSales AS frsx<br>WINDOW w1 AS (ORDER BY frsx.DueDateKey<br> ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING);
When I want to use SQL Prompt to reformat the code (Ctrl-K, Ctrl-Y), it adds an extra AS keyword in some places where it should not exist, and then gives up with an error message:
The code now looks like this:
SELECT frsx.ProductKey,<br> SUM (CAST (frsx.ShipDateKey AS bigint)) OVER w1 AS ,<br> MAX (frsx.RevisionNumber) OVER w1,<br> MIN (frsx.ShipDateKey) OVER w1 AS ,<br> COUNT (frsx.RevisionNumber) OVER w1,<br> COUNT (*) OVER w1 AS <br> FROM dbo.FactResellerSales AS frsx<br> WINDOW w1 AS (ORDER BY frsx.DueDateKey<br> ROWS BETWEEN CURRENT ROW AND UNBOUNDED FOLLOWING);
SQL Server will (rightly) throw an error when I try to execute this "reformatted" code.
--
Hugo Kornelis
(SQL Server MVP, 2006-2016 + 2019-now // Friend of Red Gate)
Hugo Kornelis
(SQL Server MVP, 2006-2016 + 2019-now // Friend of Red Gate)
Comments
Hi @HugoKornelis
Thank you for reaching out on the Redgate forums regarding this potential bug in SQL Prompt.
I believe I have replicated the behaviour using SQL Server 2022 using the OVER clause.
Initial code executes as anticipated, then when using the formatting it introduces an AS clause causing syntax issues.
Before formatting:
After formatting:
I've documented this and forwarded through to our development team. This forum post will be updated with any responses and/or fixes made available in a future release of SQL Prompt to target this concern.
Our development team have advised a fix for this has been included in the recent release of SQL Prompt v10.14.21
Release notes:
Features
Added support for Synapse Serverless SQL Pools.
Fixes