Formatting T-SQL

As you might know I’m really picky when it comes to formatting code… heck, I event wrote a book on it! This even extends to T-SQL. I have worked at companies that had stored procedures that were formatted so poorly that I could not follow it at all. Thankfully I found a very cool web site called Instant SQL Formatter that does all the work for me… instantly! It will format T-SQL like this (generated by SQL Server Management Studio):

USE [Acme]
GO
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id =
 OBJECT_ID(N'[dbo].EventLog_DeleteOld') AND type in (N'P', N'PC'))
 DROP PROCEDURE [dbo].EventLog_DeleteOld
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].EventLog_DeleteOld AS
 BEGIN DELETE FROM EventLog WHERE ([TimeStamp] < GETDATE() - 14) END
GO

To this:



Very nice! There are lots of options (the T-SQL was formatted with “Function Case” set to “Unchanged”).

Tip Submitted By: David McCarter

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s