0

我有这两张表:

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 限制,我不得不将两个触发器压缩为一个。谁能给我正确的翻译?

4

1 回答 1

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;
于 2012-11-05T17:45:42.407 回答