Format T-SQL Code (Pretty Print)

18 Dec
December 18, 2011

There is nothing more annoying than having to go through SQL Server code line by line trying to get the formatting of the each statement correct, in order to be able to reach the code. SQL Server (or any SQL Engine) has a multitude of plugins and online SQL code formatting tools. This short post goes through one online tool to format SQL Server code.

Unformatted SQL Server code is usually the result of inheriting someone else’s code, automatically generating the code (for example through Code Designer in SSMS), or simply by passing code around through a medium that loses the code’s formatting (for example text only emails).

There is a nifty little online tool to pretty print SQL code, which i regularly use.

The cool thing about this tool is that it allows you to customize your SQL code formatting output, for example if you want to place your SQL code on your website, you can output in HTML format. Additionally, the tool handles alphabetical casing very well (I show signs of OCD when it comes to SQL code casing), along with an array of other option to output your formatted SQL code in the exact SQL language implementation (so MS Access, Delphi, MySQL, etc.) and format required.

The site also has tutorial videos to go through the options and demonstrate using the tool.

There are other tools and SSMS plugin-ins to do T-SQL code formatting, as well as a paid SSMS plug-in of the same online tool highlighted above, although I never found the need to look further.

That being said, the tool does choke up on certain statements, I particularly noticed this when you have a PARTITION BY function that partitions based on a particular ORDER BY column, in which case the parser thinks the ORDER BY applies to the whole statement, rather than the PARTITION BY. but minor!

Hope it helps!

          0 votes
1 reply
  1. Dinesh says:

    This is also very good online t-sql code formatter/beautifier. It works perfectly.

    http://sqlserverlearner.com/online-tools/tsql-code-formatter

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>