May 8th, 2025

Avoid T-SQL anti-patterns with the free T-SQL analysis tool

Guest Post

Erik Ejlskov Jensen is a Principal Specialist at Delegate A/S with over 30 years of experience in .NET Data development, EF Core, and Azure SQL Database/SQL Server. He is the author of the well-known EF Core Power Tools and is now introducing his latest tool T-SQL Analyzer in this blog post. Thanks Erik!

T-SQL Analyzer is a free, open-source, cross platform command line tool for identifying, and reporting the presence of anti-patterns and design issues in SQL Server T-SQL scripts. As a database developer, you can catch any potential bad practices and design problems very early in the project process – this ensures a frictionless path to deployment from development and test environments to production.

The tool evaluates your scripts with more than 140 rules for design, naming and performance issues. The rules are created by Microsoft and the SQL Server community.

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

Installation

The tool is a .NET 8 tool, and requires the .NET 8 runtime to be installed.

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

Usage

Once installed, you can start using the tool right away. Running the tool from the command prompt in any folder will analyze all .sql files in that folder and sub-folders

tsqlanalyze

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

CREATE TABLE [dbo].[DemoTable]
(
    [Id] INT NOT NULL, 
    [Sample] NCHAR(500) NOT NULL,
    [Data] NCHAR(10) NOT NULL
)

analyze image

To get help on the options available in the tool, use the help feature:

tsqlanalyze --help

For example, to analyze a single file, use this syntax:

tsqlanalyze -i C:\code\demotable.sql

Advanced usage

Let me highlight some of the more advanced options the tool supports.

Ignore rules

If you would like to ignore some of the rules, you can add a rule exclusion filter to the command line.

tsqlanalyze -i C:\code\sproc.sql -r Rules:-SqlServer.Rules.SRD0005;-Smells.*

This will cause the tool to ignore the SRD0005 rule and any rules in the Smells rule set.

Format script

The tool can also help you format your script, with the -f option.

tsqlanalyze -i C:\code\sproc.sql -f

The available formatting options can be customized in an .editorconfig file.

And much more…

There are multiple other advanced options, including the ability to analyze a .dacpac file or a live database, output analysis results as xml, integration with SQL Server management Studio and Visual Studio.

You can also bring your own rules and use the analysis engine from your own code! Read more the the readme file for the tool.

VS Code MCP Server (preview)

You can use the tool to ask GitHub Copilot analyze your SQL Server CREATE scripts in Visual Studio Code, just add the built-in MCP server to your VS Code configuration.

Paste this link in you browser to prompt VS Code to configure the MCP Server.

vscode:mcp/install?%7B%22name%22%3A%22tsqlanalyzer%22%2C%22command%22%3A%22tsqlanalyze%22%2C%22args%22%3A%5B%22-mcp%22%5D%7D

or install manually:

{
    "servers": {
        "tsqlanalyzer": {
            "type": "stdio",
            "command": "tsqlanalyze",
            "args": [
                "-mcp"
            ]
        }
    }
}

You can now ask GitHub Copilot in Agent mode to help you check your SQL script for bad practices.

You will get the results presented by Copilot, who will even offer to fix the script for you!

image image

Parting words

I hope you will find this free tool to be a useful addition to your developer toolbox. Please reach out via the GitHub repository with and feedback and ideas you may have, and happy SQL coding.

Author

Erik Ejlskov Jensen (@ErikEJ) has a passion for making the lives of both .NET database devlopers and database administrators better, with tools, blog post and tips sharing on social media, that helps teams build better data driven solutions. He has been a Microsoft MVP since 2009, and works a Master as Delegate A/S in Denmark.

2 comments