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

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

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


当前回答

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. :)

其他回答

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

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

SELECT *
INTO TEMP_TABLE
FROM SOURCE_TABLE;

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

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

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

希望这能有所帮助。

请尝试以下代码:

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]

是的,我也面临着这样的问题。

REMARKS VARCHAR(500)
to
REMARKS VARCHAR(1000)

在这里,我将备注文件的长度从500更改为1000

在Acumatica ERP中,我在导入订单时得到了相同的错误。

字符串或二进制数据将被截断在表'MyDatabase.dbo。ARInvoice',列'InvoiceNbr'。截断值“Something”。

在做了这个链接中描述的步骤如何修复字符串或二进制截断错误后,我得到了另一个错误“数量将变成负数”,这是通过在收据屏幕中创建一些数量来解决的。

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