调用存储过程的触发器正在更新表上的每个数据,而不是只更新一个

时间:2016-04-15 13:33:53

标签: mysql sql

所以,我有两个表 table_a ,它有2个触发器用于插入和更新另一个表(table_b)的字段。

主要问题是,删除和插入触发器工作正常,但更新触发器在使用时,而不是只更新table_b上具有相同uid的字段,它正在更新每个字段(20k +字段)。我不确定我做错了什么,因为当我单独运行sp代码(更新查询)时,它就像预期的那样工作。但它只是在存储过程内部时不起作用。贝娄,我正在设置

触发代码:

 CREATE definer=`test_user`@`%` TRIGGER `trg_update` after
  UPDATE
  ON `table_a` FOR EACH row CALL sp_update(old.uid,new.name, new.number) 

存储过程代码:

CREATE DEFINER=`test_user`@`%` PROCEDURE `sp_update`(IN `uid` CHAR(50), IN `name` CHAR(50), IN `number` CHAR(50))
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
    SET @UID = uid;
    SET @NAME= name;
    SET @NUMBER= number;
UPDATE tbl_master
SET
    name = @NAME,
    number = @NUMBER
where
  uid = @UID;
END

提前致谢。

编辑:对于那些想要测试它的人来说,测试表中包含了每个触发器和sp:

表格

CREATE TABLE `table_a` (
    `a` INT(11) NULL DEFAULT NULL,
    `b` INT(11) NULL DEFAULT NULL,
    `c` INT(11) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;
CREATE TABLE `table_b` (
    `a` INT(11) NULL DEFAULT NULL,
    `b` INT(11) NULL DEFAULT NULL,
    `c` INT(11) NULL DEFAULT NULL
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
;

触发器

CREATE DEFINER=`root`@`%` TRIGGER `trg_insert` AFTER INSERT ON `table_a` FOR EACH ROW CALL sp_insert(new.a, new.b, new.c);

CREATE DEFINER=`root`@`%` TRIGGER `trg_update` AFTER UPDATE ON `table_a` FOR EACH ROW BEGIN
call sp_update(old.a,new.b,new.c);
END;

程序

CREATE DEFINER=`root`@`%` PROCEDURE `sp_insert`(IN `a` INT, IN `b` INT, IN `c` INT)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
begin
SET @A  =a;
SET @B = b;
SET @C = c;
insert into table_b (a,b,c) values (@A, @B, @C);
end

CREATE DEFINER=`root`@`%` PROCEDURE `sp_update`(IN `a` INT, IN `b` INT, IN `c` INT)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
set @A = a;
set @B = b;
set @C = c;
update table_b set b=@B, c=@c where a=@A;
END

1 个答案:

答案 0 :(得分:0)

So, i managed to find out what was causing this issue. I'm not sure if it is relevant or not, but since the MySQL Server is running on a windows server, I'm not sure if this issue is related to the OS or it's a behavior of the database itself.

Basicaly, it seend that i cannot use the same variable name ( inputs are in lower case, and routine body vars are in upper case). So , i had to change it from:

CREATE DEFINER=`root`@`%` PROCEDURE `sp_update`(IN `a` INT, IN `b` INT, IN `c` INT)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
set @A = a;
set @B = b;
set @C = c;
update table_b set b=@B, c=@c where a=@A;
END

To:

CREATE DEFINER=`root`@`%` PROCEDURE `sp_update`(IN `v_a` INT, IN `v_b` INT, IN `v_c` INT)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
set @A = v_a;
set @B = v_b;
set @C = v_c;
update table_b set b=@B, c=@c where a=@A;
END

Not sure if it it's my fault for using the same variable names , or if it's mysql fault for not recognizing the difference between lower/upper case characters.