如何传递逗号分隔的输入参数以从另一个存储过程调用存储过程

How to pass comma separated input parameter to call stored procedure from another stored procedure

我参考 this link 将逗号分隔的字符串传递给 运行 查询。效果很好! 但是我使用相同的方法调用存储过程,它显示以下错误:

Error Code: 1318. Incorrect number of arguments for PROCEDURE UPDATE_DETAILS; expected 1, got 3

这里是我用上面提到的方法试过的例子link,

CREATE DEFINER=`root`@`localhost` PROCEDURE `RUN_JOB`()
BEGIN
   declare ids text;

    select group_concat(id) into ids from table_1;

    set @sql = concat('call 
    UPDATE_DETAILS(',ids, ')');

    PREPARE stmt FROM @sql;

    EXECUTE stmt;

    DEALLOCATE PREPARE stmt;

END



CREATE DEFINER=`root`@`localhost` PROCEDURE `UPDATE_DETAILS`(
IN id_value int)
BEGIN

    update table_2 set 
        col_1 = 'some_value' where id = id_value;

    update table_3 set 
        col_2 = 'some_value' where id = id_value;

END

这是正确的方法还是有其他方法可以实现?

提前致谢!

由于您正在将逗号分隔值解析为 int 变量,因此它不起作用。

试试下面的 sp。这里使用 cursor 一个一个地获取 id,它会调用 sp UPDATE_DETAILS.

CREATE DEFINER=`root`@`localhost` PROCEDURE `RUN_JOB`()
BEGIN

    declare v_id int;
    declare done boolean default false;

    declare cur_id cursor for select id from table_1;

    declare continue handler for not found set done=true;

    open cur_id;

    call_loop: loop

        fetch cur_id into v_id;

            if done then

                leave call_loop;

            end if;

        call UPDATE_DETAILS(v_id);

    end loop;

    close cur_id;

    set done=false;

END