You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add code analysis settings dialog section for VS Code (#36840)
Fix image path
SQL code analysis feedback
Updated instructions for enabling and disabling SQL code analysis in various environments, including Visual Studio and SQL Server Management Studio. Consolidated information and improved clarity on using the code analysis settings dialog.
Co-authored-by: Iqra Shaikh <iqrashaikh@microsoft.com>
You can eliminate potential design and naming problems and avoid performance pitfalls by analyzing your database code. The concepts are very similar to performing static analysis to detect and correct defects in managed code. You configure which analysis rules you want to apply to your database code, analyze the code, and then correct or ignore the issues that you identify. Before you can analyze your database code, you must first import your database schema into a database project. For more information, see [Start from an existing database](../../tutorials/start-from-existing-database.md).
22
+
You can eliminate potential design and naming problems and avoid performance pitfalls by analyzing your database code. The concepts are similar to performing static analysis to detect and correct defects in managed code. You configure which analysis rules you want to apply to your database code, analyze the code, and then correct or ignore the issues that you identify. Before you can analyze your database code, you must first import your database schema into a database project. For more information, see [Start from an existing database](../../tutorials/start-from-existing-database.md).
23
23
24
-
By performing static analysis with the [provided rules](#provided-rules), you can identify problems that fall into the following categories:
24
+
By performing static analysis with the [provided rules](#provided-rules), you can identify problems that fall into the following Transact-SQL (T-SQL) categories:
25
25
26
-
-[T-SQL Design Issues](#t-sql-design-issues)
27
-
Design issues include code that might not behave the way in which you expect, deprecated syntax, and issues that could cause problems when the design of your database changes.
26
+
-[T-SQL design problems](#t-sql-design-problems): Design problems include code that might not behave the way you expect, deprecated syntax, and issues that could cause problems when the design of your database changes.
28
27
29
-
-[T-SQL Naming Issues](#t-sql-naming-issues)
30
-
Naming issues arise if the name of a database object might cause unexpected problems or violate generally accepted conventions.
28
+
-[T-SQL naming problems](#t-sql-naming-problems): Naming problems arise if the name of a database object might cause unexpected problems or violate generally accepted conventions.
Performance issues include code that might noticeably reduce the speed in which database operations are completed. Many of these issues identify code that will cause a table scan when the code is executed.
30
+
-[T-SQL performance problems](#t-sql-performance-problems): Performance problems include code that might noticeably reduce the speed of database operations. Many of these problems identify code that causes a table scan when the code runs.
34
31
35
-
:::image type="content" source="media/sql-code-analysis/static-analysis-rules.png" alt-text="Screenshot of SQL Server Data Tools project settings for code analysis rules.":::
32
+
:::image type="content" source="media/sql-code-analysis/static-analysis-rules.png" alt-text="Screenshot of SQL Server Data Tools project settings for code analysis rules.":::
36
33
37
34
Code analysis rules are extensible. You can create your own rules to enforce your own coding standards. For more information, see [Code analysis rules extensibility overview](../code-analysis-extensibility.md).
38
35
39
36
## SQL project file sample and syntax
40
37
41
-
A SQL project file can contain two properties, `RunSqlCodeAnalysis` and `SqlCodeAnalysisRules`. The `RunSqlCodeAnalysis` element specifies whether code analysis is run when the project is built. By defaultall included rules are run and rule pattern detection results in a build warning.
38
+
A SQL project file can contain two properties, `RunSqlCodeAnalysis` and `SqlCodeAnalysisRules`. The `RunSqlCodeAnalysis` element specifies whether to run code analysis when the project is built. By default, the build runs all included rules, and rule pattern detection results in a build warning.
42
39
43
40
```xml
44
41
<?xml version="1.0" encoding="utf-8"?>
@@ -53,7 +50,7 @@ A SQL project file can contain two properties, `RunSqlCodeAnalysis` and `SqlCode
53
50
...
54
51
```
55
52
56
-
The `SqlCodeAnalysisRules` element specifies the rules and their error/warning behavior. In the following example the rules Microsoft.Rules.Data.SR0006 and Microsoft.Rules.Data.SR0007 are disabled and a detection for the rule Microsoft.Rules.Data.SR0008 will result in a build error.
53
+
The `SqlCodeAnalysisRules` element specifies the rules and their error or warning behavior. In the following example, the rules `Microsoft.Rules.Data.SR0006` and `Microsoft.Rules.Data.SR0007` are disabled, and a detection for the rule `Microsoft.Rules.Data.SR0008` results in a build error.
57
54
58
55
```xml
59
56
<?xml version="1.0" encoding="utf-8"?>
@@ -69,7 +66,7 @@ The `SqlCodeAnalysisRules` element specifies the rules and their error/warning b
69
66
...
70
67
```
71
68
72
-
A file `StaticCodeAnalysis.SuppressMessages.xml`can be added to the project to suppress specific code analysis findings. The following example suppresses the warning `SR0001` for the stored procedure in the file `StoredProcedures/uspGetEmployeeManagers.sql`.
69
+
You can add a `StaticCodeAnalysis.SuppressMessages.xml`file to the project to suppress specific code analysis findings. The following example suppresses the warning `SR0001` for the stored procedure in the file `StoredProcedures/uspGetEmployeeManagers.sql`.
73
70
74
71
```xml
75
72
<?xml version="1.0" encoding="utf-8" ?>
@@ -82,95 +79,111 @@ A file `StaticCodeAnalysis.SuppressMessages.xml` can be added to the project to
82
79
83
80
## Provided rules
84
81
85
-
### T-SQL design issues
82
+
### T-SQL design problems
86
83
87
-
When you analyze the T-SQL code in your database project, one or more warnings might be categorized as design issues. You should address design issues to avoid the following situations:
84
+
When you analyze the T-SQL code in your database project, you might see one or more warnings categorized as design problems. Address these problems to avoid the following situations:
88
85
89
86
- Subsequent changes to your database might break applications that depend on it.
90
87
- The code might not produce the expected result.
91
-
- The code might break if you run it with future releases of SQL Server.
88
+
- The code breaks if you run it with future releases of SQL Server.
92
89
93
-
In general, you shouldn't suppress a design issue because it might break your application, either now or in the future.
90
+
In general, don't suppress a design problem because it might break your application, either now or in the future.
94
91
95
-
The provided rules identify the following design issues:
92
+
The provided rules identify the following design problems:
96
93
97
-
-[SR0001: Avoid SELECT * in stored procedures, views, and table-valued functions](./t-sql-design-issues.md#sr0001-avoid-select--in-stored-procedures-views-and-table-valued-functions)
98
-
-[SR0008: Consider using SCOPE_IDENTITY instead of @@IDENTITY](./t-sql-design-issues.md#sr0008-consider-using-scope_identity-instead-of-identity)
99
-
-[SR0009: Avoid using types of variable length that are size 1 or 2](./t-sql-design-issues.md#sr0009-avoid-using-types-of-variable-length-that-are-size-1-or-2)
100
-
-[SR0010: Avoid using deprecated syntax when you join tables or views](./t-sql-design-issues.md#sr0010-avoid-using-deprecated-syntax-when-you-join-tables-or-views)
101
-
-[SR0013: Output parameter (parameter) isn't populated in all code paths](./t-sql-design-issues.md#sr0013-output-parameter-parameter-isnt-populated-in-all-code-paths)
102
-
-[SR0014: Data loss might occur when casting from {Type1} to {Type2}](./t-sql-design-issues.md#sr0014-data-loss-might-occur-when-casting-from-type1-to-type2)
94
+
-[SR0001: Avoid SELECT * in stored procedures, views, and table-valued functions](t-sql-design-issues.md#sr0001-avoid-select--in-stored-procedures-views-and-table-valued-functions)
95
+
-[SR0008: Consider using SCOPE_IDENTITY instead of @@IDENTITY](t-sql-design-issues.md#sr0008-consider-using-scope_identity-instead-of-identity)
96
+
-[SR0009: Avoid using types of variable length that are size 1 or 2](t-sql-design-issues.md#sr0009-avoid-using-types-of-variable-length-that-are-size-1-or-2)
97
+
-[SR0010: Avoid using deprecated syntax when you join tables or views](t-sql-design-issues.md#sr0010-avoid-using-deprecated-syntax-when-you-join-tables-or-views)
98
+
-[SR0013: Output parameter (parameter) isn't populated in all code paths](t-sql-design-issues.md#sr0013-output-parameter-parameter-isnt-populated-in-all-code-paths)
99
+
-[SR0014: Data loss might occur when casting from {Type1} to {Type2}](t-sql-design-issues.md#sr0014-data-loss-might-occur-when-casting-from-type1-to-type2)
103
100
104
-
### T-SQL Naming Issues
101
+
### T-SQL naming problems
105
102
106
-
When you analyze the T-SQL code in your database project, one or more warnings might be categorized as naming issues. You should address naming issues to avoid the following situations:
103
+
When you analyze the T-SQL code in your database project, you might see one or more warnings categorized as naming problems. Address these problems to avoid the following situations:
107
104
108
-
- The name that you specified for an object might conflict with the name of a system object.
109
-
-The name that you specified will always need to be enclosed in escape characters (in SQL Server, '[' and ']').
110
-
- The name that you specified might confuse others who try to read and understand your code.
111
-
- The code might break if you run it with future releases of SQL Server.
105
+
- The name you specify for an object conflicts with the name of a system object.
106
+
-You must always enclose the specified name in escape characters (for example in SQL Server, `[` and `]`).
107
+
- The name you specify confuses others who try to read and understand your code.
108
+
- The code breaks if you run it with future releases of SQL Server.
112
109
113
-
In general, you might suppress a naming issue if other applications that you can't change depend on the current name.
110
+
In general, suppress a naming problem if other applications that you can't change depend on the current name.
114
111
115
-
The provided rules identify the following design issues:
112
+
The provided rules identify the following design problems:
116
113
117
-
-[SR0011: Avoid using special characters in object names](./t-sql-naming-issues.md#sr0011-avoid-using-special-characters-in-object-names)
118
-
-[SR0012: Avoid using reserved words for type names](./t-sql-naming-issues.md#sr0012-avoid-using-reserved-words-for-type-names)
119
-
-[SR0016: Avoid using sp_ as a prefix for stored procedures](./t-sql-naming-issues.md#sr0016-avoid-using-sp_-as-a-prefix-for-stored-procedures)
114
+
-[SR0011: Avoid using special characters in object names](t-sql-naming-issues.md#sr0011-avoid-using-special-characters-in-object-names)
115
+
-[SR0012: Avoid using reserved words for type names](t-sql-naming-issues.md#sr0012-avoid-using-reserved-words-for-type-names)
116
+
-[SR0016: Avoid using sp_ as a prefix for stored procedures](t-sql-naming-issues.md#sr0016-avoid-using-sp_-as-a-prefix-for-stored-procedures)
120
117
121
-
### T-SQL performance issues
118
+
### T-SQL performance problems
122
119
123
-
When you analyze the T-SQL code in your database project, one or more warnings might be categorized as performance issues. You should address a performance issue to avoid the following situation:
120
+
When you analyze the T-SQL code in your database project, you might see one or more warnings categorized as performance problems. Address a performance problem to avoid the following situation:
124
121
125
-
- A table scan will occur when the code is executed.
122
+
- A table scan occurs when you run the code.
126
123
127
-
In general, you might suppress a performance issue if the table contains so little data that a scan will not cause performance to drop significantly.
124
+
In general, you can suppress a performance problem if the table contains so little data that a scan doesn't significantly affect performance.
128
125
129
-
The provided rules identify the following design issues:
126
+
The provided rules identify the following design problems:
130
127
131
-
-[SR0004: Avoid using columns that do not have indexes as test expressions in IN predicates](./t-sql-performance-issues.md#sr0004-avoid-using-columns-that-dont-have-indexes-as-test-expressions-in-in-predicates)
132
-
-[SR0005: Avoid using patterns that start with "%" in LIKE predicates](./t-sql-performance-issues.md#sr0005-avoid-using-patterns-that-start-with--in-like-predicates)
133
-
-[SR0006: Move a column reference to one side of a comparison operator to use a column index](./t-sql-performance-issues.md#sr0006-move-a-column-reference-to-one-side-of-a-comparison-operator-to-use-a-column-index)
134
-
-[SR0007: Use ISNULL(column, default_value) on nullable columns in expressions](./t-sql-performance-issues.md#sr0007-use-isnullcolumn-default_value-on-nullable-columns-in-expressions)
135
-
-[SR0015: Extract deterministic function calls from WHERE predicates](./t-sql-performance-issues.md#sr0015-extract-deterministic-function-calls-from-where-predicates)
128
+
-[SR0004: Avoid using columns that do not have indexes as test expressions in IN predicates](t-sql-performance-issues.md#sr0004-avoid-using-columns-that-dont-have-indexes-as-test-expressions-in-in-predicates)
129
+
-[SR0005: Avoid using patterns that start with "%" in LIKE predicates](t-sql-performance-issues.md#sr0005-avoid-using-patterns-that-start-with--in-like-predicates)
130
+
-[SR0006: Move a column reference to one side of a comparison operator to use a column index](t-sql-performance-issues.md#sr0006-move-a-column-reference-to-one-side-of-a-comparison-operator-to-use-a-column-index)
131
+
-[SR0007: Use ISNULL(column, default_value) on nullable columns in expressions](t-sql-performance-issues.md#sr0007-use-isnullcolumn-default_value-on-nullable-columns-in-expressions)
132
+
-[SR0015: Extract deterministic function calls from WHERE predicates](t-sql-performance-issues.md#sr0015-extract-deterministic-function-calls-from-where-predicates)
136
133
137
134
## Enable and disable code analysis
138
135
139
-
::: zone pivot="sq1-visual-studio"
136
+
::: zone pivot="sq1-visual-studio-code"
140
137
141
-
To enable or disable SQL code analysis in Visual Studio, right-click the project in **Solution Explorer** and select **Properties**. In the **Code Analysis** tab of the properties window, select the desired code analysis settings.
138
+
To enable or disable SQL code analysis in the SQL Database Projects extension, edit the `.sqlproj` file directly or use the code analysis settings dialog.
142
139
143
-
To disable or enable a specific rule, select the rule from the table. To change the severity of a rule, select the box for **Treat Warning as Error** for that rule from the list.
140
+
### Use the Code Analysis Settings dialog in Visual Studio Code
144
141
145
-
::: zone-end
142
+
The SQL Database Projects extension provides a settings dialog to configure code analysis rules without directly editing the `.sqlproj` file.
146
143
147
-
::: zone pivot="sq1-visual-studio-sdk"
144
+
To open the Code Analysis Settings dialog, right-click your project in the **Database Projects** view and select **Code Analysis Settings**.
148
145
149
-
To enable or disable SQL codeanalysis in the SDK-style SQL projects (preview), edit the `.sqlproj` file directly. Open the `.sqlproj` file from the **Solution Explorer** view by double-clicking on the project.
146
+
:::image type="content" source="media/sql-code-analysis/code-analysis-settings-dialog.png" alt-text="Screenshot of the Code Analysis Settings dialog in Visual Studio Code showing the list of rules grouped by category.":::
150
147
151
-
From the text editor, add an element `<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>` to the first `<PropertyGroup>` block to enable code analysis. To disable code analysis change the value of the `RunSqlCodeAnalysis` element to `True` or `False` or remove the element entirely.
148
+
In the dialog, you can:
152
149
153
-
::: zone-end
150
+
-**Enable or disable code analysis on build** using the **Enable Code Analysis on Build** toggle at the top of the dialog.
151
+
-**Enable or disable a category of rules** by selecting or deselecting the checkbox next to the category name (Design, Naming, Performance).
152
+
-**Change the severity of a rule** by selecting a severity level from the dropdown list next to the rule. Available options are **Warning**, **Error**, and **None**.
153
+
-**Search for a rule** using the search bar at the top of the rules list.
154
+
-**Filter rules by severity** using the **All severities** dropdown list.
154
155
155
-
::: zone pivot="sq1-visual-studio-code"
156
+
Select **OK** to save your changes and close the dialog, or **Apply** to save without closing. Select **Reset** to revert to the default settings.
156
157
157
-
To enable or disable SQL code analysis in the SQL Database Projects extension, edit the `.sqlproj`file directly. Open the `.sqlproj` file from the **Explorer** view or by right-clicking on the project in the **Database Projects** view and selecting **Edit .sqlproj File**.
158
+
### Edit the SQL project file to modify code analysis settings
158
159
159
-
From the text editor, add an element `<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>` to the first `<PropertyGroup>` block to enable code analysis. To disable code analysis change the value of the `RunSqlCodeAnalysis` element to`True` or`False` or remove the element entirely.
160
+
From the text editor, add the `<RunSqlCodeAnalysis>True</RunSqlCodeAnalysis>`element to the first `<PropertyGroup>` block to enable code analysis. To disable code analysis, change the value of the `RunSqlCodeAnalysis` element to `False` or remove the element entirely.
160
161
161
162
::: zone-end
162
-
163
-
:::zone pivot="sq1-sql-server-management-studio"
163
+
::: zone pivot="sq1-sql-server-management-studio"
164
164
165
165
To enable or disable SQL code analysis in SQL Server Management Studio (SSMS), right-click the project in **Solution Explorer** and select **Properties**. In the **Code Analysis** tab of the properties window, select the desired code analysis settings.
166
166
167
-
To disable a specific rule or to change the severity of a rule, select the corresponding option from the dropdown list for that rule from the rule list.
167
+
To disable a specific rule or to change the severity of a rule, select the corresponding option from the dropdown list for that rule in the rule list.
168
+
169
+
::: zone-end
170
+
::: zone pivot="sq1-visual-studio"
171
+
172
+
To enable or disable SQL code analysis in Visual Studio, right-click the project in **Solution Explorer** and select **Properties**. In the **Code Analysis** tab of the properties window, select the desired code analysis settings.
173
+
174
+
To disable a specific rule or to change the severity of a rule, select the corresponding option from the dropdown list for that rule in the rule list.
175
+
176
+
::: zone-end
177
+
::: zone pivot="sq1-visual-studio-sdk"
168
178
169
-
:::zone-end
179
+
To enable or disable SQL code analysis in Visual Studio, right-click the project in **Solution Explorer** and select **Properties**. In the **Code Analysis** tab of the properties window, select the desired code analysis settings.
170
180
181
+
To disable a specific rule or to change the severity of a rule, select the corresponding option from the dropdown list for that rule in the rule list.
182
+
183
+
::: zone-end
171
184
::: zone pivot="sq1-command-line"
172
185
173
-
To override the code analysis settings in the project file, you can use the `/p:RunSqlCodeAnalysis` and `/p:SqlCodeAnalysisRules` properties with the `dotnet build` command. For example, to build with code analysis disabled:
186
+
To override the code analysis settings in the project file, use the `/p:RunSqlCodeAnalysis` and `/p:SqlCodeAnalysisRules` properties with the `dotnet build` command. For example, to build with code analysis disabled:
0 commit comments