简体   繁体   English

Oracle PL / SQL触发器:使用where条件更新/插入

[英]Oracle PL/SQL Trigger: update/insert with where condition

Maybe it's quite easy but I don't get ahead. 也许这很容易,但我没有前进。 The table B contains two fields: product_no NUMBER and product VARCHAR2 . B包含两个字段: product_no NUMBERproduct VARCHAR2 What the trigger should do is to insert a corresponding product description from BP-table in which you also have the fields product_no and product . 触发器应该执行的操作是从BP-table中插入相应的产品描述,在该BP-table中您还具有字段product_noproduct Eg you enter 20 in B.product_no then it should be automatically entered the right product from BP-Table . 例如,您在B.product_no输入20,则应该从BP-Table自动输入正确的产品。 Oracle version is 11g. 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;/

You can user pseudo records :new, :old, to make it work: 您可以使用伪记录: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;
  • Get rid of the line when others then NULL; when others then NULL;摆脱该行when others then NULL; this is unappropriated you can't hide any exceptions. 这是不适当的,您无法隐藏任何异常。
  • Get rid of your triggers, it is hard to maintenance code with logic in triggers. 摆脱触发器,很难用触发器中的逻辑来维护代码。 For more information check this article 有关更多信息,请查看本文

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

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