0

我写了下面的存储过程来从不同的表中删除用户。但不知何故,它不起作用。如果我在过程中保留单个查询,它正在工作,但如果我添加更多,那么它就不起作用。

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `deleteUser`(IN `userid` INT)
    NO SQL
BEGIN
DELETE FROM `Table1` WHERE id=userid;
DELETE FROM `Table2` WHERE user_id=userid;
DELETE FROM `Table3` WHERE userid=userid;
END$$
DELIMITER ;
4

1 回答 1

0

我无法重现该问题:

DROP PROCEDURE IF EXISTS `deleteUser`;
DROP TABLE IF EXISTS `Table3`, `Table2`, `Table1`;

CREATE TABLE IF NOT EXISTS `Table1` (
    `id` INT
);

CREATE TABLE IF NOT EXISTS `Table2` (
    `user_id` INT
);

CREATE TABLE IF NOT EXISTS `Table3` (
    `userid` INT
);

INSERT INTO `Table1` VALUES (100), (200), (300), (400);
INSERT INTO `Table2` VALUES (100), (600), (700), (800);
INSERT INTO `Table3` VALUES (100), (900), (1000), (1100);

SELECT `id` FROM `Table1`;
SELECT `user_id` FROM `Table2`;
SELECT `userid` FROM `Table3`;

DELIMITER \\

CREATE PROCEDURE `deleteUser`(IN `_userid` INT)
BEGIN
    DELETE FROM `Table1` WHERE `id` = `_userid`;
    DELETE FROM `Table2` WHERE `user_id` = `_userid`;
    DELETE FROM `Table3` WHERE `userid` = `_userid`;
END\\

DELIMITER ;

CALL `deleteUser`(100);

SELECT `id` FROM `Table1`;
SELECT `user_id` FROM `Table2`;
SELECT `userid` FROM `Table3`;

Rextester中的示例。

于 2017-04-12T12:04:54.457 回答