Mapping SQL Server Database Compatibility Levels to Version Names

To check the compatibility level of a SQL Server database and match it with the corresponding SQL Server version name, you can use the following SQL query. This query retrieves the compatibility level of each database on your SQL Server instance and then uses a CASE statement to match these compatibility levels to the corresponding SQL Server version names.

SELECT 
    name AS DatabaseName,
    compatibility_level,
    version_name = 
        CASE compatibility_level
            WHEN 160 THEN 'SQL Server 2022'
            WHEN 150 THEN 'SQL Server 2019'
            WHEN 140 THEN 'SQL Server 2017'
            WHEN 130 THEN 'SQL Server 2016'
            WHEN 120 THEN 'SQL Server 2014'
            WHEN 110 THEN 'SQL Server 2012'
            WHEN 100 THEN 'SQL Server 2008/R2'
            WHEN 90 THEN 'SQL Server 2005'
            -- Add or update cases as new SQL Server versions are released
            ELSE 'Unknown/Unsupported Version'
        END
FROM sys.databases

This query works by selecting from the sys.databases system catalog view, which contains one row per database in the instance of SQL Server. The compatibility_level column reflects the compatibility level of each database. The CASE statement then translates these numeric levels into human-readable version names.

Note: This script uses compatibility levels as of SQL Server 2022. If you’re working with a newer version of SQL Server that introduces new compatibility levels, you should update the CASE statement to include these new levels and their corresponding version names.

Related Posts

Troubleshooting Missing SQL Server Statistics

Learn how to diagnose and fix missing SQL Server statistics through a practical troubleshooting guide, including step-by-step solutions and best practices.

Read more

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from The DBA Hub

Subscribe now to keep reading and get access to the full archive.

Continue reading