0

我有一个问题,我执行了一个对数据库中的几个表进行更新的存储过程。错误是这样完成的,每次只有一个字段(表示相同的记录 id)更新为新的 id。这是spoc:

DROP PROCEDURE IF EXISTS feeds_transfer;
DELIMITER $$
CREATE PROCEDURE feeds_transfer(IN original_owner INT, IN new_owner INT, IN feed_id INT)
    BEGIN
        UPDATE `events` SET `user_id` = new_owner WHERE `user_id` = original_owner AND `feed_id` = feed_id;
        UPDATE `feeds` SET `partner_id` = new_owner WHERE `partner_id` = original_owner AND `id` = feed_id;
    UPDATE `ics_uploads` SET `user_id` = new_owner WHERE `user_id` = original_owner AND `context_type` = 'feed' AND `context_id` = feed_id;
    UPDATE `images` SET `user_id` = new_owner WHERE `user_id` = original_owner AND `context` = 'feed' AND `context_id` = feed_id;
    UPDATE `private_feed_invitees` SET `user_id` = new_owner WHERE `user_id` = original_owner AND `feed_id` = feed_id;
        UPDATE `subscribed_feeds` SET `user_id` = new_owner WHERE `user_id` = original_owner AND `feed_id` = feed_id;
    END $$
DELIMITER ;

发生的情况是 subscribed_feeds 表将更新 user_id = original_owner 的每条记录。就像它忽略了feed_id= feed_id

4

1 回答 1

1

好吧,这里发生的事情与其说feed_id是忽略了 = feed_id 子句,不如说它总是正确的,所以它总是触发比预期更多的更新。这个故事的寓意是,您永远不应该将存储过程的输入变量命名为与您计划对其进行逻辑检查的任何字段相同的名称。

于 2012-07-29T22:01:48.267 回答