Honestly I've never tried printing from SSMS before. For a workaround I'd copy and paste the code into Microsoft Word (or some other editor which supports rich data, aka text with color) and print from there.
Are you really printing code that often that the color matters?
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.
...is 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:
Go to Tools > Options > Environment > Keyboard
Place your cursor into the Press shortcut keys:
box
Hit Ctrl+K, Ctrl+D
Change the Shortcut currently used by:
drop-down from DataWarehouse Designer
to Text Editor
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.
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.
Best Answer
You could use
COALESCE(NullableColumn,'')
for any column you don't want to export withNULL
values.That
COALESCE
will output zero-length strings instead.Most of the time, however, it is important to know the difference between a zero-length string and a
NULL
value in exported data.You're sample query would become:
Although, I'd really prefer to see it as:
Putting the column alias at the beginning of the line makes it easier to find columns referenced in the output when doing debugging work down the road.