oracle 如果将一个表的主键用于其他表的外键,触发器将如何工作

py49o6xq  于 2023-10-16  发布在  Oracle
关注(0)|答案(1)|浏览(144)

我有两个DB。一个是远程DB,另一个是本地DB。两人都有三张table。我已经为订单表创建了一个触发器,当远程数据库中有任何更改时,该触发器将更新/删除。我需要创建触发器的其他两个表ORd_detail和发布。但是我想通过在order表中使用orderno来更新值。如何修改查询以在两个数据库的订单号匹配时进行验证。
1.订单--> ID(PK)和订单号

  1. Ord_Detail--> Ord_detail_ID(PK),ordersid(订单表中的FK)
  2. Releases--> Releases_id(PK),Ord_detail_ID(FK from ord_detail)
    我已经为订单表创建了一个触发器。
CREATE OR REPLACE TRIGGER trg_order_locals
AFTER UPDATE OR DELETE ON ORDERS
FOR EACH ROW
DECLARE
  PRAGMA AUTONOMOUS_TRANSACTION; 
BEGIN
  -- Update or delete the corresponding row in the remote database
  IF UPDATING THEN
    UPDATE ORDERS@local_link
    SET ID = :NEW.ID, ORDERNO = :NEW.ORDERNO, PONO = :NEW.PONO, ARCUSTO_ID = :NEW.ARCUSTO_ID, ORD_DATE = :NEW.ORD_DATE, ORD_BY = :NEW.ORD_BY, USERID = :NEW.USERID, DATE_TAKEN = :NEW.DATE_TAKEN, BILL_TO_ID = :NEW.BILL_TO_ID, SHIP_TO_ID = :NEW.SHIP_TO_ID, CUSER1 = :NEW.CUSER2, CUSER3 = :NEW.CUSER3, CUSER5 = :NEW.CUSER5, CUSER6 = :NEW.CUSER6, TIMESTAMP = :NEW.TIMESTAMP, FOB = :NEW.FOB, CHANGEUSER_ID = :NEW.CHANGEUSER_ID, TERMS_ID = :NEW.TERMS_ID, FREIGHT_ID = :NEW.FREIGHT_ID, FREE_FORM = :NEW.FREE_FORM, DISCOUNT = :NEW.DISCOUNT, CONTACT_ID = :NEW.CONTACT_ID, ECODE = :NEW.ECODE, EID = :NEW.EID, EDATE_TIME = :NEW.EDATE_TIME, ECOPY = :NEW.ECOPY, CURRENCY_ID = :NEW.CURRENCY_ID, TYPE = :NEW.TYPE, REV = :NEW.REV, OE_CONTROL = :NEW.OE_CONTROL, EPLANT_ID = :NEW.EPLANT_ID, EDI_855_CREATED = :NEW.EDI_855_CREATED, PRINT_COUNT = :NEW.PRINT_COUNT, PRINT_LAST_USER = :NEW.PRINT_LAST_USER, PRINT_LAST_DATE = :NEW.PRINT_LAST_DATE, WEB_ORDERS_ID = :NEW.WEB_ORDERS_ID, EDI_CHANGE_DATE = :NEW.EDI_CHANGE_DATE, WEB_CHANGE_DATE = :NEW.WEB_CHANGE_DATE, AGGREGATE_CALCULATED = :NEW.AGGREGATE_CALCULATED, INTER_CONSIGN = :NEW.INTER_CONSIGN, ARCUSTO_CREDIT_CARD_ID = :NEW.ARCUSTO_CREDIT_CARD_ID, DIVISION_ID = :NEW.DIVISION_ID, CUSER4 = :NEW.CUSER4, CRM_QUOTE_ID = :NEW.CRM_QUOTE_ID, ALT_DIVISION_ID = :NEW.ALT_DIVISION_ID, COMMENT1 = :NEW.COMMENT1, PAYMENT_TYPE = :NEW.PAYMENT_TYPE, EXT_COMMENT1 = :NEW.EXT_COMMENT1, ORD_LINE_DATE = :NEW.ORD_LINE_DATE, FREIGHT_CHARGE = :NEW.FREIGHT_CHARGE, FREIGHT_QUOTENO = :NEW.FREIGHT_QUOTENO, APPLY_FREIGHT_BOL_TO_PS = :NEW.APPLY_FREIGHT_BOL_TO_PS, CONTACT_ID2 = :NEW.CONTACT_ID2, CAMPAIGN_ID = :NEW.CAMPAIGN_ID, CRM_OPPORTUNITY_ID = :NEW.CRM_OPPORTUNITY_ID, FOB_THIRD_PARTY_ID = :NEW.FOB_THIRD_PARTY_ID, EDI_865_CREATED = :NEW.EDI_865_CREATED, DROP_SHIP_PO_ID = :NEW.DROP_SHIP_PO_ID, APPROVED = :NEW.APPROVED, APPROVAL_REQUIRED = :NEW.APPROVAL_REQUIRED, FREIGHT_ACCOUNT_NUMBER = :NEW.FREIGHT_ACCOUNT_NUMBER, IS_DRAFT = :NEW.IS_DRAFT, IS_MANUAL_OUTSOURCE = :NEW.IS_MANUAL_OUTSOURCE, IS_QUICK_ORDER = :NEW.IS_QUICK_ORDER, TAX_CALCULATED = :NEW.TAX_CALCULATED, SHIP_COMPLETE = :NEW.SHIP_COMPLETE, ACCRUED_FREIGHT = :NEW.ACCRUED_FREIGHT, ICT_MANUAL_SHIP_TO_ID = :NEW.ICT_MANUAL_SHIP_TO_ID, SO_DISCOUNT_TYPE_ID = :NEW.SO_DISCOUNT_TYPE_ID, SO_DISCOUNT_TYPE_TOTAL_AMOUNT = :NEW.SO_DISCOUNT_TYPE_TOTAL_AMOUNT, SO_DISCOUNT_TYPE_PERCENT = :NEW.SO_DISCOUNT_TYPE_PERCENT, STATUS = :NEW.STATUS, PALLET_CHARGE = :NEW.PALLET_CHARGE, PALLET_CHARGE_SOURCE = :NEW.PALLET_CHARGE_SOURCE, USE_PALLET_CHARGE = :NEW.USE_PALLET_CHARGE, DIST_ROUTING_RULES_ID = :NEW.DIST_ROUTING_RULES_ID, DS_SOURCE = :NEW.DS_SOURCE
    WHERE ID = :OLD.ID AND ORDERNO = :OLD.ORDERNO;

  ELSIF DELETING THEN
    DELETE FROM ORDERS@local_link
    WHERE ID = :OLD.ID AND ORDERNO = :OLD.ORDERNO;

  END IF;
  commit;
