当前位置: 首页 > mysql > 正文

MySQL函数与存储过程字符串长度限制的解决

MySQL函数或者存储过程中使用group_concat()函数导致数据字符过长而报错

CREATE DEFINER=`root`@`%` PROCEDURE `get_pipe_child`(IN `in_pipe2Num` varchar(25),IN `in_sectionNum` varchar(5))
BEGIN

 declare ids varchar(1000) default ''; 
 declare tempids varchar(1000); 
 
 -- 先根据标段号查询出数据组成临时表
 DROP TEMPORARY TABLE IF EXISTS temp_weld_position;
 CREATE TEMPORARY TABLE temp_weld_position AS
 select t1.id,t1.section_num,t1.weld_code,t1.lon,t1.lat,t2.pipe1_num,t2.pipe2_num from
    (select id,section_num,weld_code,lon,lat from weld_position where section_num = in_sectionNum and LENGTH(lon)>=7 and LENGTH(lat)>=6 and is_deleted=0) t1
    join (select id,weld_code,pipe1_num,pipe2_num from weld_manage where section_num = in_sectionNum) t2 on t1.weld_code=t2.weld_code;

 -- 在根据传入的pipe2_num 递归查询出所有的数据,将pipe2_num当做id,pipe1_num当pid
 set tempids = in_pipe2Num; 
 while tempids is not null do 
  set ids = CONCAT_WS(',',ids,tempids); 
  select GROUP_CONCAT(pipe2_num) into tempids from temp_weld_position where FIND_IN_SET(pipe1_num,tempids)>0;  
 end while; 
 
   select t1.id,t1.section_num,t1.weld_code,t1.lon,t1.lat,t2.pipe1_num,t2.pipe2_num from
   (select id,section_num,weld_code,lon,lat from weld_position where section_num = in_sectionNum and LENGTH(lon)>7 and LENGTH(lat)>6 and is_deleted=0) t1
   join (select id,weld_code,pipe1_num,pipe2_num from weld_manage where section_num = in_sectionNum) t2 on t1.weld_code=t2.weld_code
   where FIND_IN_SET(t2.pipe2_num,ids)
   order by FIND_IN_SET(t2.pipe2_num,ids);                
END

原因分析:

两个参数ids、tempids定义的varchar(1000),后续执行多次循环,GROUP_CONCAT拼接字符放入这两个参数时就会报字符串长度超限错误,因函数、存储过程中varchar类型最大长度为16383

解决方案:

将varchar(1000)类型变成text或者是BLOB类型解决此问题

本文固定链接: https://www.ziyouwu.com/archives/70479.html | 自由屋博客


关键字: