Oracle触发器错误PL / SQL:ORA-00913:值太多

时间:2012-03-24 08:20:05

标签: sql oracle triggers ora-00913

我正在尝试创建一个触发器但是有错误....

SQL> CREATE OR REPLACE TRIGGER INV_TOTAL
  2  BEFORE INSERT OR UPDATE ON INVOICE
  3  FOR EACH ROW
  4  BEGIN
  5     SELECT
  6        NVL((SELECT R.SUBTOTAL FROM HOLIDAY_RESERVATION R WHERE R.RESV_ID = :NEW.INV_ID), 0) +
  7       NVL((SELECT R.SUBTOTAL, (R.SUBTOTAL*20)/100 FROM HOLIDAY_RESERVATION R WHERE R.RESV_ID = :NEW.RESV_ID),0)
  8        INTO :NEW.INV_TOTAL_PRICE
  9     FROM DUAL;
 10  END;
 11  /

Warning: Trigger created with compilation errors.

SQL> SHOW ERRORS;
Errors for TRIGGER INV_TOTAL:

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/4      PL/SQL: SQL Statement ignored
4/9      PL/SQL: ORA-00913: too many values
SQL>

它在哪里找到太多的值,我想要的是在字段小计中选择什么,添加20%并更新另一个表中的字段

2 个答案:

答案 0 :(得分:1)

难道你不能做这样的事情:

CREATE OR REPLACE TRIGGER INV_TOTAL
BEFORE INSERT OR UPDATE ON INVOICE
FOR EACH ROW
BEGIN
   -- Add 20% to subtotal and populate inv_total_price
   SELECT (NVL(r.subtotal, 0) * 1.2) -- Multiplying by 1.2 adds 20% to the total
     INTO :NEW.INV_TOTAL_PRICE
     FROM holiday_reservation r
    WHERE r.resv_id = :NEW.inv_id;
EXCEPTION
   WHEN no_data_found
   THEN
      -- Set the inv_total_price to zero as there was no corresponding 
      -- record in holiday_reservation.
      :NEW.INV_TOTAL_PRICE := 0;
END;
/

我现在面前没有一个终端来测试这个。 :-(
N.B.:您可能会或可能不想要例外部分。
希望它有所帮助...

答案 1 :(得分:0)

“值太多”错误背后的原因是您在select语句中执行以下操作:

NVL((SELECT R.SUBTOTAL FROM HOLIDAY_RESERVATION R WHERE R.RESV_ID = :NEW.INV_ID), 0) ' 1 value
NVL((SELECT R.SUBTOTAL, (R.SUBTOTAL*20)/100 FROM HOLIDAY_RESERVATION R WHERE R.RESV_ID = :NEW.RESV_ID),0) ' gives 2 values (subtotal, subtotal*20 / 100)

使用2个值调用NVL会抛出ORA-00913。即使它没有,当你试图用2添加1值时,它最终会抛出。

另外,我会在每个声明旁边设置完全限定的模式名称。