我参与了一个数据迁移项目。当我试图将数据从一个表插入到另一个表(SQL Server 2005)时,我得到以下错误:

编号8152,16层,状态13,1线 字符串或二进制数据将被截断。

源数据列与数据类型匹配,并且在目标表列的长度定义内,因此我不知道是什么原因导致了这个错误。


当前回答

我也遇到过类似的问题。我将数据从一个表复制到一个完全相同的表,除了名称。

最后,我使用SELECT into语句将源表转储到临时表中。

SELECT *
INTO TEMP_TABLE
FROM SOURCE_TABLE;

我比较了源表和临时表的模式。我发现其中一列是varchar(4000),而我期待的是varchar(250)。

更新: 如果你感兴趣,varchar(4000)问题可以在这里解释:

对于Nvarchar(Max),我只能在TSQL中获得4000个字符?

希望这能有所帮助。

其他回答

One other potential reason for this is if you have a default value setup for a column that exceeds the length of the column. It appears someone fat fingered a column that had a length of 5 but the default value exceeded the length of 5. This drove me nuts as I was trying to understand why it wasn't working on any insert, even if all i was inserting was a single column with an integer of 1. Because the default value on the table schema had that violating default value it messed it all up - which I guess brings us to the lesson learned - avoid having tables with default value's in the schema. :)

对于其他的,也检查您的存储过程。在我的例子中,在我的存储过程CustomSearch中,我不小心声明了我的列没有足够的长度,所以当我输入一个大数据时,我收到了这个错误,尽管我的数据库中有很大的长度。我只是在自定义搜索中改变了列的长度错误就消失了。这只是为了提醒大家。谢谢。

面对同样的问题。

日志含义源表中的列长度大于目标表中的列长度 源列长度- 50和 目标列长度- nvarchar(25),并将其增加到nvarchar(50),并且它工作

请尝试以下代码:

CREATE TABLE [dbo].[Department](
    [Department_name] char(10) NULL
)

INSERT INTO [dbo].[Department]([Department_name]) VALUES  ('Family Medicine')
--error will occur

 ALTER TABLE [Department] ALTER COLUMN [Department_name] char(50)

INSERT INTO [dbo].[Department]([Department_name]) VALUES  ('Family Medicine')

select * from [Department]

问题很简单:源查询中的一个或多个列包含的数据超过了目标列的长度。一个简单的解决方案是使用源查询并在每一列上执行Max(Len(source col))。也就是说,

Select Max(Len(TextCol1))
    , Max(Len(TextCol2))
    , Max(Len(TextCol3))
    , ...
From ...

然后将这些长度与目标表中的数据类型长度进行比较。至少有一个超出了其目标列的长度。

如果你绝对肯定情况不应该是这样,也不关心是不是这样,那么另一个解决方案是强制强制转换源查询列到它们的目标长度(这将截断任何太长的数据):

Select Cast(TextCol1 As varchar(...))
    , Cast(TextCol2 As varchar(...))
    , Cast(TextCol3 As varchar(...))
    , ...
From ...