多个mysql存储过程在一次调用中

时间:2014-02-14 05:53:18

标签: mysql sql stored-procedures

我有一个像这样的存储过程,它工作正常:

$drop = $mysqli->query("DROP PROCEDURE IF EXISTS changegroup");
$initiate = $mysqli->query("
    Create Procedure changegroup(IN param1 int(10),IN param2 int(10))
    BEGIN
       UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
    END;
");
$result = $mysqli->query("CALL changegroup($p1,$p2);");

我的问题是,我们可以在一个程序中放入两个SQL语句,并基于第一个程序执行第二个程序,如下所示:

BEGIN
     SELECT * FROM ........
     /**fetch the result of this mysql statment and if matches certain conditions,then execute the update statment***/

     UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
END;

1 个答案:

答案 0 :(得分:1)

在您的存储过程中写

if <condition> then 
  your code
end if;

所以你的代码应该是这样的

Create Procedure changegroup(IN param1 int(10),IN param2 int(10))
BEGIN
     DECLARE totalcount Integer default 0;
     SELECT count(*) into totalcount  FROM yourtable where <condition>;
     /**fetch the result of this mysql statment and if matches certain conditions,then execute the update statment***/
    if(totalcount > 0) then
     UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
    end if;
END;