在 Visual Studio Code 的 MSSQL 扩展中格式化 Transact-SQL

一致的格式使得 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.* 的设置。

支持的设置

以下设置用于配置 SQL 格式化器。

概况

设置 类型 默认 Description
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 将 UPDATE 语句中的 SET 子句项对齐。
mssql.format.options.clauseBodyAlignment 枚举 aligned 保留子句主体aligned及其关键字,或将其放在下一行,如indented所示。

外壳与标识符

设置 类型 默认 Description
mssql.format.options.builtInFunctionCasing 枚举 preserve 支持的内置函数名称(如 GETDATE 和 COALESCE)的套管样式。 有效值为 preserve、uppercase、lowercase 和 pascalCase。
mssql.format.options.identifierBracketing 枚举 preserve 请保留、添加或移除标识符周围的可选方括号。 有效值为:preserve、includeBrackets 和 excludeBrackets。 保留了必需的括号。
mssql.format.options.identifierCasing 枚举 preserve 对象标识符的外壳样式。 有效值为 preserve、uppercase、lowercase 和 pascalCase。
mssql.format.options.keywordCasing 枚举 uppercase 关键字大小写风格。 有效值为:uppercase、lowercase 和 pascalCase。

路径

设置 类型 默认 Description
mssql.format.options.allowExternalLanguagePaths bool true 允许外部语言内容使用文件路径。
mssql.format.options.allowExternalLibraryPaths bool true 允许外部库内容使用文件路径。

格式设置

设置 类型 默认 Description
mssql.format.options.asKeywordOnOwnLine bool true 将 AS 单独放在一行。
mssql.format.options.columnAliasStyle 枚举 asKeyword 使用 AS、等号或其原始语法来格式化列别名。 有效值为:asKeyword、equalsSign 和 preserve。
mssql.format.options.commaPlacement 枚举 trailing 在列表项目trailing末尾()或下一个项目leading()开头加上逗号。
mssql.format.options.leadingCommaSpaceCount 整数 1 引号后方的空格数。 有效值为 0 和 1。
mssql.format.options.persistTrailingGo bool false 保留原始脚本中末尾的 GO 批次分隔符。
mssql.format.options.preserveComments bool true 设置格式时保留注释。
mssql.format.options.terminateBlockStatements bool false 在 BEGIN...END 和 TRY...CATCH 块后添加分号结束符。

缩进

设置 类型 默认 Description
mssql.format.options.indentSetClause bool false 缩进UPDATE语句中的SET子句。
mssql.format.options.indentViewBody bool false 缩进 VIEW 主体。

Multiline

设置 类型 默认 Description
mssql.format.options.multilineGroupByElementsList bool false 将 GROUP BY 元素格式化为多行列表。
mssql.format.options.multilineHavingPredicatesList bool true 将由 AND 或 OR 分隔的 HAVING 谓词格式化为多行。
mssql.format.options.multilineInsertSourcesList bool true 将源格式 INSERT 化为多行列表。
mssql.format.options.multilineInsertTargetsList bool true 将 INSERT 列设置为多行列表。
mssql.format.options.multilineInValuesList bool false 将谓词中的 IN 值格式化为多行列表。
mssql.format.options.multilineNestedFunctionCalls bool false 在不同的缩进行上格式化嵌套函数调用,同时将独立的函数调用集中在一行。
mssql.format.options.multilineOrderByElementsList bool false 将 ORDER BY 元素格式化为多行列表。
mssql.format.options.multilinePartitionByElementsList bool false 将窗口规范中的 PARTITION BY 元素格式化为多行列表。
mssql.format.options.multilineProcedureParametersList bool false 将过程参数和函数参数分别格式化到单独的行中。
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 谓词格式化为多行列表。
mssql.format.options.multilineWithOptionsList bool false 将受支持的 WITH 和 OPTION 子句值分别格式化到不同的行上。

新的一行

设置 类型 默认 Description
mssql.format.options.newLineAfterJoinKeyword bool true 将联接的表源放在 JOIN 关键字之后的新行上。
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.newLineBeforeOnClause bool true 将 join 的 ON 子句放在新的一行上。
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 将 UNIQUE、 INCLUDE、 以及 WHERE 部分内联索引定义放在不同的行上。

语句间距和批处理间距

设置 类型 默认 Description
mssql.format.options.numNewlinesAfterBatches 整数 1 每个 GO 批次分隔符后的换行次数,从 0 到 5。
mssql.format.options.numNewlinesAfterBatchStatement 整数 2 批处理中每个顶层语句后面的换行数,从 0 到 5。
mssql.format.options.numNewlinesAfterStatement 整数 1 每个语句后的换行符数量,从 0 到 5。

Spacing

设置 类型 默认 Description
mssql.format.options.spaceBetweenDataTypeAndParameters bool true 例如,在数据类型与其括号 VARCHAR (255)之间插入空格。
mssql.format.options.spaceBetweenParametersInDataType bool true 在数据类型中插入参数间的空格,例如 DECIMAL (10, 2)。

示例设置文件

{
  "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"
  }
}