简体   繁体   English

oracle pl sql 中的更新触发器问题

[英]update trigger issue in oracle pl sql

I am creating an update trigger on a table called tax_table and inserting :new and :old values into update_audit_table .我正在一个名为tax_table的表上创建一个更新触发器,并将:new:old值插入到update_audit_table中。 How can I incorporate related fields ( employee id from employee table) into update_audit_table in the same insert.如何将相关字段( employee表中的employee id )合并到同一个插入中的update_audit_table中。

CREATE  UPDAT_TAX_TRIG 
AFTER UPDATE
   ON tax_table
   FOR EACH ROW
 BEGIN
   INSERT INTO update_audit_table(PREV_TAX_CODE, OLD_TAX_CODE)
   VALUES(:NEW.PREV_TAX_CODE, OLD:PREV_TAX_CODE);
END;
update_audit_table:

employee_id,
employee_name,
prev_tax_code,
old_tax_code

employee table :
employee_id,
employee_name

I am trying to include fields from employee table into update_audit_table while inserting audit record.我试图在插入审计记录时将employee表中的字段包含到update_audit_table中。

As you have currently described the tables, it is impossible as there is no correlation that has been described between the tax_table and the employee table that would let you join the two.正如您目前描述的表一样,这是不可能的,因为tax_tableemployee表之间没有描述可以让您加入两者的相关性。

However, if we assume that tax_table has an employee_id column then you can use that:但是,如果我们假设tax_table有一个employee_id列,那么您可以使用它:

CREATE  UPDAT_TAX_TRIG 
  AFTER UPDATE ON tax_table
  FOR EACH ROW
BEGIN
  INSERT INTO update_audit_table(
     employee_id,
     employee_name,
     PREV_TAX_CODE,
     OLD_TAX_CODE
  ) VALUES(
    :NEW.employee_id,
    (SELECT employee_name FROM employees WHERE employee_id = :NEW.employee_id),
    :NEW.PREV_TAX_CODE,
    :OLD.PREV_TAX_CODE
  );
END;
/

If it does not then you need to work out how the tax_table relates to an employee .如果不是,那么您需要tax_tableemployee的关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM