为什么我的触发器在Oracle SQL中一直要求我提供绑定变量

mefy6pfw  于 2023-06-22  发布在  Oracle
关注(0)|答案(1)|浏览(102)

我有这个触发器,每当使用DIMPRODUCT表中的PRODUCTACTUALCOST AND PRODUCESALESCOSTFACTPRODUCTSALES中插入值时,我都会尝试更改SALESTOTALCOST AND PRODUCTACTUALCOST
代码如下:

CREATE TRIGGER FACTPRODUCTSALES_INSERT 
AFTER INSERT ON FACTPRODUCTSALES
FOR EACH ROW
DECLARE
    ACTUALCOST NUMBER;
    SALESCOST  NUMBER;
BEGIN
    SELECT PRODUCTACTUALCOST INTO ACTUALCOST FROM DIMPRODUCT WHERE PRODUCTKEY = :NEW.PRODUCTID;
    SELECT PRODUCTSALESCOST INTO SALESCOST FROM DIMPRODUCT WHERE PRODUCTKEY = :NEW.PRODUCTID;
    
    :NEW.SALESTOTALCOST = SALESCOST * :NEW.QUANTITY;
    :NEW.PRODUCTACTUALCOST = ACTUALCOST * :NEW.QUANTITY;
END;

但它一直要求我提供绑定变量;这是什么?为什么?我该怎么办?
我已经试过根据thisthis中的答案做SET DEFINE OFF,但它没有工作。

pieyvz9o

pieyvz9o1#

假设表存在并且包含所涉及的列,则:

  • 触发器必须是before(第1行)(如果要修改:new值)
  • 使用:=代替=(第10、11行)
SQL> CREATE OR REPLACE TRIGGER FACTPRODUCTSALES_INSERT before INSERT ON FACTPRODUCTSALES
  2  FOR EACH ROW
  3  DECLARE
  4      ACTUALCOST NUMBER;
  5      SALESCOST  NUMBER;
  6  BEGIN
  7      SELECT PRODUCTACTUALCOST INTO ACTUALCOST FROM DIMPRODUCT WHERE PRODUCTKEY = :NEW.PRODUCTID;
  8      SELECT PRODUCTSALESCOST INTO SALESCOST FROM DIMPRODUCT WHERE PRODUCTKEY = :NEW.PRODUCTID;
  9
 10      :NEW.SALESTOTALCOST := SALESCOST * :NEW.QUANTITY;
 11      :NEW.PRODUCTACTUALCOST := ACTUALCOST * :NEW.QUANTITY;
 12  END;
 13  /

Trigger created.

SQL>

相关问题