
mysql 存储过程中替换数组文本出错?
在使用存储过程替换数组文本时遇到问题,提示"大字段信息不存在"。
以下代码用于从 eb_store_product 表中提取数组文本并替换部分内容:
delimiter //
drop procedure if exists `replacename`//
create procedure replacename()
begin
    declare c int default 0;
    declare r json;
    declare id int default 0;
    declare i int default 0;
    declare result cursor for select id, slider_image from `eb_store_product`;
    select count(*) into c from eb_store_product;
    open result;
    repeat
        set i = i + 1;
        fetch result into id, r;
        set @t = replace(json_extract(r, '$[0]'), 'ceshi', 'chenggong');
    until i >= c
    end repeat;
    close result;
    select @t;
end//
delimiter ;
call replacename();然而,执行该存储过程返回 "大字段信息不存在" 的错误。
解决方案:
代码中缺少更新 eb_store_product 表中 slider_image 字段的步骤。需要在循环体内添加以下语句:
update eb_store_product set slider_image = json_replace(r, '$[0]', @t) where id = id;
这是修改后的存储过程:
DELIMITER //
DROP PROCEDURE IF EXISTS `replacename`//
CREATE PROCEDURE replacename()
BEGIN
    DECLARE c INT DEFAULT 0;
    DECLARE r JSON;
    DECLARE id INT DEFAULT 0;
    DECLARE i INT DEFAULT 0;
    DECLARE result CURSOR FOR SELECT id, slider_image FROM `eb_store_product`;
    SELECT COUNT(*) INTO c FROM eb_store_product;
    OPEN result;
    REPEAT
        SET i = i + 1;
        FETCH result INTO id, r;
        SET @t = REPLACE(JSON_EXTRACT(r, '$[0]'), 'ceshi', 'chenggong');
        -- 更新 eb_store_product 表中的 slider_image 字段
        UPDATE eb_store_product SET slider_image = JSON_REPLACE(r, '$[0]', @t) WHERE id = id;
    UNTIL i >= c
    END REPEAT;
    CLOSE result;
    SELECT @t;
END//
DELIMITER ;
CALL replacename();以上就是MySQL 存储过程中替换数组文本,为什么提示“大字段信息不存在”?的详细内容,更多请关注php中文网其它相关文章!
                        
                        每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
                Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号