Refactoring T-SQL

While much has been said about the benefits of refactoring application code (see VFPX Code Analyst), I typically haven’t seen a lot of noise about refactoring T-SQL and stored procedure code.

The refactoring features found in SQL Manager tend to be more about refactoring your database design but not about the actual content of the SPs.

It’s important to note that if you tend to rely on large stored procedures, then many of the same rules of refactoring apply:

1. Keep it short and sweet.
2. Make it readable.

So when dealing with a particularly unruly stored proc (over 1000 lines), I was quite happy to find Red Gate’s SQL Refactor (here’s a post from the lead developer).

Some of the features are pretty basic (renaming variables, etc) but the one of great interest was the Encapsulate as a new Stored Proc.

As with a number of tools that are add-ons to other components, its overall usefulness might seem limited if you are building your stored procs either using testing patterns or have good code review sessions – but if you’re inheriting or just watching one procedure get too big, it’s definitely useful.

What about your SQL applications? How big do you let your stored procedures get?
What tools do you use?