Semicolon placement with IF BEGIN-END ELSE BEGIN-END

Mikael_HMikael_H Posts: 16 Bronze 1
edited February 2, 2021 3:32PM in SQL Prompt
Hi!
I really like the formatting style options available. However, in the following IF-ELSE block the semicolon after the first END does not look so nice and I often manually remove it after applying code formatting.

Default

IF @a = 1
BEGIN
    PRINT 'This way';
END;
ELSE
BEGIN
    PRINT 'That way';
END;

Collapsed

IF @a = 1 BEGIN
    PRINT 'This way';
END;
ELSE BEGIN
    PRINT 'That way';
END;

To me, the semicolon suggests that the IF branch is completed, and the ELSE branch is a separate statement. The semicolon placement is syntactically correct, and maybe even required in upcoming versions of SQL Server.
SQL Prompt version 10.6.0.17688


Tagged:

Best Answer

  • Dan_JDan_J Posts: 465 Silver 2

    Thanks for your reply on this, my sincere apology for the delay in responding to you!

    I completely understand that unchecking Insert semicolons is not a good solution for you on this. I have had a further look through the SQL Prompt formatting options but unfortunately cannot find a way to achieve this. As a result, can I encourage you to raise this as a feature request via our User Voice system (https://redgate.uservoice.com/forums/94413-sql-prompt). Our development team will then review it alongside other submitted feature requests.
    Kind regards

    Dan Jary | Redgate Software
    Have you visited our Help Center?

Answers


  • Thanks for reaching out to us regarding this, my apology for the delay in responding to you!

    I may need to raise this with our development team as a feature request, but before I do so, would you mind checking if the following helps? - Looking under the Styles options, unchecking 'Insert semicolons' will stop the semicolon being added after formatting.


    I hope this helps, but please don't hesitate in coming back to us again if any further assistance is required on this.

    Kind regards

    Dan Jary | Redgate Software
    Have you visited our Help Center?
  • Mikael_HMikael_H Posts: 16 Bronze 1
    Hi!
    Unchecking the Insert semicolons action prevents the insertion of the semicolon after the first END, as well as any other missing semicolons when formatting the code.

    An option for specifying the behavior in this scenario would be nice, but I also understand the complexity of adding formatting options without introducing conflicting settings or other flaws.

    Actually, a reason for this particular formatting behavior catching my attention was that in a previous version this particular "extra" semicolon insertion was made without the full CR+LF line and that caused the source control to detect change in line endings. However, I can't reproduce that behavior now so I assume that has been corrected.

    Best Regards
    Mikael
Sign In or Register to comment.