网络编程
位置:首页>> 网络编程>> 数据库>> MySQL函数与存储过程字符串长度限制的解决

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

作者:Taysuesue  发布时间:2024-01-16 13:17:01 

标签: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://blog.csdn.net/Taysuesue/article/details/125272159

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com