END;
/
CREATE OR REPLACE TRIGGER trg_orddetail_locals
AFTER UPDATE OR DELETE ON ORD_DETAIL
FOR EACH ROW
DECLARE
  PRAGMA AUTONOMOUS_TRANSACTION; 
BEGIN
  -- Update or delete the corresponding row in the remote database
  IF UPDATING THEN
    UPDATE ORD_DETAIL@local_link dst
    SET ID = :NEW.ID, ORDERS_ID = :NEW.ORDERS_ID, ARINVT_ID = :NEW.ARINVT_ID, ORD_DET_SEQNO = :NEW.ORD_DET_SEQNO, TOTAL_QTY_ORD = :NEW.TOTAL_QTY_ORD, CUMM_SHIPPED = :NEW.CUMM_SHIPPED, ONHOLD = :NEW.ONHOLD, TAX_CODE_ID = :NEW.TAX_CODE_ID, MISC_ITEM = :NEW.MISC_ITEM, COMMENT1 = :NEW.COMMENT1, UNIT_PRICE = :NEW.UNIT_PRICE, SALESPEOPLE_ID = :NEW.SALESPEOPLE_ID, COMM_PCT = :NEW.COMM_PCT, PRICE_PER_1000 = :NEW.PRICE_PER_1000, LIST_UNIT_PRICE = :NEW.LIST_UNIT_PRICE, DISCOUNT = :NEW.DISCOUNT, ECODE = :NEW.ECODE, EID = :NEW.EID, EDATE_TIME = :NEW.EDATE_TIME, ECOPY = :NEW.ECOPY, EPLANT_ID = :NEW.EPLANT_ID, COST_OBJECT_ID = :NEW.COST_OBJECT_ID, COST_OBJECT_SOURCE = :NEW.COST_OBJECT_SOURCE, UNIT = :NEW.UNIT, UOM_FACTOR = :NEW.UOM_FACTOR, GLACCT_ID = :NEW.GLACCT_ID, DOCKID = :NEW.DOCKID, LINEFEED = :NEW.LINEFEED, RESERVELOCATION = :NEW.RESERVELOCATION, KBTRIGGER = :NEW.KBTRIGGER, AGGREGATE_DISCOUNT = :NEW.AGGREGATE_DISCOUNT, CUST_CUM_START = :NEW.CUST_CUM_START, LAST_RECEIPT_QTY = :NEW.LAST_RECEIPT_QTY, LAST_RECEIPT_DATE = :NEW.LAST_RECEIPT_DATE, RMA_DETAIL_ID = :NEW.RMA_DETAIL_ID, REF_CODE_ID = :NEW.REF_CODE_ID, FAB_QTY = :NEW.FAB_QTY, RAW_MT_QTY = :NEW.RAW_MT_QTY, FAB_START_DATE = :NEW.FAB_START_DATE, FAB_END_DATE = :NEW.FAB_END_DATE, CONTAINERS = :NEW.CONTAINERS, FROM_SALES_OPTION = :NEW.FROM_SALES_OPTION, SHIP_TO_ID_FROM = :NEW.SHIP_TO_ID_FROM, IN_TRANSIT_WORKORDER_ID = :NEW.IN_TRANSIT_WORKORDER_ID, IN_TRANSIT_PARTNO_ID = :NEW.IN_TRANSIT_PARTNO_ID, IS_DROP_SHIP = :NEW.IS_DROP_SHIP, IS_MAKE_TO_ORDER = :NEW.IS_MAKE_TO_ORDER, MFG_QUAN = :NEW.MFG_QUAN, PO_INFO = :NEW.PO_INFO, MAKE_TO_ORDER_PS_TICKET_DTL_ID = :NEW.MAKE_TO_ORDER_PS_TICKET_DTL_ID, CAMPAIGN_ID = :NEW.CAMPAIGN_ID, CTP = :NEW.CTP, CRM_QUOTE_DETAIL_ID = :NEW.CRM_QUOTE_DETAIL_ID, SALES_OPTION_CHOICE_ID = :NEW.SALES_OPTION_CHOICE_ID, CUSER1 = :NEW.CUSER1, CUSER2 = :NEW.CUSER2, CUSER3 = :NEW.CUSER3, REBATE_PARAMS_ID = :NEW.REBATE_PARAMS_ID, PHANTOM_ORD_DETAIL_ID = :NEW.PHANTOM_ORD_DETAIL_ID, PHANTOM_PTSPER = :NEW.PHANTOM_PTSPER, HIDE = :NEW.HIDE, OUTSOURCE_PO_DETAIL_ID = :NEW.OUTSOURCE_PO_DETAIL_ID, LOT_CHARGE_ARINVT_ID = :NEW.LOT_CHARGE_ARINVT_ID, STANDARD_ID = :NEW.STANDARD_ID, DIVISION_ID = :NEW.DIVISION_ID, AKA_KIND = :NEW.AKA_KIND, MSDS_UPLOAD = :NEW.MSDS_UPLOAD, SHIPHOLD = :NEW.SHIPHOLD, MILK_RUN_LOCATIONS_ID = :NEW.MILK_RUN_LOCATIONS_ID, LOT_CHARGE_ORD_DETAIL_ID = :NEW.LOT_CHARGE_ORD_DETAIL_ID, AUTO_INVOICE = :NEW.AUTO_INVOICE, C_PO_MISC_ID = :NEW.C_PO_MISC_ID, MISC_ITEMNO = :NEW.MISC_ITEMNO, SAMPLE = :NEW.SAMPLE, PHANTOM_DTL_MODIFIED = :NEW.PHANTOM_DTL_MODIFIED, BOGO_ORD_DETAIL_ID = :NEW.BOGO_ORD_DETAIL_ID, BOGO_AR_DISCOUNT_TIERS_ID = :NEW.BOGO_AR_DISCOUNT_TIERS_ID, BOGO_EXCEPTION = :NEW.BOGO_EXCEPTION, PRICE_BOOK_ID = :NEW.PRICE_BOOK_ID, PRICE_BOOK_PRICE_TYPE_ID = :NEW.PRICE_BOOK_PRICE_TYPE_ID, NEW_SEQUENCE_COLUMN = :NEW.NEW_SEQUENCE_COLUMN
    WHERE dst.ID = :NEW.ID 
    OR dst.ORDERS_ID = (SELECT ORDERNO FROM ORDERS@local_link WHERE ID = :NEW.ORDERS_ID);
  ELSIF DELETING THEN
    DELETE FROM ORD_DETAIL@local_link
    WHERE ID = :OLD.ID AND ORDERS_ID = :OLD.ORDERS_ID;
  END IF;
END;
/

我已经为ord_detail创建了触发器。但不管用。请验证并更正代码

9wbgstp7

9wbgstp71#

“不工作”是什么意思?
由于TRG_ORDDETAIL_LOCALS是一个自治事务,它需要COMMITROLLBACK,而您既没有。
无论如何,我建议你考虑放弃这个模型,切换到其他的东西,例如物化视图你会刷新,例如。on commit
(正在刷新示例化视图)
可以使用ON COMMIT方法自动刷新实体化视图。因此,只要事务提交更新了定义物化视图的表,这些更改就会自动反映在物化视图中。使用此方法的优点是您永远不必记得刷新实体化视图。(...)

相关问题