我参与了一个数据迁移项目。当我试图将数据从一个表插入到另一个表(SQL Server 2005)时,我得到以下错误:
编号8152,16层,状态13,1线 字符串或二进制数据将被截断。
源数据列与数据类型匹配,并且在目标表列的长度定义内,因此我不知道是什么原因导致了这个错误。
我参与了一个数据迁移项目。当我试图将数据从一个表插入到另一个表(SQL Server 2005)时,我得到以下错误:
编号8152,16层,状态13,1线 字符串或二进制数据将被截断。
源数据列与数据类型匹配,并且在目标表列的长度定义内,因此我不知道是什么原因导致了这个错误。
当前回答
至少将数据类型nchar(10)的长度更改为nchar(255)。
其他回答
我在表创建上使用空字符串,然后在后续更新中接收错误'Msg 8152,字符串或二进制数据将被截断'。发生这种情况是因为更新值包含6个字符,并且比预期的列定义大。我使用“SPACE”来解决这个问题,只是因为我知道我将在初始数据创建后批量更新,即列不会长时间保持空。
这里有个大警告:这不是一个特别巧妙的解决方案,但在你把一个数据集放在一起的情况下很有用,例如,对于一次性的情报请求,你要为数据挖掘创建一个表,应用一些批量处理/解释,并存储结果前后,以便以后比较/挖掘。这是我这一行经常发生的事。
最初可以使用SPACE关键字进行填充。
select
Table1.[column1]
,Table1.[column2]
,SPACE(10) as column_name
into table_you_are_creating
from Table1
where ...
随后将允许对“column_name”进行10个字符或更少的后续更新(如适用可替换),而不会导致截断错误。同样,我只会在类似于我的警告中描述的场景中使用它。
是的,我也面临着这样的问题。
REMARKS VARCHAR(500)
to
REMARKS VARCHAR(1000)
在这里,我将备注文件的长度从500更改为1000
我也遇到过类似的问题。我将数据从一个表复制到一个完全相同的表,除了名称。
最后,我使用SELECT into语句将源表转储到临时表中。
SELECT *
INTO TEMP_TABLE
FROM SOURCE_TABLE;
我比较了源表和临时表的模式。我发现其中一列是varchar(4000),而我期待的是varchar(250)。
更新: 如果你感兴趣,varchar(4000)问题可以在这里解释:
对于Nvarchar(Max),我只能在TSQL中获得4000个字符?
希望这能有所帮助。
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.
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. :)