Msg 8152 Still Showing in SQL Server 2019: Check Compatibility Level

Msg 8152 still shows up in SQL Server 2019 for some databases, and the compatibility level decides why. I covered the new message in String or Binary Data Would Be Truncated: Find the Column With Msg 2628.

Gouache painting of a small cream wooden panel with two toggle switches on a plaster wall, one flipped up with a vermilion tag and one flipped down

The Error

This is the old message:

Msg 8152, Level 16, State 30, Line 15
String or binary data would be truncated.
The statement has been terminated.

What the Reader Sent

The release notes say Microsoft improved this error in SQL Server 2019. I get questions about it in my Comprehensive Database Performance Health Check.

A reader had migrated from SQL Server 2017 to SQL Server 2019 and couldn’t see the improvement. They sent me this script and a screenshot. The script drops and recreates a database named TestTruncationMessage, so run it only where that’s fine.

SET NOCOUNT ON
GO
SELECT SERVERPROPERTY('ProductVersion')
GO
USE master
GO
DROP DATABASE IF EXISTS TestTruncationMessage
GO
Create database TestTruncationMessage
GO
use TestTruncationMessage
GO
CREATE TABLE MyTable (i INT, j VARCHAR(10))
GO
INSERT INTO MyTable VALUES (1,'This is more than 10 characters')
GO
USE [TestTruncationMessage]
GO
select * from sys.database_scoped_configurations where name = 'VERBOSE_TRUNCATION_WARNINGS'
GO

SSMS query window with the reader's script and its results: SQL Server version 15.0.2070.41 labeled SQL Server 2019, Msg 8152 string or binary data would be truncated, and the setting VERBOSE_TRUNCATION_WARNINGS with value 1.

The reader was on SQL Server 2019, and the setting VERBOSE_TRUNCATION_WARNINGS was on. The message was still the old one.

Why It Happens

The documentation for ALTER DATABASE SCOPED CONFIGURATION explains it. In SQL Server 2019, Msg 2628 is the default under database compatibility level 150. You can go back to Msg 8152 with the setting VERBOSE_TRUNCATION_WARNINGS, or with a lower compatibility level.

I checked the reader’s database. Its compatibility level was 140, because it was an upgrade from SQL Server 2017.

The Fix

To get Msg 2628, the database needs compatibility level 150. Test your application before you raise it. This script shows the difference between the two levels:

ALTER DATABASE [TestTruncationMessage] SET COMPATIBILITY_LEVEL = 140
GO
INSERT INTO MyTable VALUES (1,'This is more than 10 characters')
GO
ALTER DATABASE [TestTruncationMessage] SET COMPATIBILITY_LEVEL = 150
GO
INSERT INTO MyTable VALUES (1,'This is more than 10 characters')
GO

SSMS query window with the compatibility level script. At level 140 the insert returns Msg 8152. At level 150 it returns Msg 2628, naming table TestTruncationMessage.dbo.MyTable, column j, and the truncated value.

The compatibility level changes the message. The database scoped configuration controls it too. Here is a quick demonstration:

SELECT DATABASEPROPERTYEX('TestTruncationMessage', 'CompatibilityLevel') 'CurrentCompatibilityLevel'
GO
USE [TestTruncationMessage]
GO
ALTER DATABASE SCOPED CONFIGURATION SET VERBOSE_TRUNCATION_WARNINGS = OFF;
GO
INSERT INTO MyTable VALUES (1,'This is more than 10 characters')
GO
USE [TestTruncationMessage]
GO
ALTER DATABASE SCOPED CONFIGURATION SET VERBOSE_TRUNCATION_WARNINGS = ON;
GO
INSERT INTO MyTable VALUES (1,'This is more than 10 characters')
GO

SSMS query window with the scoped configuration script. The compatibility level is 150. With the setting OFF the insert returns Msg 8152. With the setting ON it returns Msg 2628 with the table, column and truncated value.

With the setting OFF you get Msg 8152. With it ON, at compatibility level 150, you get Msg 2628.

How to Prevent It

After an upgrade, check the compatibility level of your databases. A database upgraded from SQL Server 2017 keeps level 140 until you change it.

Let me know if this post was useful. Leave your feedback in the comments, and I’ll reply.

Quick reference card: Why 8152 Still Appears

Msg 8152 is not a missing feature, it is a database still running at level 140.

Published by Pinal Dave on SQLAuthority. More of my work at pinaldave.com.


Discover more from SQL Authority with Pinal Dave

Subscribe to get the latest posts sent to your email.

Compatibility Level, SQL Error Messages, SQL Scripts, SQL Server 2019, SQL String
Previous Post
Setup Error 1603: Patch Fails With Not Enough Disk Space on the Volume
Next Post
Error 15141: Upgrade Fails Because a Login Owns an Availability Group

Related Posts

2 Comments. Leave new

  • Hi Pinal,

    I used last aproach and msg 8152 still comes when VERBOSE_TRUNCATION_WARNINGS is ON or OFF.

    SELECT DATABASEPROPERTYEX(‘DBNAME’, ‘CompatibilityLevel’) ‘CurrentCompatibilityLevel’
    GO
    USE [DBNAME]
    GO
    ALTER DATABASE SCOPED CONFIGURATION SET VERBOSE_TRUNCATION_WARNINGS = OFF;
    GO
    INSERT INTO [dbo].[Logs_Test]([CustomerName])VALUES(‘7n0yv40tc23c*y209nc32vc0n2*’);
    GO
    USE [DBNAME]
    GO
    ALTER DATABASE SCOPED CONFIGURATION SET VERBOSE_TRUNCATION_WARNINGS = ON;
    GO
    INSERT INTO [dbo].[Logs_Test]([CustomerName])VALUES(‘7n0yv40tc23c*y209nc32vc0n2*’);
    GO

    CurrentCompatibilityLevel
    140

    Msg 8152, Level 16, State 30, Line 16
    String or binary data would be truncated.
    The statement has been terminated.
    Msg 8152, Level 16, State 30, Line 22
    String or binary data would be truncated.
    The statement has been terminated.

    Reply
  • Also want to ask upgrading my CompatibilityLevel from 140 to 150 can cause any problem?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.