string or binary data would be truncated - WHICH FIELD?

THis is a highly annoying SQL error, especially since i have a huge SQL statement in a table with 30 different columns.  how do I know which field is getting kicked out without having to dissect every element of my statement?
arthurh88Asked:
Who is Participating?
 
Scott PletcherSenior DBACommented:
Yes, IT REALLY IS ANNOYING.  I too think SQL should specify the column(s) ... but, as we know, it doesn't.


If it's an INSERT (95+% of the time), what I typically do is create the structure in tempdb, but change every string/binary column to varchar|bin(max).

INSERT INTO tempdb.dbo.test_table
SELECT ...
FROM ...

Then, after it's run, do a single query to get the MAX(LEN( of every column, and compare that to the max lengths of the original/output data.  That allows you to identify all over-length columns at the same time.

For example, something like this:


USE maindb

SELECT
    CASE c.name WHEN 'columnA' THEN [columnA] WHEN 'columnB' THEN [columnB] ... END AS max_length,
    CASE WHEN c.max_length = -1 THEN 'ok'
        WHEN c.name = 'columnA' THEN [columnA]
        WHEN c.name = 'columnB' THEN [columnB]
        ...
    END > c.max_length THEN 'TOO LONG!' ELSE 'ok' END AS check_length,
    c.*
FROM (
    SELECT
        MAX(LEN(columnA)) AS columnA, MAX(LEN(columnB)) AS columnB,
        MAX(LEN(columnC)) AS columnC, --, ...
    FROM tempdb.dbo.test_table
) AS tt
CROSS JOIN maindb.sys.columns c ON
    c.object_id = OBJECT_ID('dbo.table_name')
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
No clue, but the way I typicall troubleshoot it is to have the table design of the source table in one window, and the table design of the destination table in another window, and compare column-by-column.

> how do I know which field is getting kicked
truncated just means characters to the right are not being inserted/updated.
0
 
Tony303Commented:
So this is an SSIS package? Or an import wizard, which is the same anyway? Or are you using Bulk load?

And what is the error message in it's entirity?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Aneesh RetnakaranDatabase AdministratorCommented:
try commenting out few column names from source and destinations and run the statement.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
<posting this just in case it's not clear>

So if your source has a column that is a varchar(10), and your destination column that you're trying to insert/update is a varchar(5), the warning message is saying that you are at risk of losing the five characters to the right, regardless of whether it's populated or not.

So, if your source varchar(10) contains 'abcdefg'
What will be inserted to the destination varchar(5) is 'abcde', with the warning message.
0
 
Anthony PerkinsCommented:
THis is a highly annoying SQL error, especially since i have a huge SQL statement in a table with 30 different columns.  how do I know which field is getting kicked out without having to dissect every element of my statement?
What do you propose?  Are you suggesting that SQL Server list all the columns that could be truncated?  If that is the case, then I am afraid for a number of reasons that is not going to happen.
0
 
UnifiedISCommented:
It would be nice if it told you the column but unfortunately, it doesn't.  You will have to do some manual investigation.  
At least as a takeaway, you can understand the importance of properly constraining the data in the source so it stays at or below the column limit in your database.
0
 
tliottaCommented:
The column(s) at risk are the ones that are longer than the target columns. How can you not know the sizes of your columns? And if you are generating values, are you defining their lengths when you generate them? If not, then there might not be a way to tell without creating a new query that generates the same values and shows their lengths.

Tom
0
 
arthurh88Author Commented:
glad to know someone relates!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.