为什么此存储过程会删除数据库中的所有对象?

时间:2012-07-20 05:33:45

标签: mysql stored-procedures

这是一个存储过程,只能擦除数据库中的某些对象,但它会擦除所有对象。

DROP PROCEDURE IF EXISTS `server_WIPE`;
DELIMITER //
CREATE DEFINER=`root`@`localhost` PROCEDURE `server_WIPE`(IN `worldID` INT)
MAIN:BEGIN

DECLARE UID BIGINT UNSIGNED;
DECLARE finished INT DEFAULT 0;
DECLARE cityLoop CURSOR FOR SELECT `object`.`parent` FROM `object` INNER JOIN `obj_city` ON `object`.`UID` = `obj_city`.`UID` WHERE `obj_city`.`worldServerID` = worldID;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;

DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
BEGIN 
    ROLLBACK; 
    SELECT -1 AS `errorno`, 'Procedure failed and rolled back' as `errormsg`;
END;

#Verify worldID exists
IF ISNULL(worldID) THEN
    SELECT -2 AS `errorno`, "worldID CANNOT be null" AS `errormsg`;
    LEAVE MAIN;
ELSEIF NOT EXISTS(SELECT NULL FROM `obj_worldServer` WHERE `world_id` = worldID) THEN
    SELECT -3 AS `errorno`, "worldID must exist. World not found" AS `errormsg`;
    LEAVE MAIN;
END IF;

START TRANSACTION;

    #Delete all cities. This will also remove Buildings, npc's and mobs for city.
    OPEN cityLoop;
        read_loop: LOOP
            FETCH cityLoop INTO UID;
            IF finished THEN
                LEAVE read_loop;
            END IF;
            DELETE FROM `object` WHERE `UID` = UID;             
        END LOOP;
    CLOSE cityLoop;

    #Delete all player guilds for server
    DELETE `object` FROM `object` INNER JOIN `obj_guild` ON `object`.`UID` = `obj_guild`.`UID` WHERE `obj_guild`.`worldServerID` = worldID;

    #Delete all items for server
    DELETE `object` FROM `object` INNER JOIN `obj_item` ON `object`.`UID` = `obj_item`.`UID` WHERE `obj_item`.`worldServerID` = worldID;

    #Delete all players for server
    DELETE `object` FROM `object` INNER JOIN `obj_character` ON `object`.`UID` = `obj_character`.`UID` WHERE `obj_character`.`worldServerID` = worldID;

COMMIT; 
SELECT 'Server Wiped Successfully' AS `result`;

END MAIN//
DELIMITER ;

0 个答案:

没有答案