Performing Static Analysis

Set active analysis rules

To configure which rules will be active by default, do the following:

1. Open SQL Enlight > Options dialog and go to the Analysis > Rules tab.

analysis-rules-1

2. Set the checked state of each rule which you want to be active by default.

3. Optionally set default values to rule parameters.

Analyze a selection of T-SQL code

SQL code commands

analysis-main-menu
Note Note

The static analysis commands are enabled only when there is an active code window as they are applied on the SQL code inside the current code editor.

There are two main analysis commands available under SQL Enlight -> Static Analysis, and SQL Editor’s context menu:

sql-editor-context-menu
  • The Analyze with All Rules command can be used to analyze with all default active rules.

  • The Analyze with Specific Rules… command can be used to select rules which to be applied.

    The command will show the following rule selector dialog:

    analysis-rules-selector

    Use the Show Details button to show rule details – see rule description and set rule parameters.

    analysis-rules-parameters-selector

The commands will analyze the selected SQL code or the entire active document if there is no current selection.

Both commands are available on the SQL Enlight menu, SQL Enlight toolbar and SQL Editor’s context menu.

Note Note

By default, only Batch scoped analysis rules will be applied when analyzing T-SQL documents using the Analyze with All Rules command.

Explicit rules

Explicit analysis rules are directly available in the Static Analysis sub-menu.

These rules are managed in the Explicit Rules analysis group and due to their kind are available only to be used separately from the general rules.

analysis-rules-explicit-1
Analyze database stored procedures, functions, and triggers in SQL Server Management Studio

1. Right-click an object in SQL Server Management Studio’s Object Explorer to open the context menu.

analyze-4

2. Use the Static Analysis menu to select SQL Enlight command, specific group, or rule. The menu will be available If the selected object or one of its children can be analyzed.

3. If the Analyze with Specific Rules… command is used, specific rules can be selected prior to running the analysis.

4. The command will script the stored procedures, functions, and triggers under the selected object in a new query window and will analyze the resulting script.

Note Note

By default only Batch scoped analysis rules will be applied when analyzing T-SQL document using the Analyze with All Rules command.

Analyze SQL files in Solution Explorer

1. Open Solution Explorer in SQL Server Management Studio or Visual Studio.

2. Right-click the target Solution Explorer item to open its context menu. It can be a solution, project, folder which contains SQL file(s), or a single SQL file.

3. To start the analysis use the SQL Enlight context menu item and select one of Analyze with All Rules or Analyze with Specific Rules… commands.

This way all SQL files under the selected Solution Explorer items will be analyzed and the generated rule violations will be added to the SQL Enlight Error List.

Instant Code Analysis

Instant Code Analysis feature provides real-time analysis and code issues feedback in the editors of SQL Server Management Studio and Visual Studio.

The analysis is fired a few seconds after a script document is opened for the first time or an opened document has its content changed.

The document is analyzed in the background using the rules in the current analysis template.

The code elements that trigger violations are highlighted in the editor and a complete list of file code issues is added and displayed in the SQL Enlight Error List tool window.

The Details pane of the Error List shows more information about the source and explains the nature of the issue.

The feature can be configured and turned on/off using SQL Enlight Settings, SQL Enlight toolbar, and SQL Enlight main menu.

Analysis Result

If there are T-SQL script analysis rules violations, the SQL Enlight Error List Window will be displayed and the violations will be marked in the text editor.

analyze-3

See SQL Enlight Error List for more details.

Automatic Fix

Several of the analysis rules can be fixed automatically using the Fix context menu command of SQL Enlight Error List.

error-list-fix-issues

When a code rule issue is fixed, the fix is applied in the SQL document where the rule issue is found.
As for issues of database rules, a change script is written in a new document that is common for all database issue fixes.

error-list-fix-issues-script

The issues that are successfully fixed are crossed out in the SQL Enlight Error List.

See Also