1

I have to create a trigger to add 'NoOfCustomers' into the STORE table. If the number of customers is greater than the limit then it will display an error message. I'm getting the error "bad bind variable 'NEW.NOOFCUSTOMERS'" when trying to run the trigger and I can't seem to work out why.

CREATE OR REPLACE TRIGGER NoOfCustomersTrigger 
      BEFORE INSERT ON STORE
      FOR EACH ROW 
      DECLARE V_CAPACITY SHOPS.LIMIT%TYPE;
    BEGIN 
          SELECT LIMIT INTO V_LIMIT
          FROM SHOPS, SERVES
          WHERE CUSTID = SERVES.CUSTID AND STORENO = :NEW.STORENO;
          IF (:NEW.NOOFCUSTOMERS > V_LIMIT) THEN 
            RAISE_APPLICATION_ERROR (-20004,'The Number of Customers exceeds the LIMIT'); 
          END IF; 
        END; 
4

1 回答 1

1

您的代码中似乎有一些错误,

  1. V_LIMIT在声明部分中使用,而不是在代码V_CAPACITY中使用。v_limit
  2. sql 语句会抛出column ambiguously defined错误,列必须引用为 TABLE.COLUMN_NAME 或 TABLE_ALIAS.COLUMN_NAME。

像这样试试

CREATE OR REPLACE 
TRIGGER noofcustomerstrigger 
      BEFORE INSERT ON STORE
      FOR EACH ROW 
DECLARE v_limit shops.LIMIT%TYPE;
BEGIN 
     SELECT LIMIT INTO v_limit
     FROM shops A, serves b
     WHERE A.custid = b.custid AND storeno = :NEW.storeno;

     IF (:NEW.noofcustomers > v_limit) THEN 
          raise_application_error (-20004,'The Number of Customers exceeds the LIMIT'); 
     END IF; 
END;
于 2013-10-28T07:03:51.853 回答