1

When creating a new view in SSMS I inserted nicely formatted SQL code which included CTEs. As soon as I click into the diagram pane, the SQL code loses alignment no matter which editor options I try. What am I missing? I'm trying to make the SQL easier to maintain and more readable, but the editor will not cooperate. Where/How do you preserve formatting? I appreciate any help!

2
  • 4
    Don't use the diagram pane with CTEs. Really, I can't remember the last time I used the diagram at all. If you do need this, save your work before opening the pane, capture your diagram, close the pane, and then revert. Commented Jun 11, 2014 at 20:32
  • Create your view in a New Query window. Don't use the visual designers; they're utter crap. They won't keep your formatting because they're trying to "improve" it. Commented Jun 11, 2014 at 20:55

1 Answer 1

1

SSMS Query Designer helps you to write a query and it should really be used only when you are writing your a Sql server query for the 1st time :).

When creating views or any other sql server objects use the SSMS query Window to write you code. Once you have created the object next time when you want to get the definition of that object use system stored procedure sp_helptext, which will return the code the way you wrote it.

EXECUTE sp_helptext objectName
Sign up to request clarification or add additional context in comments.

4 Comments

Perfect answer, but to me it sounds more like a workaround. Why doesn't SSMS preserve formatting?!
Thank you :) . If you have ever written a query in query designer you have probably noticed it as soon as you execute it in query designer it just changes the formatting, it just not on of its functionalities, And personally I find it very difficult to write a query using query designer, it just doesnt feel right :)
@PatrickHofman It does preserve formatting. For that, you need to create the view in the query window, that format gets preserved. The designer just formats it the way it wants
Also since query designer does a few things for you like creating joins and join conditions, it would prefer to have code the way it likes :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.