简体   繁体   English

'字符串或二进制数据将被截断'修复无法正常工作。 我究竟做错了什么?

[英]'String or binary data would be truncated' fix isn't working. What am I doing wrong?

So this is an old question that has been asked many times here, but the obvious solution isn't working for me. 所以这是一个古老的问题,这里曾经多次提出过这个问题,但显而易见的解决办法并不适用于我。 I have a query that reads: 我有一个查询:

INSERT Table1
(
    schoolName
)
VALUES
(
    <cfqueryparam value="#schoolName#" cfsqltype="CF_SQL_VARCHAR" maxlength="50">
)

When running this query, I was getting back the 'String or binary data would be truncated' error that's been reported here dozens of times. 运行此查询时,我回到了“字符串或二进制数据将被截断”错误,这里已经报告了数十次。 I looked at the 'Design' option for my table and the maxlength for schoolName was 20, while the value I was trying to insert was of length 22. So in what I thought was an easy fix I simply ran: 我查看了我的表的'Design'选项,schoolName的maxlength是20,而我试图插入的值是22。所以在我想的是一个简单的修复我只是运行:

ALTER TABLE Table1
ALTER COLUMN schoolName VARCHAR(50)

However I ran this query again, and got the same error! 但是我再次运行此查询,并得到了同样的错误! Is there anything else I need to do or anything dumb I am missing? 还有什么我需要做的或者我失踪的任何蠢事吗? Could it be a caching issue? 这可能是一个缓存问题吗? From what I can tell, the error is saying that I'm trying to insert a string with a length larger than the column is set to allow, and therefore one (of the many) solutions is to expand the max column length. 据我所知,错误在于我正在尝试插入一个长度大于列设置为允许的字符串,因此其中一个(很多)解决方案是扩展最大列长度。

Note that the maxlength attribute in the cfqueryparam tag is a validation that simply throws an exception before even sending the query to the SQL Server when the string length exceeds the specified maximum. 请注意, cfqueryparam标记中的maxlength属性是一种验证,在字符串长度超过指定的最大值时,甚至会在将查询发送到SQL Server之前抛出异常。

In your case it passed and the String or binary data would be truncated message is an error caused by SQL Server. 在你的情况下,它传递和String or binary data would be truncated消息是由SQL Server引起的错误。 Check if the schoolName column in the Table1 table was properly altered by your change. 检查Table1表中的schoolName列是否被更改正确更改。

On a side note, if you are fine with truncated data, you could always use: 另外,如果您对截断数据没问题,可以随时使用:

<cfqueryparam value="#left(schoolName, 50)#" cfsqltype="CF_SQL_VARCHAR">

This truncates your value if it exceeds 50 characters without causing an error - unless your table schema specifies an even lower max. 如果超过50个字符而不会导致错误,则会截断您的值 - 除非您的表模式指定了更低的最大值。 length. 长度。 In this case you would still see the same error message. 在这种情况下,您仍会看到相同的错误消息。

I feel stupid making this mistake, but it happens so I'll post the answer for anyone in the future: 我觉得犯这个错误很愚蠢,但它发生了,所以我会在将来为任何人发布答案:

There is a trigger set on my Table1 that was inserting into/updating a Table1Log table. 我的Table1上有一个触发器设置插入/更新Table1Log表。 Since the Table1Log schoolName column was not altered along with the Table1 schoolName column, I was getting the same error as before. 由于Table1Log schoolName列未与Table1 schoolName列一起更改,因此我得到了与以前相同的错误。 Once I altered the column on both things started working fine. 一旦我改变了列,两件事情都开始正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM