By: Greg Robidoux | Updated: 2024-05-30 | Comments (7) | Related: > Database Configurations
Problem
When upgrading databases from an older version of SQL Server using either the backup and restore method or detach and attach method the compatibility level does not automatically change and therefore your database still acts as though it is running using an earlier version of SQL Server. From an overall standpoint this is not a major problem, but there are certain features that you will not be able to take advantage of unless your database compatibility level is changed. This tip will show you how to check the current compatibility level, how to change the compatibility level.
Solution
The first thing that you need to do is to check the compatibility level that your database is running under. As mentioned above, any database that is upgraded using the backup and restore or detach and attach method will not change the compatibility level automatically, so you will need to check each database and make the change.
Each version of SQL Server has an internal number that differs from the version name. Here is a list of the compatibility levels and versions that you will see:
- 60 = SQL Server 6.0
- 65 = SQL Server 6.5
- 70 = SQL Server 7.0
- 80 = SQL Server 2000
- 90 = SQL Server 2005
- 100 = SQL Server 2008
- 110 = SQL Server 2012
- 120 = SQL Server 2014
- 130 = SQL Server 2016
- 140 = SQL Server 2017
- 150 = SQL Server 2019
- 160 = SQL Server 2022
Identifying Database Compatibility Level
To check the compatibility level of your databases you can use one of these methods:
Using SQL Server Management Studio, right click on the database, select "Properties" and look at the "Options" page for each database as the following image shows:
Another option is to use sp_helpdb so you can get the information for all databases at once:
EXEC sp_helpdb
Or select directly from the sys.databases catalog to get the information for all databases.
SELECT * FROM sys.databases
Compatibility Level for New Databases
When issuing a CREATE DATABASE statement there is not a way to select which compatibility level you want to use. The compatibility level that is used is the compatibility level of your model database.
Here is a sample CREATE DATABASE command, but as you can see there is not an option to change the compatibility level.
CREATE DATABASE [test] ON PRIMARY ( NAME = N'test', FILENAME = N'Z:\SQLData\test.mdf' , SIZE = 2048KB , FILEGROWTH = 1024KB ) LOG ON ( NAME = N'test_log', FILENAME = N'Y:\SQLData\test3_log.ldf' , SIZE = 3072KB , FILEGROWTH = 10%) GO
When creating a database using SQL Server Management Studio you have the ability to change the compatibility level on the "Options" tab such as follows:
If we use the "Script" option we can see that SQL Server issues the CREATE DATABASE statement and then issues a "ALTER DATABASE" to set the database compatibility level as shown below.
CREATE DATABASE [test] ON PRIMARY ( NAME = N'test', FILENAME = N'Z:\SQLData\test.mdf' , SIZE = 2048KB , FILEGROWTH = 1024KB ) LOG ON ( NAME = N'test_log', FILENAME = N'Y:\SQLData\test3_log.ldf' , SIZE = 3072KB , FILEGROWTH = 10%) GO ALTER DATABASE [test] SET COMPATIBILITY_LEVEL = 80 GO
Changing Compatibility Level
So once you have identified the compatibility level of your database and know what you want to change it to, you can use the ALTER DATABASE command to make the change or you can use SSMS and right click on Properties for the database and use the Options page. There is also an older way to do this using sp_dbcmptlevel, but you should use ALTER DATABASE.
The SQL command to make the changes has the following syntax:
ALTER DATABASE [test] SET COMPATIBILITY_LEVEL = 100 GO
Compatibility Differences
There are several differences on how compatibility levels impact your database operations. Sometimes there are just new features that are added to SQL Server that you won't be able to use unless you update the compatibility level and there are also changes that impact how SQL Server processes and optimizes queries. Just because you upgrade to a later version doesn't mean that you are using all of the latest features, so be sure to check your database compatibility levels after upgrading or migrating a database.
Summary
The compatibility level setting is used by SQL Server to determine how certain features should be handled. This was setup so you could migrate your databases to a later release of SQL Server without having to worry about the application breaking. This setting can be changed forward and backwards if needed, so if you do change your compatibility level and find that there are problems you can set the value back again until you resolve all of the issues that you may be facing during the upgrade.
In addition, there are certain features that only work if the database is set to the latest compatibility level, therefore to get all of the benefits of the version of SQL Server you are running you need to make sure you are using the latest compatibility level.
Next Steps
- Check your database compatibility level. I have seen many databases that have been migrated to later versions of SQL Server, but the compatibility level has not been changed.
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips
Article Last Updated: 2024-05-30