开发者

Why do I get "String or binary data would be truncated" in some cases only?

I'm troubleshooting a problem of SQL Server error message 8152 "String or binary data would be truncated" in some scenarios only. The following query is similar to that one, that is throwing the error.

CREATE TABLE SourceValues (
    SourceId INT IDENTITY (1,1),
    SourceValue VARCHAR(3)
)
GO
INSERT INTO SourceValues (SourceValue) VALUES ('aaa')
INSERT INTO SourceValues (SourceValue) VALUES ('aab')
INSERT INTO SourceValues (SourceValue) VALUES ('aa')
INSERT INTO SourceValues (SourceValue) VALUES ('ab')
INSERT INTO SourceValues (SourceValue) VALUES ('a')
INSERT INTO SourceValues (SourceValue) VALUES ('b')
GO

PRINT 'NOT WORKING #1'
CREATE TABLE TargetValues (TargetValue VARCHAR(2))
INSERT INTO TargetValues (TargetValue) 
SELECT s1.SourceValue
FROM SourceValues s1, SourceValues s2
WHERE s1.SourceId=s2.SourceId+1 AND s1.SourceValue!='aab'
DROP TABLE TargetValues
GO

PRINT 'NOT WORKING #2'
CREATE TABLE TargetValues (TargetValue VARCHAR(2))
INSERT INTO TargetValues (TargetValue) 
SELECT s1.SourceValue
FROM SourceValues s1, SourceValues s2
WHERE s1.SourceId=s2.SourceId+1 AND s1.SourceValue!='aab'
ORDER BY s1.SourceValue
DROP TABLE TargetValues
GO

PRINT 'WORKING #1'
CREATE TABLE TargetValues (TargetValue VARCHAR(2))
INSERT INTO TargetValues (TargetValue) 
SELECT s1.SourceValue
FROM SourceValues s1, SourceValues s2
WHERE s1.SourceId=s2.SourceId+1 AND s1.SourceValue!='aab'
ORDER BY s2.SourceValue -- <-- using s2 instead of s1 for order
DROP TABLE TargetValues
GO

PRINT 'WORKING #2'
CREATE TABLE TargetValues (TargetId INT IDENTITY (1,1),TargetValue VARCHAR(2)) -- <-- using identity column
INSERT INTO TargetValues (TargetValue) 
SELECT s1.SourceValue 
FROM SourceValues s1, SourceValues s2 
WHERE s1.SourceId=s2.SourceId+1 AND s1.SourceValue!='aab'
DROP TABLE Targe开发者_运维技巧tValues
GO

DROP TABLE SourceValues

The problem occured in query 'NOT WORKING 1', the others are some thoughts about solutions. Does anyone know about the differences between the not working queries and working queries?

I tested this on SQL Server 2005, SQL Server 2008 and SQL Server 2008 R2 and came to the same results. But I heard that all queries failed on another SQL Server 2008 R2 instance.

Please notice also, that I have solved this issue already by setting varchar in TargetValues table to size of 3 (corrected the error).


It's when the data would overflow the length of the field eg abc into char(2)

You have aaa in your source data

It can't be switched off or silently truncated

See SQL Server silently truncates varchar's in stored procedures for more then you probably ever wanted to know

Edit:

This is odd behaviour

This still fails with an explicit JOIN and breaks one of the "working" queries

SELECT s1.SourceValue FROM SourceValues s1 JOIN SourceValues s2 On s1.SourceId=s2.SourceId+1 WHERE s1.SourceValue<>'aab' order by s2.SourceValue,s1.SourceValue

I can't find anything on MS Connect about this


If i'm right you try to add 'aaa' to a varchar(2) field. Thats not possible because the data would be truncated.


SQL server knows the schemas are different. you are still selecting a VARCHAR(3) column ultimately into a VARCHAR(2) receiving column, despite the where clause. CAST/CONVERT the resulting select into varchar(2) and it will work. Its probably not best practice to have the resulting type linked like this to the where clause though - the where clause could return a 3 letter column at some later date (depends on your data of course) and it would break it, but an explicit CAST/CONVERT in the select would resolve this specific problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