duan00529 2014-05-22 14:54 采纳率: 100%
浏览 1020

触发器阻止INSERT

I have a html form that send some data in a database. I just created a trigger that on every people that complete the city field with LA will add the word "HI FROM LA" in a column named tag.

The problem is that after I created this trigger I get an error when I try to submit a form:

#1442 - Can't update table 'users' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

The trigger is:

DELIMITER |
CREATE TRIGGER update_tag BEFORE INSERT ON users
        FOR EACH ROW
        BEGIN
            IF (NEW.city= 'LA') THEN
            INSERT INTO users
            SET NEW.tag = 'HI FROM LA';
            END IF;
        END;
|
DELIMITER ;

The insert is done with:

INSERT INTO users(id, name, city) VALUES(23, "John", "LA")

What alternatives do I have to avoid this conflict?

EDITED TRIGGER:

I also created a new table called 'trigger':

DELIMITER |
CREATE TRIGGER update_tag BEFORE INSERT ON users
        FOR EACH ROW
        BEGIN
            IF (NEW.city= 'LA') THEN
            INSERT INTO trigger
            SET NEW.tag = 'HI FROM LA';
            END IF;
        END;
|
DELIMITER ;

With this trigger, I get an error on line 5

  • 写回答

3条回答 默认 最新

  • donglu1881 2014-05-22 15:04
    关注

    You don't need to work with triggers for this.

    The trigger you have showed us would even create an endless loop, if it were to work.

    Just use your insert query like this:
    insert into users set (city, tag) values ('LA', concat('HI FROM ', city));

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题