表關(guān)聯(lián)形式為:PetAge->PetsonPet->Person.
代碼如下:
DROP PROCEDURE IF EXISTS delatePerson;
CREATE PROCEDURE delatePerson(in personId INT)
BEGIN
?? DECLARE petId INT;
?? DECLARE petAgeId INT;
?? DECLARE t_error INT DEFAULT 0;?
?? DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;
?? SELECT id INTO petId FROM personpet pp WHERE pp.id=personId;
?? SELECT id INTO petAgeId FROM petage pa WHERE pa.id=petId;
? START TRANSACTION;
?? DELETE FROM petage WHERE petage.id=petAgeId;
?? DELETE FROM personpet WHERE personpet.id=petId;
?? DELETE FROM person WHERE person.id=personId;
?? IF t_error = 1 THEN?
? ? ? ROLLBACK;?
?? ELSE?
? ? ? COMMIT;?
? ? END IF;
select t_error;
END
其中在存儲過程中自定義變量使用declare(局部)或者set(全局)净薛;
變量查詢賦值使用“select 字段 into 變量”的語句;
開啟事務(wù)start transaction或者begin榆浓,需要回滾則需要一個變量判斷是否錯誤租漂,使用DECLARE t_error INT DEFAULT 0;?
?? DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET t_error=1;然后判斷t_error的值來判斷是否提交還是回滾禾锤;
最后調(diào)用存儲過程使用CALL()方法即可蛔屹。