Formatting and Aligning SQL Server Code

Hi Experts,

I need to format SQL Server code so, it will be easier to read and understand. Does Sql Server has something to do that? If not can you refer me to a free tool.

Thanks a lot in advance.
HarreniAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikas GargBusiness Intelligence DeveloperCommented:
Hi,

You can try this online SQL formatter.

Or

SQL tuning

You can find more and detail about it over here

Hope this will help you.....
0
PortletPaulfreelancerCommented:
Online in a browser:
     For T-SQL I use: http://www.sql-format.com/
     For other SQL I use: http://poorsql.com/ (which is available as SSMS plugin, see below)

For free SSMS plugin try
http://www.architectshack.com/PoorMansTSqlFormatter.ashx

nb: I have not personally used it in SSMS but has a good reputation and a Notepad++ plugin too

Note also: BOTH these tools have options for what I call "comma first" layout (see below), something that may formatters will not do, but which I adhere to

-- http://www.sql-format.com/
SELECT
      y
    , AVG(CPUUsage) AS CPUUsage
    , AVG(CPUReady) AS CPUReady
    , AVG(MemoryUsage) AS MemoryUsage
    , AVG(DiskIOPerfRead) AS DiskIOPerfRead
    , AVG(DiskIOPerfWrite) AS DiskIOPerfWrite
    , AVG(NicPerf) AS NicPerf
FROM x

Open in new window

-- http://poorsql.com/
SELECT y
 , AVG(CPUUsage) AS CPUUsage
 , AVG(CPUReady) AS CPUReady
 , AVG(MemoryUsage) AS MemoryUsage
 , AVG(DiskIOPerfRead) AS DiskIOPerfRead
 , AVG(DiskIOPerfWrite) AS DiskIOPerfWrite
 , AVG(NicPerf) AS NicPerf
FROM x

Open in new window

0
hostingplusCommented:
Notepad++ is free. When you open the file select language up the top, then select the language of the code. It will automatically colour is so its easier to read.

Also turn on word wrap so you don't have to scroll right constantly if it helps.
0
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
<not an answer, but it has to be said>
On behalf of all developers who inherit others code for a living, thank you for paying attention to make your code easier to read and understand.
1
ZberteocCommented:
You can use the Refractor free tool from ApexSQL:

http://www.apexsql.com/sql_tools_refactor.aspx

It simply installs and seamlessly integrates with Management Studio and you can choose different kind of formatting. It installs in you menu and you can use it from there by clicking it or  by simply applying a combination of keys, like Ctrl+Shift+Alt+F.

It also has other features beside formatting.  They have an intllisense with autocomplete plug in with more features that the one from MS but it is a bit intrusive and sometimes causes the MS to crash. I disabled that one. Good thing is that you can enable/disable then from the menu if you need tu turn them on/off..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.