I have a huge INSERT INTO TABLE1 (....) SELECT .... FROM TABLE2
statement. It gives me the error
"String or binary data would be truncated".
I know that one of the columns from TABLE2 is way bigger for one column from TABLE1 in the INSERT
statement.
I have more than 100 columns in each table. So it is hard to find out the problem. Is there any easier way to figure this out?
To fix this error, patch to SQL Server 2016 SP2, CU6 or newer (including SQL Server 2017), and then turn on trace flag 460. You can enable it at the query level or at the server level.
The "String or binary data would be truncated" error indicates that the procedure is attempting to store something in the DBServerInfo table that is larger than the column allows. The two known reasons this can occur are: SQL Server has at least one database whose name exceeds 25 characters in length.
How to fix “String or binary data would be truncated” The main reason behind this error is the more amount of data that we are trying to store in a column than a specific column can store. So a quick solution to solve this error is by increase the column size.
"String or binary data would be truncated." The "String or binary data would be truncated" error occurs when the value persisted in a field is higher (in character count) than the one the database column max value allows.
You can query Information_Schema.Columns
for both tables and check the difference in content length.
Assuming your tables have the same column names, you can use this:
SELECT t1.Table_Name, t1.Column_Name FROM INFORMATION_SCHEMA.Columns t1 INNER JOIN INFORMATION_SCHEMA.Columns t2 ON (t1.Column_Name = t2.Column_Name) WHERE t1.Table_Name = 'Table1' AND t2.Table_Name = 'Table2' AND ISNULL(t1.Character_maximum_length, 0) < ISNULL(t2.Character_maximum_length, 0)
Assuming your tables have different column names, you can do this and just look for the difference
SELECT Table_Name, Column_Name, Character_maximum_length FROM INFORMATION_SCHEMA.Columns WHERE Table_Name IN('Table1', 'Table2') ORDER BY Column_Name, Character_maximum_length, Table_Name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With