2014-02-14 58 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

相關問題