2014-02-14 6 views
0

У меня есть хранимая процедура, как это и это работает прекрасно:множественного MySQL хранимых процедур в одном вызове

$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

В хранимой процедуре написать

if <condition> then 
    your code 
end if; 

так что ваш код Шоул d быть таким

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; 
+0

спасибо .. попробуем – coolguy

Смежные вопросы