如果超出现有数量

时间:2018-09-26 01:20:34

标签: sql oracle

需要在Oracle中创建一个触发器,如果​​数量大于“项目”表中“现有数量”中的数量,则该订单将阻止Orders输入到ItemsOrder表中。

这是我到目前为止所拥有的:

create or replace
TRIGGER check_qty
AFTER UPDATE on OrderItems
FOR EACH ROW
BEGIN
  IF(SELECT
      OrderItems.quantity, Items.quantityOnHand FROM Items
      INNER JOIN OrderItems On Items.itemID = OrderItems.itemID
      WHERE Items.quantityOnHand < OrderItems.quantity);
      raise_application_error(-20999,'The quantity amount is greater than the unite available');
  ELSE
    dbms_output.put_line('Success');
  END IF;
END

出现以下错误:

错误(2,6):PLS-00103:预期以下情况之一时遇到符号“ SELECT”:(-+ case mod new not null继续avg计数当前存在最大值min先前的sql stddev总和方差执行forall合并时间时间戳记间隔日期管道

错误(4,13):PLS-00103:预期以下情况之一时遇到符号“ JOIN”:对于具有相减负序的组,在连接处开始联合

1 个答案:

答案 0 :(得分:0)

我会这样想:

create or replace trigger check_qty before update on OrderItems
for each row as
    v_quantity number;
BEGIN
    select i.quantityOnHand into v_quantity
    from items i
    where i.itemId = :new.itemId;

    if (v_quantity < :new.quantity) then
      raise_application_error(-20999, 'The quantity amount is greater than the unite available');
    end if;
end;