0

我创建了这 4 个表:

create table terminatedEmployees (
    empid       number primary key,
    dept        number,
    empname     varchar2(50),
    salary      number
);

create table employees (
    empid       number primary key,
    dept        number, 
    empname     varchar2(50),
    salary      number
);

create table payroll (
    empid       number primary key,
    salary      number,
    CONSTRAINT fk_payemploy
    FOREIGN KEY (empid)
    REFERENCES employees(empid)
);

create table salaryAudit (
    empid       number primary key,
    oldsal      number,
    newsal      number, 
    datechanged date,
    changedby   varchar2(25),
    CONSTRAINT fk_salaryaudit
    FOREIGN KEY (empid)
    REFERENCES employees(empid)
);

现在我正在尝试创建一个触发器,以便在更新员工表时更新其中两个:

CREATE TRIGGER trigger_updated_employees 
AFTER UPDATE ON employees
    FOR EACH ROW
    when (old.salary != new.salary)

BEGIN
    UPDATE INTO salaryAudit (newsal, oldsal)
        VALUES(:new.salary, :old.salary);
    UPDATE INTO payroll (salary)
        VALUES(:new.salary);
END;

但我得到了错误:

2/5      PL/SQL: SQL Statement ignored
2/12     PL/SQL: ORA-00903: invalid table name
4/5      PL/SQL: SQL Statement ignored
4/12     PL/SQL: ORA-00903: invalid table name

我在触发器中调用的三个表都可以,我创建的其他触发器工作......

4

2 回答 2

0

尝试这样的事情:

CREATE TRIGGER TRIGGER_UPDATED_EMPLOYEES
AFTER UPDATE ON EMPLOYEES
    FOR EACH ROW
    WHEN (OLD.SALARY <> NEW.SALARY)
BEGIN
    MERGE INTO PAYROLL p
      USING (SELECT :NEW.EMPID AS EMPID FROM DUAL) d
        ON (p.EMPID = d.EMPID)
      WHEN NOT MATCHED THEN
        INSERT (EMPID, SALARY)
          VALUES (:NEW.EMPID, :NEW.SALARY)
      WHEN MATCHED THEN
        UPDATE
          SET SALARY = :NEW.SALARY;

    MERGE INTO SALARYAUDIT a
      USING (SELECT :NEW.EMPID AS EMPID FROM DUAL) d
        ON (a.EMPID = d.EMPID)
      WHEN NOT MATCHED THEN
        INSERT (EMPID, OLDSAL, NEWSAL, DATECHANGED, CHANGEDBY)
          VALUES (:NEW.EMPID, :OLD.SALARY, :NEW.SALARY, SYSDATE, 'SOME_USER')
      WHEN MATCHED THEN
        UPDATE 
          SET OLDSAL = :OLD.SALARY,
              NEWSAL = :NEW.SALARY,
              DATECHANGED = SYSDATE,
              CHANGEDBY = 'SOME_USER';
END TRIGGER_UPDATED_EMPLOYEES;

分享和享受。

于 2013-11-06T18:48:56.537 回答
0

我得到它的工作只是更正 UPDATE 语句语法并按照@BobJarvis 的建议修改条件,这是最终结果:

    CREATE TRIGGER trigger_updated_employees 
    AFTER UPDATE OF salary ON employees
        FOR EACH ROW
        when (old.salary <> new.salary)

    BEGIN
        UPDATE salaryAudit 
            SET (newsal, oldsal)
            VALUES (:new.salary, :old.salary)
            WHERE (salaryAudit.empid = old.empid);
        UPDATE payroll 
            SET (salary)
            VALUES (:new.salary)
            WHERE (payroll.empid = old.empid);
    END;

这个概念与@BobJarvis 提出的概念相同,但要简单得多。谢谢

于 2013-11-20T01:42:14.283 回答