osh*_*nen 7 sql t-sql sql-server-2008
我有一个大多数时间都可以运行的存储过程,但是每次都会收到一条错误消息:
Msg 8152, Level 16, State 2, Line 98
String or binary data would be truncated.
The statement has been terminated.
Run Code Online (Sandbox Code Playgroud)
如何确定导致此问题的数据字符串?
问题很明显,表中的一列的长度大于目标表的长度。
要查找可能产生问题的列的长度,您可以运行此查询
Select Max(Len(Column1)) --Take only varchar columns in this.
, Max(Len(Column2))
, Max(Len(Column3))
From YourTable
Run Code Online (Sandbox Code Playgroud)
现在您可以使用目标表的列长度检查字符串的长度。您很可能会发现任何一列的长度超过目标表列的指定长度。
假设执行上述查询后,column2 出现问题,即 varchar 的长度大于列长度。然后要查找特定值,您可以运行以下查询:
select * from yourtable
where len(column2)>20 --change 20 to the actual value of your column2
Run Code Online (Sandbox Code Playgroud)