As a SQL Server DBA or developer, one of the challenges we often face is creating a standardized T-SQL coding style among our team. Inconsistent code formatting can make it difficult to read and understand the code, leading to potential errors and inefficiencies. In this blog post, we will explore a free tool called Poor Man’s T-SQL Formatter that can help us adopt a common coding style.
Web Based SQL Formatter
Poor Man’s T-SQL Formatter offers a brilliant solution to create a consistent standardized T-SQL code formatting. Let’s see it in action with a series of examples:
Select tbla.Name, tbla.LstName, tblB.PhoneNum, tblC.Address from tblUsrName tbla join tblUsrPhone tblB on tblA.usrID = tblB.usrID left join tblUsrAddr tblC on tblC.usrID =tblausrID join (select workplace, workadr, workrole from tblOfice) z join tblB on z.ID=tblB.OfficeID where tblc.Stane in ('GA','AL','NY','CA')
By copying and pasting the above code into the Poor Man’s T-SQL Formatter web tool, we can easily format it for better readability.
SQL Formatter SSMS Add-In
While the web-based solution is effective, it is not integrated with SQL Server Management Studio (SSMS). To fully integrate the formatting component within SSMS, we can download and install the add-in called SqlFormatterSSMSAddIn.Setup.1.5.3.msi.
The installation process is straightforward, simply double-click on the .msi file and follow the setup instructions. The tool will install two components, a command line formatter and an add-on for SSMS.
Format SQL Code in File
If we have T-SQL code saved in a file, we can easily format it using the Poor Man’s T-SQL Formatter. Simply save the T-SQL code in a .sql file, such as “myquery.sql”, and execute the following command:
SqlFormatter myquery.sql /o:formatted_myquery.sql
This command will take the input from the file “myquery.sql” and produce a formatted .sql file named “formatted_myquery.sql”. We can then open the generated file with SSMS to see the formatted T-SQL code.
Format SQL Code in SSMS Query Window
The Poor Man’s T-SQL Formatter also integrates with SSMS. After installing the add-in, we need to close and reopen SSMS. Then, in a SSMS query window, we can highlight the code that needs to be formatted and select “Format T-SQL Code” from the SSMS Tools menu.
SQL Formatter SSMS Add-in Settings
The add-in also provides a formatting option dialog box that allows users to customize the formatting style according to their preferences.
Conclusion
In conclusion, Poor Man’s T-SQL Formatter is a valuable tool that can be used to establish a standard for T-SQL code writing among developers. By adopting a consistent coding style, we can improve code readability, maintainability, and collaboration within our team.
Troubleshooting
If you are unable to see the formatter integrated with your version of SSMS, please make sure to copy “PoorMansTSqlFormatterSSMSAddIn.AddIn” to the appropriate location:
- SQL 2012: C:\Users\All Users\Microsoft\SQL Server Management Studio\11.0\Addins
- SQL 2014: C:\Users\All Users\Microsoft\SQL Server Management Studio\12.0\Addins
Remember to restart SSMS after the file copy completes.