Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding COMPATIBILITY_LEVEL in SQL Server

I understood that setting a database to a COMPATIBILITY_LEVEL prior to your native one prevented features from being used. However this doesn't seem to be the case. Witness the following SQL script:

CREATE DATABASE Foo
GO
USE Foo
GO
ALTER DATABASE Foo SET COMPATIBILITY_LEVEL = 80
GO

CREATE TABLE Bar
(
    Id UNIQUEIDENTIFIER NOT NULL,
    TestNvcMax NVARCHAR (MAX) NOT NULL, -- Arrived in SQL 2005
    TestDateTime2 DATETIME2 (7) NOT NULL -- Arrived in SQL 2008
)
GO

But this table creates perfectly - any ideas? I would have thought some kind of an error message or warning would have been appropriate

like image 745
noonand Avatar asked Apr 11 '11 16:04

noonand


People also ask

Can I change compatibility level SQL Server?

You can use Transact-SQL to view or change the compatibility level of a database using SSMS or Azure Data Studio.

What is backward compatibility in SQL Server?

Microsoft SQL Server can attach databases from older versions, but not newer versions. For example, SQL Server 2008 can attach a database that was detached from the 2005 version, but not one detached from the 2012 version.

What is compatibility level in SQL Server 2016?

When you are on SQL Server 2016 or newer, using database compatibility level 130 will use CE130 by default, and will enable a number of other performance related changes. The effects of global trace flags 1117, 1118, and 2371 are enabled with database compatibility level 130.

Does SQL compatibility level affect performance?

Upgrade compatibility level degrades performance - SQL Server | Microsoft Learn.


2 Answers

BOL says:

Compatibility level provides only partial backward compatibility with earlier versions of SQL Server.

Also:

New functionality might work under older compatibility levels, but SET options might require adjustments.

I believe that is your case.

like image 161
GSerg Avatar answered Sep 28 '22 06:09

GSerg


Here you can read about the differences between compatibility level 80, 90 and 100. ALTER DATABASE Compatibility Level

Apparently new data types is not affected. I think that compatibility level is there to make SQL Server "behave" like the older version, not prevent you from doing new fancy stuff.

like image 34
Mikael Eriksson Avatar answered Sep 28 '22 04:09

Mikael Eriksson