程序无效

时间:2017-04-12 08:47:53

标签: mysql stored-procedures mysql-routines

我写了以下商店程序来删除不同表中的用户。但不知怎的,它不起作用。如果我在程序中保留单独的查询它正在工作,但是如果我添加多个查询它就不起作用。

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 ;

1 个答案:

答案 0 :(得分: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中的示例。