我参与了一个数据迁移项目。当我试图将数据从一个表插入到另一个表(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. :)

其他回答

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语句中以错误的顺序指定了列。假设tableX和tableY的列名称相同,但顺序不同

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

REMARKS VARCHAR(500)
to
REMARKS VARCHAR(1000)

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

SQL Server 2019将最终返回更有意义的错误消息。

二进制或字符串数据将被截断=>错误消息增强 如果您(在生产环境中)出现了该错误,则不太容易看到该错误来自哪一列或哪一行,以及如何准确定位它。

要启用新行为,需要使用DBCC TRACEON(460)。sys.messages中的新错误文本:

SELECT * FROM sys.messages WHERE message_id = 2628

2628 -字符串或二进制数据将在表' %中被截断。*ls ',列' %.*ls '。截断值:' %.*ls '。

字符串或二进制数据将被截断:替换臭名昭著的错误8152

这个新消息也会被反向移植到SQL Server 2017 CU12(以及即将发布的SQL Server 2016 SP2 CU),但不是默认情况下。您需要启用跟踪标志460,以便在会话级别或服务器级别将消息ID 8152替换为2628。 请注意,目前即使在SQL Server 2019 CTP 2.0中也需要启用相同的跟踪标志460。在未来的SQL Server 2019版本中,默认情况下,消息2628将取代消息8152。


SQL Server 2017 CU12也支持该特性。

改进:在SQL Server 2017中用扩展信息替换“字符串或二进制数据将被截断”消息

此SQL Server 2017更新引入了一条可选消息,其中包含以下附加上下文信息。 Msg 2628,级别16,状态6,程序程序命名,行Linenumber 字符串或二进制数据将在表'%中被截断。*ls',列'%.*ls'。 截断值:'%.*ls'。 新的消息ID是2628。如果启用了跟踪标志460,此消息将替换任何错误输出中的消息8152。

db < > fiddle演示


ALTER DATABASE SCOPED CONFIGURATION VERBOSE_TRUNCATION_WARNINGS = { ON | OFF } APPLIES TO: SQL Server (Starting with SQL Server 2019 (15.x)) and Azure SQL Database Allows you to enable or disable the new String or binary data would be truncated error message. SQL Server 2019 (15.x) introduces a new, more specific error message (2628) for this scenario: String or binary data would be truncated in table '%.*ls', column'%.*ls'. Truncated value: '%.*ls'. When set to ON under database compatibility level 150, truncation errors raise the new error message 2628 to provide more context and simplify the troubleshooting process. When set to OFF under database compatibility level 150, truncation errors raise the previous error message 8152. For database compatibility level 140 or lower, error message 2628 remains an opt-in error message that requires trace flag 460 to be enabled, and this database scoped configuration has no effect.