我建立了一个触发器t_add 当t1中插入数据时t2中自动插入一条。
create table t1(
id number(4),
name varchar2(20)
);create table t2(
id number(4),
name varchar2(20)
);
CREATE OR REPLACE TRIGGER t_add
BEFORE
insert on t1
FOR EACH ROW
begin
IF INSERTING THEN insert into t2(id,name) values(new.id,new.name);
END;可是创建触发器时创建失败,请问各位大哥是为什么啊?

解决方案 »

  1.   


    CREATE OR REPLACE TRIGGER t_add
    BEFORE
    insert on t1
    FOR EACH ROW
    begin
    IF INSERTING THEN insert into t2(id,name) values(:new.id,:new.name);
    end if;--不是new 是:new ,而且要endif
    --其实你这个不用if语句的
    --这样就可以了insert into t2(id,name) values(:new.id,:new.name);
    END;
      

  2.   

    --关键字new前面要加冒号
    CREATE OR REPLACE TRIGGER t_add
    BEFORE insert on t1
    FOR EACH ROW
    begin
    IF INSERTING THEN insert into t2(id,name) values(:new.id,:new.name);  --冒号
    end if;  --加end if
    END;
      

  3.   


    CREATE OR REPLACE TRIGGER t_add
    BEFORE insert on t1
    FOR EACH ROW
    begin
    insert into t2(id,name) values(new.id,new.name);
    END;
      

  4.   

    为什么我复制过去还报错啊?是权限不够吗?我是用root身份登录的。
      

  5.   

    ERROR 1064 (42000): 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 if' at line 1
      

  6.   

    --不好意思,查了下mysql好像没有or replace
    CREATE TRIGGER t_add
    BEFORE insert on t1
    FOR EACH ROW
    begin
    insert into t2(id,name) values(new.id,new.name);
    END;
      

  7.   

    ERROR 1064 (42000): 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 '' at line 5
    还是有问题,报这个错,我对触发器没怎么用过,呵呵 ,希望多指教,谢谢!
      

  8.   

    这个应该不是语法问题,具体什么原因我也不清楚 没玩过mysql 你google下
      

  9.   

    存储过程是赋值是:new.id,mysql创建存储过程的用法 没有orreplace这个东西的。
    mysql5.0创建触发器语法如下:
    create trigger t_add after insert on t1 for each row 
    begin
    insert into t2(id,name) values(new.id,new.name);
    end; 
      

  10.   

    CREATE OR REPLACE TRIGGER t_add BEFORE insert on t1 FOR EACH ROW
    begin
    insert into t2(id,name) values(:new.id,:new.name);
    END;--你的本来事件就是insert 就没必要IF INSERTING THEN 
      

  11.   

    IF INSERTING THEN  这个 oracle 有?