Skip to content

Commit 7b56702

Browse files
Iqra617Iqra Shaikh
andauthored
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>
1 parent 826e94b commit 7b56702

2 files changed

Lines changed: 74 additions & 61 deletions

File tree

23.1 KB
Loading

docs/tools/sql-database-projects/concepts/sql-code-analysis/sql-code-analysis.md

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Analyze database code to improve code quality.
44
author: rwestMSFT
55
ms.author: randolphwest
66
ms.reviewer: drskwier
7-
ms.date: 03/11/2026
7+
ms.date: 04/07/2026
88
ms.service: sql
99
ms.subservice: sql-database-projects
1010
ms.topic: concept-article
@@ -19,26 +19,23 @@ zone_pivot_groups: sq1-sql-projects-tools
1919

2020
[!INCLUDE [SQL Server Azure SQL Database Azure SQL Managed Instance FabricSQLDB](../../../../includes/applies-to-version/sql-asdb-asdbmi-fabricsqldb.md)]
2121

22-
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).
2323

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:
2525

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.
2827

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.
3129

32-
- [T-SQL Performance Issues](#t-sql-performance-issues)
33-
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.
3431

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.":::
3633

3734
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).
3835

3936
## SQL project file sample and syntax
4037

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 default all 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.
4239

4340
```xml
4441
<?xml version="1.0" encoding="utf-8"?>
@@ -53,7 +50,7 @@ A SQL project file can contain two properties, `RunSqlCodeAnalysis` and `SqlCode
5350
...
5451
```
5552

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.
5754

5855
```xml
5956
<?xml version="1.0" encoding="utf-8"?>
@@ -69,7 +66,7 @@ The `SqlCodeAnalysisRules` element specifies the rules and their error/warning b
6966
...
7067
```
7168

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`.
7370

7471
```xml
7572
<?xml version="1.0" encoding="utf-8" ?>
@@ -82,95 +79,111 @@ A file `StaticCodeAnalysis.SuppressMessages.xml` can be added to the project to
8279

8380
## Provided rules
8481

85-
### T-SQL design issues
82+
### T-SQL design problems
8683

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:
8885

8986
- Subsequent changes to your database might break applications that depend on it.
9087
- 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.
9289

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.
9491

95-
The provided rules identify the following design issues:
92+
The provided rules identify the following design problems:
9693

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)
103100

104-
### T-SQL Naming Issues
101+
### T-SQL naming problems
105102

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:
107104

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.
112109

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.
114111

115-
The provided rules identify the following design issues:
112+
The provided rules identify the following design problems:
116113

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)
120117

121-
### T-SQL performance issues
118+
### T-SQL performance problems
122119

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:
124121

125-
- A table scan will occur when the code is executed.
122+
- A table scan occurs when you run the code.
126123

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.
128125

129-
The provided rules identify the following design issues:
126+
The provided rules identify the following design problems:
130127

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)
136133

137134
## Enable and disable code analysis
138135

139-
::: zone pivot="sq1-visual-studio"
136+
::: zone pivot="sq1-visual-studio-code"
140137

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.
142139

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
144141

145-
::: zone-end
142+
The SQL Database Projects extension provides a settings dialog to configure code analysis rules without directly editing the `.sqlproj` file.
146143

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**.
148145

149-
To enable or disable SQL code analysis 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.":::
150147

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:
152149

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.
154155

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.
156157

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
158159

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.
160161

161162
::: zone-end
162-
163-
:::zone pivot="sq1-sql-server-management-studio"
163+
::: zone pivot="sq1-sql-server-management-studio"
164164

165165
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.
166166

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"
168178

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.
170180

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
171184
::: zone pivot="sq1-command-line"
172185

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:
174187

175188
```bash
176189
dotnet build /p:RunSqlCodeAnalysis=False

0 commit comments

Comments
 (0)