使用插入触发器比较两个日期

时间:2013-12-01 18:19:41

标签: sql oracle triggers

我想确保子订单日期不在使用此触发器的订单日期之前:::

CREATE OR REPLACE TRIGGER suborder_date before
  INSERT ON suborder FOR EACH row DECLARE suborder_dd DATE;
  orderdate DATE;
  BEGIN
    SELECT reqshipdate INTO suborder_dd FROM suborder;
    SELECT orders.orddate INTO orderdate FROM orders;
    IF orderdate < suborder_dd THEN
      raise_application_error(-20002,('Required Ship Date is before Order Date'));
    END IF;
  END;

这个编译现在我想测试它并将其插入表中... 插入子顺序值(1,1,'12 -Jan-13','13-Jan-14','Office Depot',1)

  

错误:在命令的第1行开始出错:插入子顺序   值(1,1,'12 -Jan-13','13-Jan-14','Office Depot',1)错误报告:   SQL错误:ORA-01422:精确提取返回超过请求的数量   行ORA-06512:在“BB.SUBORDER_DATE”,第4行ORA-04088:错误   执行触发器'BB.SUBORDER_DATE'期间   01422. 00000 - “确切的提取返回超过请求的行数”   *原因:精确提取中指定的数字小于返回的行数。   *操作:重写查询或更改请求的行数

在我的帮助下,当我为自己编辑命令时,我终于得到了我想要的输出。谢谢!

CREATE OR REPLACE TRIGGER suborder_date BEFORE
  INSERT ON suborder FOR EACH row DECLARE reqship DATE; orderdate date;
  BEGIN
    SELECT o.orddate
    INTO orderdate
    FROM orders o
    WHERE o.orderno = :new.orderno;
    IF orderdate     > :new.reqshipdate THEN
      raise_application_error(-20002, ('Required Ship Date is before Order Date'));
    END IF;
  END;

1 个答案:

答案 0 :(得分:1)

如果你按照评论,你应该得到这样的东西:

CREATE OR REPLACE TRIGGER suborder_date
  BEFORE INSERT ON suborder 
  FOR EACH row 
  DECLARE 
    orderdate DATE;
  BEGIN

    SELECT o.orddate
    INTO   orderdate
    FROM   orders o
    WHERE  o.order_id = :new.order_id;

    IF orderdate < :new.suborder_dd THEN
      raise_application_error(-20002, ('Required Ship Date is before Order Date'));
    END IF;
  END;