How can we help you today? How can we help you today?

SQL Prompt is not formatting IF statement correctly

The following SQL is not formatted correct.

IF @CalculateHeld = 'Yes' BEGIN
SELECT * FROM [#HeldFunds] WHERE [HeldAmount] > 0
DROP TABLE [#HeldFunds]
END ELSE BEGIN
SELECT * FROM [#HeldFunds2] WHERE [HeldAmount] > 0
DROP TABLE [#HeldFunds2]
END

BrienKing
0

Comments

6 comments

  • Matthew_Sontum
    Do you mean that everything inside the BEGIN and END blocks has no indentation? I have been seeing this as well.
    Matthew_Sontum
    0
  • BrienKing
    Correct
    BrienKing
    0
  • Russell D
    When you say its not formatting "correctly" - how do you think it should be being formatted?

    If I use default_lowercase on 9.2.9 I get this result with indentations:


    If you're not on the latest version trying updating and test again.
    Russell D
    0
  • BrienKing
    I am on the latest (9.2.9.6459) and it wasn't formatting correctly on the previous version.  In the previous version it was basically the opposite problem where everything was spread way out.

    In this version everything is all left justified.

    This is what is looks like after being formatted:

    IF @CalculateHeld = 'Yes' BEGIN
    SELECT * FROM [#HeldFunds] WHERE [HeldAmount] > 0
    DROP TABLE [#HeldFunds]
    END ELSE BEGIN
    SELECT * FROM [#HeldFunds2] WHERE [HeldAmount] > 0
    DROP TABLE [#HeldFunds2]
    END

    What it should look like is this:

    IF @CalculateHeld = 'Yes' BEGIN
    ...SELECT * FROM [#HeldFunds] WHERE [HeldAmount] > 0
    ...DROP TABLE [#HeldFunds]
    END ELSE BEGIN
    ...SELECT * FROM [#HeldFunds2] WHERE [HeldAmount] > 0
    ...DROP TABLE [#HeldFunds2]
    END


    BrienKing
    0
  • Russell D
    Are you using a default style or a custom style?
    Russell D
    0
  • way0utwest
    With my style, I get this:

    <div>IF @CalculateHeld = 'Yes'</div><div>BEGIN</div><div>&nbsp; &nbsp; SELECT *</div><div>&nbsp; &nbsp; FROM [#HeldFunds]</div><div>&nbsp; &nbsp; WHERE [HeldAmount] > 0;</div><div>&nbsp; &nbsp; DROP TABLE [#HeldFunds];</div><div>END;</div><div>ELSE</div><div>BEGIN</div><div>&nbsp; &nbsp; SELECT *</div><div>&nbsp; &nbsp; FROM [#HeldFunds2]</div><div>&nbsp; &nbsp; WHERE [HeldAmount] > 0;</div><div>&nbsp; &nbsp; DROP TABLE [#HeldFunds2];</div><div>END;<br></div>

    What I'd suggest is look at the whitespace and the control flow sections. Watch out for checking the "wrap if longer than" items. I've had some strange behavior here when these are applied with certain values.
    way0utwest
    0

Add comment

Please sign in to leave a comment.