Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String or binary data would be truncated -- Heisenberg problem

When you get this error, the first thing you ask is, which column? Unfortunately, SQL Server is no help here. So you start doing trial and error. Well, right now I have a statement like:

INSERT tbl (A, B, C, D, E, F, G)
SELECT A, B * 2, C, D, E, q.F, G
  FROM tbl
      ,othertable q
 WHERE etc etc

Note that

  • Some values are modified or linked in from another table, but most values are coming from the original table, so they can't really cause truncation going back to the same field (that I know of).
  • Eliminating fields one at a time eventually makes the error go away, if I do it cumulatively, but — and here's the kicker — it doesn't matter which fields I eliminate. It's as if SQL Server is objecting to the total length of the row, which I doubt, since there are only about 40 fields in all, and nothing large.

Anyone ever seen this before?

Thanks.

UPDATE: I have also done "horizontal" testing, by filtering out the SELECT, with much the same result. In other words, if I say

  • WHERE id BETWEEN 1 AND 100: Error
  • WHERE id BETWEEN 1 AND 50: No error
  • WHERE id BETWEEN 50 AND 100: No error

I tried many combinations, and it cannot be limited to a single row.

like image 393
harpo Avatar asked Mar 11 '10 22:03

harpo


1 Answers

Although the table had no keys, constraints, indexes, or triggers, it did have statistics, and therein lay the problem. I killed all the table's stats using this script

http://sqlqueryarchive.blogspot.com/2007/04/drop-all-statistics-2005.html

And voila, the INSERT was back to running fine. Why are the statistics causing this error? I don't know, but that's another problem...

UPDATE: This error came back even with the stats deleted. Because I was convinced that the message itself was inaccurate (there is no evidence of truncation), I went with this solution instead:

SET ANSI_WARNINGS OFF
INSERT ...
SET ANSI_WARNINGS ON

Okay, it's more of a hack than a solution, but it allows me — and hopefully someone else — to move on to other things.

like image 137
harpo Avatar answered Oct 10 '22 02:10

harpo