一致的格式能讓 Transact-SQL(T-SQL)更容易閱讀、審查和維護,尤其當多人共同貢獻同一程式碼庫時。 Visual Studio Code 的 MSSQL 擴充套件內建 SQL 格式化器,您可以按需執行,並在儲存時設定自動格式化,並透過 Visual Studio Code 設定自訂。
MSSQL 擴充中的 T-SQL 格式化功能建立在 ScriptDOM 上,這是一個開源的 .NET 函式庫,能解析 T-SQL 並根據抽象語法樹產生腳本。
依需求格式化
你可以在任何編輯器視窗中格式化 T-SQL。 格式化器對整份文件或只對你選擇的文字有效。
若要按需格式化 T-SQL,請使用以下方法之一:
右鍵選單:在 T-SQL 編輯器視窗中右鍵點擊,選擇 格式 文件或 格式選擇。
指令調色盤:執行 格式文件 或 格式選擇。
鍵盤快捷鍵:格式化文件時,Windows 和 Linux 按 Shift+Alt+F,macOS 按 Shift+選項+F。 格式選擇時,在 Windows 和 Linux 按 Ctrl+K、Ctrl+F,或在 macOS 按 Cmd+K、Cmd+F。
存檔時的格式
在 Visual Studio Code 中,標準編輯器設定控制的是儲存格式,而非專用的 MSSQL 格式化設定。
在 Visual Studio Code settings.json 檔案中,請使用以下設定,在儲存檔案時自動格式化 T-SQL:
{
"[sql]": {
"editor.formatOnSave": true
}
}
設定格式選項
在 Visual Studio Code 設定介面或使用者或工作區settings.json中設定格式。
在設定編輯器中搜尋 MsSQL>格式 以查看可用選項。 在 settings.json中,使用相應 mssql.format.* 的設定。
支援的設定
下列設定用於設定 SQL 格式化工具。
一般
| Setting | 類型 | 預設 | Description |
|---|---|---|---|
mssql.format.showParseErrorNotification |
bool | true |
當格式化器無法完全解析 T-SQL 時,請顯示通知。 |
mssql.format.options.sqlVersion |
列舉 | sql170 |
T-SQL 版本用於解析與產生格式化腳本。 |
mssql.format.options.sqlEngineType |
列舉 | all |
資料庫引擎 類型用於解析與產生格式化腳本。 有效值為 all、standalone和 sqlAzure。 |
對準
| Setting | 類型 | 預設 | 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 子句項目。 |
mssql.format.options.clauseBodyAlignment |
列舉 | aligned |
保留子句主體 aligned 及其關鍵字,或將其放在下一行,如 indented。 |
外殼與識別碼
| Setting | 類型 | 預設 | 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。 |
路徑
| Setting | 類型 | 預設 | Description |
|---|---|---|---|
mssql.format.options.allowExternalLanguagePaths |
bool | true |
允許外部語言內容使用檔案路徑。 |
mssql.format.options.allowExternalLibraryPaths |
bool | true |
允許外部函式庫內容使用檔案路徑。 |
Formatting
| Setting | 類型 | 預設 | 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 區塊後面加上分號結束符號。 |
縮排
| Setting | 類型 | 預設 | Description |
|---|---|---|---|
mssql.format.options.indentSetClause |
bool | false |
在SET陳述式中縮排UPDATE子句。 |
mssql.format.options.indentViewBody |
bool | false |
縮排 VIEW 主體內容。 |
多行
| Setting | 類型 | 預設 | 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 子句值分別在不同行。 |
換行
| Setting | 類型 | 預設 | 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 部分內嵌索引定義放在獨立行中。 |
語句與批次間距
| Setting | 類型 | 預設 | Description |
|---|---|---|---|
mssql.format.options.numNewlinesAfterBatches |
整數 | 1 |
每個 GO 批次分隔符後的行斷點數,從 0 到 5。 |
mssql.format.options.numNewlinesAfterBatchStatement |
整數 | 2 |
在批次中,每個頂層陳述句後的換行數,從 0 到 5。 |
mssql.format.options.numNewlinesAfterStatement |
整數 | 1 |
每個陳述式後的換行次數,從 0 到 5。 |
間距
| Setting | 類型 | 預設 | 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"
}
}