![](/img/trans.png)
[英]How to create the correct trigger instead of insert or update? ORACLE. Trigger. pl/sql
[英]Oracle PL/SQL Trigger: update/insert with where condition
也許這很容易,但我沒有前進。 表B
包含兩個字段: product_no NUMBER
和product VARCHAR2
。 觸發器應該執行的操作是從BP-table
中插入相應的產品描述,在該BP-table
中您還具有字段product_no
和product
。 例如,您在B.product_no
輸入20,則應該從BP-Table
自動輸入正確的產品。 Oracle版本是11g。
CREATE OR REPLACE TRIGGER A.trigger_on_B
AFTER INSERT OR UPDATE
ON A.B FOR EACH ROW
DECLARE
varProduct varchar(20);
BEGIN
SELECT bp.report_info
into varProduct
from bp,
B
where bp.product_no = B.product_no;
insert into B (product) values(varProduct) ;
exception
when others then NULL;
END;/
您可以使用偽記錄:new,:old來使其工作:
CREATE OR REPLACE TRIGGER A.trigger_on_B
AFTER INSERT OR UPDATE
ON A.B FOR EACH ROW
DECLARE
varProduct varchar(20);
BEGIN
SELECT bp.report_info
into varProduct
from bp
where bp.product_no = :new.product_no;
insert into B (product) values(varProduct) ;
exception
when others then NULL;
END;
when others then NULL;
擺脫該行when others then NULL;
這是不適當的,您無法隱藏任何異常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.