Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

According to this Microsoft document:

I am supposed to be able to use ctrl+K then ctrl+D to format my SQL documents in SQL Server Management Studio 2012 but when I use that combo I get the error:

The key combination (Ctrl+K, Ctrl+D) is bound to command (Format Document) which is not currently available.

I am trying to make modifications to an existing SQL document that has no formatting to it at all which makes it extremely difficult to read. Does anyone know how to make the Format Document command available so I can have SQL format this code for me?

share|improve this question
I think the statement Available only in the text editor is the tech writer's way of saying "not the query editor" without explicitly calling out the negative – billinkc Jun 21 '13 at 17:55
@billinkc I don't think that's quite right. The text editor is the "query editor" used for both Transact-SQL and XML, it just isn't really a query editor when it's dealing with XML. I think the documentation is a bit over-promising, and I commented as such on the Connect item. – Aaron Bertrand Jun 21 '13 at 22:51
up vote 25 down vote accepted

The keyboard combination you're after (Ctrl+K, Ctrl+D) is for "formatting" - but not to the extent you seem to expect. It is not a prettifier, it is just used to insert correct spacing and tabs as found in Tools > Options > Text Editor > Transact-SQL > General/Tabs - so if you highlight a portion of text and hit the keyboard combination, it is supposed to convert tabs to 4 spaces (if you've selected insert spaces), apply the type of indenting you've specified, etc.

This option is NOT intended to make code more readable - this is not functionality that Management Studio currently offers natively. Though there are several 3rd party options - some are external to Management Studio, like:

And there are add-ins as well for various levels of formatting assistance within the editor:

Now, the reason you are getting the message...

The key combination (Ctrl+K, Ctrl+D) is bound to command (Format Document) which is not currently available. because SSMS has mapped that key combination to a different context. The way you should be able to "fix" this - again, it still wouldn't do what you want it to do, even if the "fix" worked - is by doing the following:

  1. Go to Tools > Options > Environment > Keyboard
  2. Place your cursor into the Press shortcut keys: box
  3. Hit Ctrl+K, Ctrl+D
  4. Change the Shortcut currently used by: drop-down from DataWarehouse Designer to Text Editor

    enter image description here

  5. Press OK

Now, this is supposed to map the keyboard combination to the text editor, but Management Studio reverts it after hitting OK (you will continue to get the error message. So, I think the problem is that the documentation believes this functionality exists, but Management Studio knows better and simply doesn't offer it (and Microsoft will likely write this off as a bug in the documentation, and correct it, rather than a shortcoming in the tool). There may be hope in the future but, for now, this is a known and largely ignored issue. You'll note that the Formatting tab that the documentation refers to is simply not present (though it is there for XML, where the keyboard combination does work). The documentation should probably state:

Applies the indenting and space formatting for the language specified on the Formatting pane of the language in the Text Editor section of the Options dialog. Available only in the text editor and only for certain languages.

Another way to get an idea of what type of formatting options SSMS offers natively is to go to Tools > Customize > Commands > Edit > Add Command... > Format and look at the list of possible commands there. Nothing that indicates there is any knowledge of the actual language, so it wouldn't know where to insert line breaks or add additional indents or assist with parentheses etc.

enter image description here

If you want language-specific formatting to make existing T-SQL code more readable, you won't get much from SSMS, and you'll need to seek other options.

share|improve this answer

I don't believe this is possible in SSMS. There was a Connect item opened up for this feature.

What I personally like to use for poorly formatted SQL code is the web application on Poor SQL. It does a really nice job of formatting SQL code to your specifications. Even generates HTML if you so desire. I don't use the SSMS plug-in or any other deliverables they advertise, I always just hop on the site to do the quick formatting and then copy/paste right into SSMS.

share|improve this answer
Thanks for the tip on Poor SQL. Gonna keep that one tucked in my back pocket for sure! – Kris Gruttemeyer Jun 21 '13 at 15:30
Another good one (also useful for formatting code for blogs) is Simple-Talk's SQL Prettifier. – Aaron Bertrand Jun 21 '13 at 15:47
So even though Microsoft's document says it can format, SSMS can not? – user11512 Jun 21 '13 at 16:47

Install an add-on that does this. I use the Free Edition of SQL Complete: which offers some basic SQL formatting functionality. It integrates with SSMS and uses Ctrl + K, D.

share|improve this answer
free online version – imanabidi Dec 29 '14 at 8:17

SqlSmash is a commercial tool that lets you format your SQL (and much more) in SSMS 2012 and 2014. The default keyboard shortcut is (Ctrl + K, Ctrl + D).
Disclaimer: I am the developer behind it.

share|improve this answer
Please add that the tool is not free. – ypercubeᵀᴹ Aug 1 '14 at 21:43

protected by Paul White Aug 1 '14 at 21:15

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?