mysql存储过程遇到问题,谁来看看怎么解决?


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//

call replacename();//
DELIMITER ;


上述代码,就是想实现取出数组内文本并替换部分内容,然后显示替换后的文本。结果提示显示失败。提示大字段信息不存在。

哪位技术牛来看看怎么解决 ?

在你的代码中,你定义了一个变量 @t,但是在循环中,你每次都重新赋值给 @t,导致最终只能得到最后一条记录的结果。另外,你也没有将替换后的结果更新回数据库中。
以下是修改后的代码,可以实现取出数组内文本并替换部分内容,然后更新回数据库中:

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 r = json_replace(r, '$[0]',replace(json_extract(r,'$[0]'),'ceshi','chenggong'));
    update'eb_store_prpduct' set slider_image=r where id=id;
    until i >= c
CLOSE result;
END//
DELIMITER ;
call 'replacename'();

在上述代码中,我使用了 JSON_REPLACE 函数来替换 JSON 数组中的文本,并使用 UPDATE 语句将更新后的结果更新回数据库中。

是执行过程出错吗?把出错的信息贴出来看看?