sqlsql-servercoding-stylecode-formattingquery-designer

SQL Server 2008 / 2012 - views & sql formatting


Hopefully this is a newbie question.

So if I have kind of a long set of selects in my SQL and a couple of sub-selects within my where clause, I'm less than satisfied with the formatting in the view designer, which appears to be the only way to display/show my views after they are saved.

So, in short, is there any way to directly edit my views within SSMS, and have the views retain the formatting in my code?

i.e.;

select case /*comment */
  when a = 5  
   then b = 2 
   else b = 4  /* why b 4 */
  end as field_1,
  dt as field_2,...

get's all mashed up

select case /*comment */     when a = 5                  
then b = 2                 else b = 4  /* why b 4 */              
end as field_1,             
dt as field_2,...

Or something along those lines.


Solution

  • Are you using the query designer? Instead, use an SSMS query window. From the SSMS Object Explorer, right-click on the view and select Script View as-->Alter To-->New Query Editor window. Format the view source in the query window and run the script to change the view. Your formatting will be preserved.