T-SQL Formatters


August 6, 2018 by Kenneth Fisher

The other day I was given a truly horribly formatted piece of SQL. Something a bit like this only worse, and with plenty of dynamic SQL.

CREATE TRIGGER [Sales].[iduSalesOrderDetail] ON [Sales].[SalesOrderDetail] 






    DECLARE @Count int;

    SET @Count = @@ROWCOUNT;

    IF @Count = 0 



    SET nocount on;


    begin try
-- If inserting or updating these columns
IF update([ProductID]) OR UPDATE([OrderQty]) or Update([UnitPrice]) OR update([UnitPriceDiscount]) 
-- Insert record into TransactionHistory
    INSERT into [Production].[TransactionHistory]















                inserted.[ProductID]        ,inserted.[SalesOrderID]




                ,'S',GETDATE()                ,inserted.[OrderQty]




FROM inserted 


INNER join [Sales].[SalesOrderHeader] 


ON inserted.[SalesOrderID] = [Sales].[SalesOrderHeader].[SalesOrderID];
            UPDATE [Person].[Person] 


            SET [Demographics].modify('declare default element namespace 




                replace value of (/IndividualSurvey/TotalPurchaseYTD)[1] 


                with data(/IndividualSurvey/TotalPurchaseYTD)[1] + sql:column ("inserted.LineTotal")')   from inserted 
                INNER JOIN [Sales].[SalesOrderHeader] AS SOH


	ON inserted.[SalesOrderID] = SOH.[SalesOrderID] INNER JOIN [Sales].[Customer] AS C


                ON SOH.[CustomerID] = C.[CustomerID]

	    WHERE C.[PersonID] = [Person].[Person].[BusinessEntityID];



        -- Update SubTotal in SalesOrderHeader record. Note that this causes the 


        -- SalesOrderHeader trigger to fire which will update the RevisionNumber.


        UPDATE [Sales].[SalesOrderHeader]


I have a hard time working with code that’s so badly formatted I can’t read it. Normally I’d just format it myself, but this was a good dozen pages long. So what did I do? I ran to twitter screaming for help.

And I got a lot of responses! Here were the recommendations followed by who recommended them:

I haven’t tested most of these but from what I understand none of them (for obvious reasons) will handle dynamic SQL and the ones I tested won’t handle the multi-line breaks. For the multi-line breaks I just did a regex search and replace, then used the formatter.

Now, as I was writing this Brent Erik Darling (b) wrote a similar post so I’m including it for completeness. Quick tips for debugging large stored procedures – See tip 1

3 thoughts on “T-SQL Formatters

  1. I don’t know if my brain doesn’t quite focus right but I find badly formatted code near impossible to read.

    • You and me both! It’s really astounding how much time I’ve spent re-formatting other people’s code. Got in trouble for it once too. They were using code versioning and of course this completely messed up their comparisons 😀

  2. […] Kenneth Fisher links to a set of T-SQL formatters: […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Enter your email address to follow this blog and receive notifications of new posts by email.

Join 3,755 other subscribers

Follow me on Twitter

ToadWorld Pro of the Month November 2013
%d bloggers like this: