假设我有一张表叫test,里面有三个字段。分别是id name news
然后我写一个触发器。drop trigger if exists tdu;
create trigger tdu after INSERT
on test for each row
begin
if new.news is NULL then
update test
set test.news=new.name
where test.id=new.id;
end if;
end然后执行 INSERT into test(id,name)value('1','ddd')然后就出现了错误  如下:
[Err] 1442 - Can't update table 'test' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

解决方案 »

  1.   

    drop trigger if exists tdu;
    create trigger tdu after INSERT
    on test for each row
    begin
    if new.news is NULL then
    SET new.news=new.name;
    end if;
    end
      

  2.   

    在MYSQL的TRIGGER中,不能对本表进行UPDATE、INSERT、DELETE等等操作,用SET new.news=new.name;即可赋值
      

  3.   


    大哥,还是不行啊,我重新修改触发器出现
    [Err] 1362 - Updating of NEW row is not allowed in after trigger
    能在帮我看看吗?
      

  4.   

    after INSERT->before INSERT
      

  5.   

    大哥。照你这样写。我这里出现了语法错误了
    [Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end
    end' at line 6能继续帮我看看么.打扰你了。。
      

  6.   


    create trigger tdu BEFORE INSERT
    on test for each row
    begin
    if new.news is NULL then
    SET new.news=new.name
    end if;
    end
      

  7.   

    我的要求是这样。在做新增的时候,如果没有给news字段赋值。就自动将name的值赋值给news插入。。我新手。之前没有写过触发器。
      

  8.   

    DELIMITER $$
    CREATE TRIGGER tdu  BEFORE INSERT
    ON test  FOR EACH ROW
    BEGIN
    IF NEW.news IS NULL THEN
    SET NEW.news=NEW.`name`;
    END IF;
    END$$
    DELIMITER ;