SQL Server – Identify Column Causing Truncation Error in Insert Into

sql serversql server 2014stored-procedurest-sql

I have a stored procedure that inserts 650 fields into a table. The insert is failing with a truncation error.

It's a simple

INSERT INTO
SELECT (a bunch of fields) 
FROM (a bunch of tables)

Below is the error message:

Msg 8152, Level 16, State 14, Procedure DSP_Procedure, Line
1075 String or binary data would be truncated.

Is there a quick way that I can identify what field is causing the truncation error?

The fact that the select statement to be inserted into the table has 650 fields makes it difficult to pinpoint which field is causing the truncation error.

I am thinking I can maybe comment out blocks of fields at a time so as to only have the SP insert 100 fields at a time and then run the SP 6 or 7 different times until i can at least narrow down to a group of 100 fields that will contain the field that is causing the truncation error.

Alternatively I am thinking that maybe I can just SELECT INTO a new table and then compare the data lengths in the table vs the data lengths of the target table I am trying to insert into in my SP to see which field contains a longer than expected field length…

I am using SQL Server 2014.

Any easier alternatives?

Best Answer

If you are on SQL Server 2016 (SP2, CU6 or newer), one option is to turn on trace flag 460 e.g. (QUERYTRACEON 460). The output will indicate the column and offending data.

See this article for details. https://www.brentozar.com/archive/2019/03/how-to-fix-the-error-string-or-binary-data-would-be-truncated/

If you don't care about the truncation you can use SET ANSI_WARNINGS OFFto ignore that type of truncation.