在 SQL Server Management Studio 中格式化 SQL

2022-01-12 00:00:00 formatting sql-server ssms

在 Visual Studio &在其他 IDE 中,您可以轻松地使用键盘快捷键、通过菜单或在您键入时自动格式化您的代码.

In Visual Studio & other IDEs, you can easily auto format your code with a keyboard shortcut, through the menu, or automatically as you type.

我想知道是否有办法在 SQL Server Management Studio 中启用此标准功能?

I was wondering if there is yet a way to enable this standard feature in SQL Server Management Studio?

我正在处理一些大型的存储过程,这些过程是由格式不佳的 SQL 组成的杂乱无章的混乱,如果我可以去全选 -> 格式化 SQL"就好了

I'm working with some large-ish stored procs that are a mangled mess of poorly formatted SQL and it'd be nice if I could just go "Select All -> Format SQL"

推荐答案

迟到的答案,但希望值得:Poor Man's T-SQL Formatter 是一个开源(免费)T-SQL 格式化程序,具有完整的 T-SQL 批处理/脚本支持(任何 DDL、任何 DML)、SSMS 插件、命令行批量格式化程序和其他选项.

Late answer, but hopefully worthwhile: The Poor Man's T-SQL Formatter is an open-source (free) T-SQL formatter with complete T-SQL batch/script support (any DDL, any DML), SSMS Plugin, command-line bulk formatter, and other options.

它可在 http://poorsql.com 上立即/在线使用,今天刚刚升级到1.0 版""(它是几个月前的测试版),刚刚获得对 MERGE 语句、OUTPUT 子句和其他挑剔的东西的支持.

It's available for immediate/online use at http://poorsql.com, and just today graduated to "version 1.0" (it was in beta version for a few months), having just acquired support for MERGE statements, OUTPUT clauses, and other finicky stuff.

SSMS 插件允许您设置自己的热键(默认为 Ctrl-KCtrl-F,以匹配 Visual Studio),并格式化整个脚本或仅格式化您选择/突出显示的代码(如果有).输出格式可自定义.

The SSMS Add-in allows you to set your own hotkey (default is Ctrl-K, Ctrl-F, to match Visual Studio), and formats the entire script or just the code you have selected/highlighted, if any. Output formatting is customizable.

在 SSMS 2008 中,它与内置的智能感知很好地结合在一起,有效地提供了与 Red Gate 的 SQL Prompt 或多或少相同的基本功能(当然,SQL Prompt 有额外的东西,比如片段,快速对象脚本等).

In SSMS 2008 it combines nicely with the built-in intelli-sense, effectively providing more-or-less the same base functionality as Red Gate's SQL Prompt (SQL Prompt does, of course, have extra stuff, like snippets, quick object scripting, etc).

非常欢迎提供反馈/功能请求,如果有机会,请试一试!

Feedback/feature requests are more than welcome, please give it a whirl if you get the chance!

披露:这可能已经很明显了,但我写了这个库/工具/站点,所以这个答案也是无耻的自我推销:)

Disclosure: This is probably obvious already but I wrote this library/tool/site, so this answer is also shameless self-promotion :)

相关文章