mysql - 我在调用另一个存储过程时遇到困难

标签 mysql sql mysql-workbench

我想在另一个存储过程中调用一个存储过程,但语法似乎不正确。

我尝试跳过该过程并直接执行选择命令,但这似乎不起作用,而且我想知道如何执行此操作。

DELIMITER //
create procedure Find_Eid(Pid int)
    select EId from Employees inner join Patient on EId = EId_fk where Patient.PId = Pid;
//
DELIMITER ;
call Find_Eid(4);
drop procedure Fill_Interact;
DELIMITER //
create procedure Fill_Interact()
begin
    declare N1 int;
    declare TOT date;
    declare Rid int;
    declare Pid int;
    declare Eid int;  
    set N1 = (select count(*) from Patient);
    set TOT = curdate();
    set Rid = 1;
    set Pid = 1;
    while N1 > 0 do
        set Eid = (call Find_Eid(Pid));
        insert into Interact
        (Time_Of_Treatment, RId_fk, PId_fk, EId_fk)
        values
        (TOT,Rid,Pid, Eid);
        if Rid = (select count(*) from Room limit 1) then
                set Rid = 1;
                set TOT = TOT + 1;
        else
                set Rid = Rid + 1;
        end if;
        set N1 = N1 - 1;
        set Pid = Pid + 1;
    end while;
end;
//
DELIMITER ;
call Fill_Interact();

select * from Interact;

最佳答案

如果要将过程值分配给另一个变量,则必须在过程中使用输出参数。所以你的第一个过程应该是 -

DELIMITER //
create procedure Find_Eid(in Pid int, out eid int)
    select EId into eid
    from Employees
    inner join Patient on EId = EId_fk
    where Patient.PId = Pid;
//

然后您可以在第二个过程中使用此过程 -

drop procedure Fill_Interact;
DELIMITER //
create procedure Fill_Interact()
begin
    declare N1 int;
    declare TOT date;
    declare Rid int;
    declare Pid int;
    declare Eid int;  
    set N1 = (select count(*) from Patient);
    set TOT = curdate();
    set Rid = 1;
    set Pid = 1;
    while N1 > 0 do
        call Find_Eid(Pid, Eid);
        insert into Interact
        (Time_Of_Treatment, RId_fk, PId_fk, EId_fk)
        values
        (TOT,Rid,Pid, Eid);
        if Rid = (select count(*) from Room) then
                set Rid = 1;
                set TOT = TOT + 1;
        else
                set Rid = Rid + 1;
        end if;
        set N1 = N1 - 1;
        set Pid = Pid + 1;
    end while;
end;
//
DELIMITER ;
call Fill_Interact();

关于mysql - 我在调用另一个存储过程时遇到困难,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57962423/

相关文章:

MySQL 错误 1025

mysql - 关于我的 Deck Builder 数据库的建议

sql - 如何在 Sequelize 中为带有响应数组的表对象设置模型

mysql - 子查询的外部查询非常慢(Mysql)

sql - 哪里都不是空的

mysql - 使用 IN BOOLEAN MODE 时找不到 FULLTEXT 索引

mysql - 使用 MySQL Workbench CE 导出数据库的数据字典?

mysql - 如何从 MySQL Workbench 中的图表生成 SQL 脚本?

php - 使用 mysqli 的 user_exists 函数

mysql - mysql workbench 中最大 key 长度为 1000 字节