This is a ‘quick-and-not-so-dirty’ version of a T-SQL formatter / ‘pretty printer’ in PowerShell. To use this, please install the SQL 2012 SP1 version of the SQLDOM from the Feature Pack page. (If you already have SQL 2012 client tools on your machine, you should be ready to go.) And you will need PowerShell of course
Here is a sample usage of this script:
tsqlpp.ps1 -Source c:\temp\testcases.sql -Target c:\temp\out.sql
Some usage tips:
- Right click and save the file to your machine, do NOT run it directly from this blog site
- To execute the script your PowerShell execution policy must be set to RemoteSigned
- The parser is by default set to 110 compatibility i.e. SQL 2012 syntax. If you are working with older compatibility levels feel free to edit the script to reference the 80, 90, 100 versions of the parser.
- The generated / formatted script will NOT contain comments, you will lose any comments you had in the unformatted version
Please leave your comments, suggestions and asks for future versions of the script in the Comments section of this post. Thank you and I’m glad to be of help to you!
Disclaimer: This script is provided AS IS with no warranties, and confer no rights.