SQL string formatter [closed]

安稳与你 提交于 2019-11-28 18:10:37

http://www.sqlinform.com/

This tool reformats SQL code. I've used it with awesome results. It's free as a webapp and has a downloadable version as well.

Check out sqlparse. It is a Python module that installs the command sqlformat. Usage is simple, e.g.:

sqlformat --reindent --keywords upper --identifiers lower my_file.sql

I tried the aforementioned CLI alternatives, but:

  • sqlinform is out, because I want an open source CLI application.
  • fsqlf has only few features (create view is missing for example).

Thanks to sqlformat I even learned that "REF" is a reserved keyword in SQL:2011 and SQL:2008.

fsqlf (http://sourceforge.net/projects/fsqlf/) is a command line or GUI program, open source, to format SQL. It supports having a formatting.conf file which allows you a lot of flexibility in how the final product looks.

Examples:

☺  [wwalker@speedy:~] 
$ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf 

SELECT
  f1
, f2
, fname
, lName
FROM tblName
WHERE f1=true
AND fname is not null
ORDER BY lName asc
☺  [wwalker@speedy:~] 
$ vim formatting.conf # 2 character change
☺  [wwalker@speedy:~] 
$ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf 

SELECT
 f1 ,
 f2 ,
 fname ,
 lName
FROM tblName
WHERE f1=true
AND fname is not null
ORDER BY lName asc
☺  [wwalker@speedy:~] 
$ vim formatting.conf # 1 character change
☺  [wwalker@speedy:~] 
$ echo "select f1, f2, fname, lName from tblName where f1 = true and fname is not null order by lName asc" | fsqlf 

SELECT
 f1 , f2 , fname , lName
FROM tblName
WHERE f1=true
AND fname is not null
ORDER BY lName asc
☺  [wwalker@speedy:~] 
$ 

I enjoy using http://www.dpriver.com - it's a nice add-in to SQL Server Management Studio.

Paul D. Eden

Not that I recommend spending the (relatively large) money on it for just this purpose, but Toad has a feature built in that does exactly what you're wanting.

It includes a bunch of options allowing you to set up exactly how you want your SQL formatted (all columns on separate lines, etc.). Pretty nice, but only if you've already got Toad. I know the Oracle version does this, but I'd assume the SQL Server or other versions would as well.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!