T-SQL Analyzer is a free, open-source new command line tool for identifying, and reporting the presence of anti-patterns in SQL Server T-SQL scripts.

It evaluates more than 140 rules for design, naming and performance issues.

If you already maintain your SQL scripts in a SQL Database project, use build analysis as described in my blog post here.

This tool is for ad-hoc, commandline based analysis of individual scripts.

Getting started

The tool runs on any system with the .NET 8.0 runtime installed.

Installing the tool

dotnet tool install --global ErikEJ.DacFX.TSQLAnalyzer.Cli

Usage

# Analyze all .sql scripts in current folder and sub-folders
tsqlanalyze

## Analyze a single file
tsqlanalyze -i C:\scripts\sproc.sql

## Analyze a folder
tsqlanalyze -i "c:\database scripts"

## Analyze a folder with a filter and a full folder path
tsqlanalyze -i c:\database_scripts\sp_*.sql "c:\old scripts"

## Analyze a script with a rule settings filter and for a specific SQL Server version
tsqlanalyze -i C:\scripts\sproc.sql -r Rules:-SqlServer.Rules.SRD0004 -s SqlAzure

## Analyze a .dacpac
tsqlanalyze -i C:\scripts\Chinook.dacpac

## Analyze a live database
tsqlanalyze -c "Data Source=.\SQLEXPRESS;Initial Catalog=Chinook;Integrated Security=True;Encrypt=false"

You can exclude individual rules and rule categories with a "Rules:" statement, you can read more about the syntax for this here.

You can specify that your script targets a particular SQL Server version, using a SQL Server version enumeration value as documented here.

Sample output

The tool will output a summary of the rules that were violated, and the line numbers where the violations occurred.

CREATE TABLE [dbo].[Table3]
(
    [Id] INT NOT NULL, 
    [Wang] NCHAR(500) NOT NULL,
    [Chung] NCHAR(10) NOT NULL
)

Feedback

If you encounter any issue with the tool, or have suggestions, please create an issue here.