Oracle-ORA-01461:can bind a LONG value only for insert into a LONG column解决办法之二!

青旅半醒 2023-10-09 20:23 142阅读 0赞

ORA-01461解决办法

  • 问题
  • 场景1:sql中使用from dual
    • 分析
    • 解决
  • 场景2:字符串超长
    • 解决

问题

在客户现场执行写入逻辑时遇到异常

  1. ORA-01461can bind a LONG value only for insert into a LONG column

场景1:sql中使用from dual

出问题的sql如下:

  1. insert into ttt(id, text)
  2. select SEQ_TTT.nextval, t.*
  3. from (
  4. <foreach collection="lists" item="item" separator="union all">
  5. select #{item.text}
  6. from dual
  7. </foreach>
  8. ) t;

分析

text字段是Clob类型

当从dual中取数据时,会将Clob对象的字段转为Long型,于是就有了can bind a LONG value only for insert into a LONG column这个报错

解决

修改sql如下:

  1. begin
  2. <foreach collection="lists" item="item">
  3. insert into ttt(id, text)values (
  4. SEQ_TTT.nextval,#{item.text}
  5. );
  6. </foreach>
  7. end;

场景2:字符串超长

首先说一下本例Oracle中该字段类型是nvarchar2(2000),什么意思呢?

重点说明:该字段不管存储中文还是英文,都能存入2000个字符,并且每个字符占2字节,即该字段最大存入字节数4000。

解决

在业务代码中,限制该字段最大字节数<=4000,即如果该字段字节数大于4000了,就截取<=4000字节数的最大字符串。方法如下:

  1. /**
  2. * 字符串限制截取
  3. *
  4. * @param str 待处理字符串
  5. * @param limit 限制字节数
  6. * @return 最大字节数以内的最长字符串
  7. */
  8. public static String subString(String str, int limit) {
  9. if (str == null) {
  10. return "";
  11. }
  12. int length = 0;
  13. StringBuilder stringBuilder = new StringBuilder();
  14. for (char c : str.toCharArray()) {
  15. if (length + String.valueOf(c).getBytes().length <= limit) {
  16. stringBuilder.append(c);
  17. length += String.valueOf(c).getBytes().length;
  18. } else {
  19. break;
  20. }
  21. }
  22. return stringBuilder.toString();
  23. }

发表评论

表情:
评论列表 (有 0 条评论,142人围观)

还没有评论,来说两句吧...

相关阅读