Oracle SQL 触发器与 JOIN

Oracle SQL trigger with JOIN

CREATE OR REPLACE TRIGGER TRIGGER_NAME 
BEFORE INSERT OR UPDATE of ON TABLE_NAME 
FOR EACH ROW 
BEGIN
    if inserting then
        if :new.col_a < (select col_b from table_a natural join table_b where some_col = :new.some_value)
            then
            raise_application_error(-20000, 'Raise some error');
        end if;
    end if;
END;

当我尝试编译时出现此错误

Error(6,93): PLS-00103: Encountered the symbol "JOIN" when expecting one of the following: ) , with group having intersect minus start union where connect The symbol "," was substituted for "JOIN" to continue.

我错过了什么?

导致错误的不是 JOIN,而是子查询的使用在此上下文中无效这一事实。 Select 值优先(在 IF 之外),稍后使用。像这样:

CREATE OR REPLACE TRIGGER trigger_name
   BEFORE INSERT
   --OR UPDATE of    --> of what?
   ON table_name
   FOR EACH ROW
DECLARE
   l_col_b  table_b.col_b%TYPE;
BEGIN
   IF INSERTING
   THEN
      SELECT col_b
        INTO l_col_b
        FROM table_a NATURAL JOIN table_b
       WHERE some_col = :new.some_value;

      IF :new.col_a < l_col_b
      THEN
         raise_application_error (-20000, 'Raise some error');
      END IF;
   END IF;
END;

此外,使用 table 别名。不可能知道哪个列属于哪个table。此外,您的触发器想要在 update of 之前触发……什么?我注释掉了。