【问题标题】:Live SQL problems in storing procedures and triggers存储过程和触发器中的实时 SQL 问题
【发布时间】:2021-12-01 11:50:52
【问题描述】:

我在将存储过程应用到我的数据库时遇到问题。 在我的 order_table 中,我有以下属性 order_table(orderID、数量、价格、折扣)。 当数量超过 500 时,我需要将折扣值设置为“是”,并将价格设置为 15% 的折扣。 我试图使用以下语法使用触发器:

create trigger discount_t
before insert or update on order_table
for each row
begin
if quantity > 500
then (discount = 'Yes') and (price = price - ((price*15)/100);
end if;
end;

使用此语法时,我收到以下错误:

Errors: TRIGGER DISCOUNT_T
Line/Col: 3/15 PLS-00103: Encountered the symbol "=" when expecting one of the following:

   := . ( @ % ;

Line/Col: 3/59 PLS-00103: Encountered the symbol ";" when expecting one of the following:

   ) , * & - + / at mod remainder rem <an exponent (**)> and or
   || year day

我对 Oracle/Live SQL 不是很熟悉,所以有人知道我做错了什么吗?

【问题讨论】:

    标签: sql oracle


    【解决方案1】:

    使用:new 绑定变量引用行的新值,使用:= 进行PL/SQL 中的赋值:

    create trigger discount_t
    before insert or update on order_table
    for each row
    begin
      if :NEW.quantity > 500 then
        :NEW.discount := 'Yes';
        :NEW.price := 0.85 * :NEW.price;
      end if;
    end;
    /
    

    那么,对于表:

    CREATE TABLE order_table (
      quantity NUMBER,
      discount VARCHAR2(3) DEFAULT 'No',
      price    NUMBER
    );
    

    如果你:

    INSERT INTO order_table (quantity, price) VALUES (1000, 100);
    
    SELECT * FROM order_table;
    

    那么输出是:

    QUANTITY DISCOUNT PRICE
    1000 Yes 85

    db小提琴here

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 2011-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-30
      相关资源
      最近更新 更多