一致的格式使得 Transact-SQL(T-SQL)更易于阅读、审阅和维护,尤其是在多人参与同一代码库时。 Visual Studio Code 的 MSSQL 扩展内置了一个 SQL 格式化器(预览),你可以按需运行,保存时配置自动格式化,并通过 Visual Studio Code 设置进行自定义。
MSSQL 扩展中的 T-SQL 格式化功能基于 ScriptDOM 构建,ScriptDOM 是一个开源的 .NET 库,能够解析 T-SQL 并基于抽象语法树生成脚本。
按需格式化
你可以在任何编辑器窗口中格式化T-SQL。 格式化器对整个文档或仅对你选择的文本有效。
要按需格式化T-SQL,请使用以下方法之一:
右键菜单:在 T-SQL 编辑器窗口中右键点击,选择 格式化文档 或 格式选择。
命令面板:运行 格式文档 或 格式选择。
键盘快捷键:格式化文档时,在 Windows 和 Linux 上按 Shift+Alt+F,在 macOS 上按 Shift+选项+F。 对于格式选择,在 Windows 和 Linux 上按 Ctrl+K、+F,或在 macOS 上按 Cmd+K、Cmd+F。
保存时的格式
在 Visual Studio Code 中,标准编辑器设置控制保存格式,而不是专门的 MSSQL 格式化设置。
在Visual Studio Codesettings.json文件中使用以下设置,每当你保存文件时,自动格式化T-SQL:
{
"[sql]": {
"editor.formatOnSave": true
}
}
配置格式选项
在 Visual Studio Code 设置界面或用户或工作区settings.json中配置格式。
在设置编辑器中,搜索 Mssql>格式 以查看可用的选项。 在 settings.json中,使用相应 mssql.format.* 的设置。
预览格式化器默认启用。 它的选项使用
mssql.format.options.*命名空间。启用预览格式化器后,现有的五个格式化设置依然可用。 预览格式化程序会添加
mssql.format.options.*设置。
支持的设置
表格列出了现有的格式化设置,然后是预览格式化器的设置。
现有的格式化器设置
| 设置 | 类型 | 默认 | Description |
|---|---|---|---|
mssql.format.alignColumnDefinitionsInColumns |
bool | false |
将列定义对齐在列中。 |
mssql.format.datatypeCasing |
枚举 | none |
将数据类型格式化为 uppercase、 或 lowercasenone (未格式化)。 |
mssql.format.keywordCasing |
枚举 | none |
关键词格式为 uppercase、 、 lowercase或 none (未格式化)。 |
mssql.format.placeCommasBeforeNextStatement |
bool | false |
在列表中每个项目开头放置逗号,例如 , mycolumn2,而不是在结尾, mycolumn1,例如。 |
mssql.format.placeSelectStatementReferencesOnNewLine |
bool | false |
在语句中将引用 SELECT 放在不同的行中。 对于 SELECT C1, C2 FROM T1,C1 和 C2 都位于不同的线路上。 |
预览格式化器设置
这些 mssql.format.options.* 设置会在启用预览格式化器时叠加到现有格式化器设置之上。
概况
| 设置 | 类型 | 默认 | Description |
|---|---|---|---|
mssql.format.enablePreviewFormatter |
bool | true |
使用SQL格式化器(预览版)。 |
mssql.format.showParseErrorNotification |
bool | true |
当格式化器无法完全解析T-SQL时,会显示通知。 |
mssql.format.options.sqlVersion |
枚举 | sql170 |
T-SQL 版本用于解析和生成格式化脚本。 |
mssql.format.options.sqlEngineType |
枚举 | all |
数据库引擎类型用于解析和生成格式化脚本。 有效值为:all、standalone 和 sqlAzure。 |
对齐
| 设置 | 类型 | 默认 | Description |
|---|---|---|---|
mssql.format.options.alignClauseBodies |
bool | true |
对齐 FROM、WHERE、GROUP BY 及类似子句的主体部分。 |
mssql.format.options.alignColumnDefinitionFields |
bool | true |
将列定义字段对齐,例如名称、类型和约束。 |
mssql.format.options.alignSetClauseItem |
bool | true |
对齐 SET 语句中的 UPDATE 子句项。 |
路径
| 设置 | 类型 | 默认 | Description |
|---|---|---|---|
mssql.format.options.allowExternalLanguagePaths |
bool | true |
允许外部语言内容使用文件路径。 |
mssql.format.options.allowExternalLibraryPaths |
bool | true |
允许外部库内容使用文件路径。 |
格式设置
| 设置 | 类型 | 默认 | Description |
|---|---|---|---|
mssql.format.options.asKeywordOnOwnLine |
bool | true |
将 AS 单独放在一行。 |
mssql.format.options.keywordCasing |
枚举 | uppercase |
关键字大小写风格。 有效值为:uppercase、lowercase 和 pascalCase。 |
mssql.format.options.preserveComments |
bool | true |
设置格式时保留注释。 |
mssql.format.options.numNewlinesAfterStatement |
int | 1 |
每个语句后的换行符数量,从 0 到 5。 |
缩进
| 设置 | 类型 | 默认 | Description |
|---|---|---|---|
mssql.format.options.indentSetClause |
bool | false |
在SET语句中缩进UPDATE子句。 |
mssql.format.options.indentViewBody |
bool | false |
缩进 VIEW 正文。 |
Multiline
| 设置 | 类型 | 默认 | Description |
|---|---|---|---|
mssql.format.options.multilineInsertSourcesList |
bool | true |
INSERT 源为多行。 |
mssql.format.options.multilineInsertTargetsList |
bool | true |
INSERT 列设为多行。 |
mssql.format.options.multilineSelectElementsList |
bool | true |
SELECT 列设为多行。 |
mssql.format.options.multilineSetClauseItems |
bool | true |
SET 条目显示为多行。 |
mssql.format.options.multilineViewColumnsList |
bool | true |
VIEW 列设为多行。 |
mssql.format.options.multilineWherePredicatesList |
bool | true |
将 WHERE 谓词设为多行。 |
新的一行
| 设置 | 类型 | 默认 | Description |
|---|---|---|---|
mssql.format.options.newLineBeforeCloseParenthesisInMultilineList |
bool | true |
多行列表中在右括号前换行。 |
mssql.format.options.newLineBeforeFromClause |
bool | true |
在从句前加一行 FROM 。 |
mssql.format.options.newLineBeforeGroupByClause |
bool | true |
在 GROUP BY 子句前换行。 |
mssql.format.options.newLineBeforeHavingClause |
bool | true |
在 HAVING 子句前换行。 |
mssql.format.options.newLineBeforeJoinClause |
bool | true |
在 JOIN 子句前另起一行。 |
mssql.format.options.newLineBeforeOffsetClause |
bool | true |
在 OFFSET 子句前换行。 |
mssql.format.options.newLineBeforeOpenParenthesisInMultilineList |
bool | false |
多行列表中开括号前加行。 |
mssql.format.options.newLineBeforeOrderByClause |
bool | true |
在 ORDER BY 子句前换行。 |
mssql.format.options.newLineBeforeOutputClause |
bool | true |
在 OUTPUT 子句前另起一行。 |
mssql.format.options.newLineBeforeWhereClause |
bool | true |
在 WHERE 子句前换行。 |
mssql.format.options.newLineBeforeWindowClause |
bool | true |
在 WINDOW 子句前换行。 |
mssql.format.options.newlineFormattedCheckConstraint |
bool | false |
换行格式约束 CHECK 。 |
mssql.format.options.newLineFormattedIndexDefinition |
bool | false |
按换行格式设置的索引定义。 |
Spacing
| 设置 | 类型 | 默认 | Description |
|---|---|---|---|
mssql.format.options.spaceBetweenDataTypeAndParameters |
bool | true |
例如 VARCHAR (255),数据类型与括号之间的空格。 |
mssql.format.options.spaceBetweenParametersInDataType |
bool | true |
数据类型中参数之间的空隙。 |
示例设置文件
{
"mssql.format.options.keywordCasing": "lowercase",
"mssql.format.options.alignClauseBodies": false,
"mssql.format.options.numNewlinesAfterStatement": 2,
"[sql]": {
"editor.formatOnSave": true
}
}
设置默认格式化器
要将MSSQL扩展设置为默认,请选择“配置默认格式......”>SQL Server(mssql),或者将以下配置添加到settings.json:
{
"[sql]": {
"editor.defaultFormatter": "ms-mssql.mssql"
}
}