字符串或二进制数据将被截断 SQL 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6825550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
String or binary data would be truncated SQL Error
提问by Lior Ohana
I have a SQL stored procedure that accepts a parameter of type VARCHAR(MAX). As far as I know and according to all I read about, the max size for such strings is 2GB: MSDN
我有一个 SQL 存储过程,它接受一个 VARCHAR(MAX) 类型的参数。据我所知,根据我阅读的所有内容,此类字符串的最大大小为 2GB: MSDN
For some reason when passing a string larger than 8KB I get:
由于某种原因,当传递大于 8KB 的字符串时,我得到:
String or binary data would be truncated.
字符串或二进制数据将被截断。
Why do I get this error message, and how can I resolve it?
为什么会收到此错误消息,我该如何解决?
采纳答案by Mark Kremers
According to BoL (the link you specified) there is a difference in interpretation. The maximum amount you can use in a query (the n part) is 8000. For storage purposes the varchar(max) can handle 2GB on disk.
根据 BoL(您指定的链接),解释有所不同。您可以在查询中使用的最大数量(n 部分)是 8000。出于存储目的,varchar(max) 可以处理磁盘上的 2GB。
It is just interpretation of datatypes for querying and storage purposes. So bottom line, you can only use 8000 chars in a query....
它只是用于查询和存储目的的数据类型的解释。所以最重要的是,您只能在查询中使用 8000 个字符....
回答by knittl
to avoid this problem, you have to cast your string first to varchar(max)
:
为避免此问题,您必须先将字符串转换为varchar(max)
:
column = cast(other_column as varchar(max))
this way any string longer than max (8000 or 4000, depending on version) will be truncated to the maximum length.
这样,任何超过 max(8000 或 4000,取决于版本)的字符串都将被截断为最大长度。
回答by Junior Mayhé
You are passing a string biggerthan the column in database, right? Try to increase the size of your column.
您传递的字符串比数据库中的列大,对吗?尝试增加列的大小。
回答by Feroj Ahmed
You are passing a stringbigger than the columnin database.
您正在传递一个比数据库中的列大的字符串。
Like your database table is as follow:
就像你的数据库表如下:
EXECUTIVE varchar(15)
But your insert string like (in vb.net):
但是您的插入字符串如(在vb.net 中):
cmd.Parameters.Add("@EXECUTIVE", Data.SqlDbType.VarChar, 150).Value = CmbExecutive.Text.ToString()