Ignore Begin statements with CTE immediately after
SQL Prompt's "Insert semicolon" function behaves weirdly when a CTE is a the start of a control loop. For example, if I have this code:
IF (1=1) BEGIN
WITH numbers AS (
SELECT 1 AS num
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
)
SELECT * FROM numbers
END;
SQL Prompt will format it as
IF (1=1) BEGIN;
WITH numbers AS (
SELECT 1 AS num
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
)
SELECT * FROM numbers;
END;
While the code runs, I think the semicolon after "BEGIN" makes very little sense. First, the beginning of a control loop generally isn't terminated with a semicolon – just the end. Second, the code without the semicolon after "Begin" runs just fine, since the "IF..." statement doesn't count towards the "last line must end in a semicolon" rule.
This actually matters to me because I tend to use straight "Begin/End" pairs to denote code regions. So I will have code such as:
BEGIN -- Do Something
<some sql>
END;
BEGIN -- Do something else
<more sql>
END;
So the semicolon after "begin" throws everything off when I collapse everything and just draws attention to its goofiness. Also, SQL Prompt often throws and error I try to format the code, and then I have to insert the unnecessary semis. So I end up doing stuff like
IF (1=1) BEGIN
;
WITH numbers AS (
SELECT 1 AS num
UNION SELECT 2
UNION SELECT 3
UNION SELECT 4
)
SELECT * FROM numbers;
END;
This works, but I hate to have to do this because the formatting tool can't figure out when it is in a block.