Skip to content

ErikEJ/SqlServer.Rules

 
 

Repository files navigation

Static Analysis Rule-sets for SQL Projects

build status latest version GitHub Sponsors

Overview

A library of SQL best practices implemented as more than 130 database code analysis rules checked at build time.

The rules can be added as NuGet packages to SQL Database projects:

  • Modern SDK-style projects: MSBuild.Sdk.SqlProj and Microsoft.Build.Sql
  • Classic .sqlproj: Legacy SSDT projects with automatic configuration through MSBuild props/targets (Visual Studio 2017+ required)

For a complete list of the current rules we have implemented see here.

Usage

The latest version is available on NuGet

dotnet add package ErikEJ.DacFX.SqlServer.Rules

Modern SDK-style Projects

You can read more about using and customizing the rules in the readme here

Classic .sqlproj Projects

The NuGet package now supports classic .sqlproj files through MSBuild props and targets. Simply add the package reference to your project:

Using PackageReference (Visual Studio 2017+):

<ItemGroup>
  <PackageReference Include="ErikEJ.DacFX.SqlServer.Rules" Version="5.0.0" />
</ItemGroup>

Using packages.config:

Install-Package ErikEJ.DacFX.SqlServer.Rules

Code analysis will automatically run during build. No manual installation of DLLs required!

For more details on classic .sqlproj support, see the investigation documentation.

Command line tool - T-SQL Analyzer CLI

This repository also contains a .NET command line tool, that uses the rule sets.

You can use it to analyze SQL scripts, or SQL Database projects, and output the results in a variety of formats, including XML, and JSON.

You can also use the tool as a MCP Server with GitHub Copilot with VS Code and Visual Studio, allowing you to get feedback on your SQL code using GitHub Copilot Chat.

The T-SQL Analyzer MCP Server supports quick installation across multiple development environments. Choose your preferred client below for streamlined setup:

Client One-click Installation MCP Guide
VS Code Install in VS Code VS Code MCP Official Guide
Visual Studio Install in Visual Studio Visual Studio MCP Official Guide

Read more in the dedicated readme file

Visual Studio extension - T-SQL Analyzer

This repository also contains a Visual Studio extension, that uses the rule sets.

You can run live analysis of your SQL Database projects in Visual Studio, and get the results in the Error List window.

Download the extension from the Visual Studio Marketplace or get the CI build

Read more in the dedicated readme file

Solution Organization

.github - GitHub actions

docs - markdown files generated from rule inspection with the DocsGenerator unit test

Solution Items - files relating to build etc.

src

  • SqlServer.Rules - This holds the rules derived from SqlCodeAnalysisRule

test

  • SqlServer.Rules.Tests - a few test to demonstrate unit testing of rules
  • TestHelpers - shared test base classes

tools

  • SqlAnalyzerCli - a command line tool to run rules against a SQL Project
  • SqlAnalyzerVsix - a Visual Studio extension to run rules against a SQL Project
  • ErikEJ.DacFX.TSQLAnalyzer - library and NuGet package for running rules against SQL scripts and reporting results. Used by SqlAnalyzerCli
  • SqlServer.Rules.Generator - a quick console app to report on all rules in a SQL Project.
  • SqlServer.Rules.Report - Library for evaluating a rule and serializing the result.

sqlprojects

  • AW - AdventureWorks schema SQL Project for rules validation
  • TestDatabase - a small SQL Database Project with some rule violations
  • TSQLSmellsTest - a SQL Database Project with some rule violations

About

130+ T-SQL static code analysis rules for SQL Database Projects

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Sponsor this project

 

Contributors

Languages

  • C# 63.2%
  • TSQL 36.0%
  • Other 0.8%