如何引用转换表

时间:2012-11-05 17:30:33

标签: mysql sql triggers

我有这两个表:

create table possiede (
soc1 integer not null,
soc2 integer not null,
primary key (soc1,soc2),
perc double
);

create table contr (
soc1 integer not null,
soc2 integer not null,
primary key(soc1, soc2)
);

我在通用SQL语法中有这两个触发器,我需要将它们转换为MySQL语法:

create trigger contrDir
after insert on possiede
for each row
when percent > 0.5 and (soc1,soc2) not in (select * from contr)
insert into contr values (soc1,soc2);

create trigger contrIndir
after insert on possiede
referencing new table as newTable
for each row
insert into possiede values
(select P.soc1, N.soc2, P.perc+N.perc
from newTable as N join possiede as P on N.soc1 = P.soc2);

这是我的第一次尝试,但它在“引用”关键字(“语法错误,意外的IDENT_QUOTED,期待FOR_SYM”)上给出了错误,我不确定翻译是否正确:

create trigger controllo
after insert on possiede
REFERENCING new table as newTable
for each row
begin
    insert into possiede (select P.soc1, N.soc2, P.perc+N.perc from
    newTable as N join possiede as P on N.soc1=P.soc2);
    if percent > 0.5 and (soc1,soc2) not in (select * from contr) then
    insert into contr values (soc1,soc2);
    end if;
end;

同样你注意到由于MySQL的一些限制,我必须将两个触发器压缩成一个。有人可以给我正确的翻译吗?

1 个答案:

答案 0 :(得分:0)

请将列名放在大括号内并使用NEW_TABLE。另外,我认为IN CLAUSE within IF BLOCK不正确,因为您正在检查两列(soc1和soc2)对select * from...。请尝试使用更新的查询,如下所示:

  CREATE TRIGGER controllo
  AFTER INSERT on possiede
  REFERENCING NEW_TABLE AS newTable
  FOR EACH ROW
   BEGIN
      INSERT INTO possiede (soc1, soc2, perc) 
      SELECT P.soc1, N.soc2, P.perc+N.perc 
      FROM newTable AS N JOIN possiede AS P ON N.soc1=P.soc2;
      IF percent > 0.5 and soc1 not in (select soc1 from contr)
          and soc2 not in (select soc2 from contr)
        THEN
          INSERT INTO contr VALUES (soc1,soc2);
      END IF;
    END;