【问题标题】:Create function and trigger in SQL Developer?在 SQL Developer 中创建函数和触发器?
【发布时间】:2022-01-03 16:18:23
【问题描述】:

我有以下数据库

  • 客户(customerNO、customerName、address、city、category、custBalance)
  • 产品(产品编号、标签、价格、QStock)
  • CustomerOrder (orderNO, orderDate, #customerNO)
  • OrderedProduct(#orderNO、#productNO、orderQuantity)

并希望创建一个名为 changeCustomer_Category 的触发器,该触发器应在客户表中的任何客户余额更新之前调用。

它的工作是当客户的余额状态低于某个阈值(-10000)时,将客户的类别从 B2 修改为 B1,从 C2 修改为 C1。

我尝试了这个,但它不起作用

CREATE or REPLACE TRIGGER changeCustomer_Category
    AFTER update ON customer
    FOR EACH ROW
    ENABLE
    DECLARE 
     
   BEGIN

    
    END;

【问题讨论】:

    标签: oracle plsql triggers oracle-sqldeveloper


    【解决方案1】:

    我会想到

    SQL> create or replace trigger trg_au_cust
      2    before update on customer
      3    for each row
      4  begin
      5     :new.category := case when :new.custbalance < -10000 then
      6                           case when :new.category = 'B2' then 'B1'
      7                                when :new.category = 'C2' then 'C1'
      8                           end
      9                           else :new.category
     10                      end;
     11  end trg_au_cust;
     12  /
    
    Trigger created.
    

    测试:初始数据:

    SQL> select * from customer;
    
    CA CUSTBALANCE
    -- -----------
    B2        5000
    

    不应该影响任何东西(因为余额不低于-10000):

    SQL> update customer set custbalance = 100;
    
    1 row updated.
    
    SQL> select * from customer;
    
    CA CUSTBALANCE
    -- -----------
    B2         100
    

    设置非常低的余额:

    SQL> update customer set custbalance = -20000;
    
    1 row updated.
    
    SQL> select * from customer;
    
    CA CUSTBALANCE
    -- -----------
    B1      -20000             --> right; category is now B1
    
    SQL>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-25
      • 1970-01-01
      • 2011-03-22
      • 2015-06-27
      相关资源
      最近更新 更多