formatting - Format SQL in SQL Server Management Studio

ID : 10354

viewed : 46

Tags : sql-serverformattingssmssql-server

Top 5 Answer for formatting - Format SQL in SQL Server Management Studio

vote vote

100

Late answer, but hopefully worthwhile: The Poor Man's T-SQL Formatter is an open-source (free) T-SQL formatter with complete T-SQL batch/script support (any DDL, any DML), SSMS Plugin, command-line bulk formatter, and other options.

It's available for immediate/online use at http://poorsql.com, and just today graduated to "version 1.0" (it was in beta version for a few months), having just acquired support for MERGE statements, OUTPUT clauses, and other finicky stuff.

The SSMS Add-in allows you to set your own hotkey (default is Ctrl-K, Ctrl-F, to match Visual Studio), and formats the entire script or just the code you have selected/highlighted, if any. Output formatting is customizable.

In SSMS 2008 it combines nicely with the built-in intelli-sense, effectively providing more-or-less the same base functionality as Red Gate's SQL Prompt (SQL Prompt does, of course, have extra stuff, like snippets, quick object scripting, etc).

Feedback/feature requests are more than welcome, please give it a whirl if you get the chance!

Disclosure: This is probably obvious already but I wrote this library/tool/site, so this answer is also shameless self-promotion :)

vote vote

82

There is a special trick I discovered by accident.

  1. Select the query you wish to format.
  2. Ctrl+Shift+Q (This will open your query in the query designer)
  3. Then just go OK Voila! Query designer will format your query for you. Caveat is that you can only do this for statements and not procedural code, but its better than nothing.
vote vote

79

Azure Data Studio - free and from Microsoft - offers automatic formatting (ctrl + shift + p while editing -> format document). More information about Azure Data Studio here.

While this is not SSMS, it's great for writing queries, free and an official product from Microsoft. It's even cross-platform. Short story: Just switch to Azure Data Studio to write your queries!

Update: Actually Azure Data Studio is in some way the recommended tool by Microsoft for writing queries (source)

Use Azure Data Studio if you: [..] Are mostly editing or executing queries.

vote vote

64

vote vote

60

The prior value for the key is dropped and replaced with the new one.

If you'd like to keep all the values a key is given, you might consider implementing something like this:

import org.apache.commons.collections.MultiHashMap; import java.util.Set; import java.util.Map; import java.util.Iterator; import java.util.List; public class MultiMapExample {     public static void main(String[] args) {       MultiHashMap mp=new MultiHashMap();       mp.put("a", 10);       mp.put("a", 11);       mp.put("a", 12);       mp.put("b", 13);       mp.put("c", 14);       mp.put("e", 15);       List list = null;        Set set = mp.entrySet();       Iterator i = set.iterator();       while(i.hasNext()) {          Map.Entry me = (Map.Entry)i.next();          list=(List)mp.get(me.getKey());           for(int j=0;j<list.size();j++)          {           System.out.println(me.getKey()+": value :"+list.get(j));          }       }    } } 

Top 3 video Explaining formatting - Format SQL in SQL Server Management Studio

Related QUESTION?