Oracle SQL错误PLS-00049:错误的绑定变量'NEW.NOOFCUSTOMERS'

时间:2013-10-28 06:35:34

标签: sql oracle oracle11g oracle-sqldeveloper

我必须创建一个触发器来添加NoOfCustomers'进入STORE表。如果客户数量大于限制,则会显示错误消息。我收到了错误"错误的绑定变量' NEW.NOOFCUSTOMERS'"当我试图运行触发器时,我似乎找不到原因。

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; 

1 个答案:

答案 0 :(得分:1)

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

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

    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;