我正在尝试创建一个触发器,如果客户总数达到10,那么它将引发错误
这是我的plsql代码:
SQL> CREATE OR REPLACE TRIGGER customer_count_check
2 BEFORE INSERT OR UPDATE ON customer2
3 FOR EACH ROW
4 DECLARE
5 count_customer NUMBER(5);
6 BEGIN
7 SELECT COUNT(*) INTO count_customer FROM customer2
8 WHERE cusid = :new.cusid;
9 IF count_customer > 10 THEN
10 Raise_application_error(-20000,'Maximum customer count:' ||
11 count_customer || 'reached');
12 ENDIF;
13 END;
14 /
这是我得到的警告:
Warning: Trigger created with compilation errors. SQL> SHOW ERROR; Errors for TRIGGER CUSTOMER_COUNT_CHECK: LINE/COL ERROR -------- ----------------------------------------------------------------- 10/4 PLS-00103: Encountered the symbol ";" when expecting one of the following: if
答案 0 :(得分:2)
我有时也希望错误消息会更清楚。
第12行:
错
ENDIF;
右:
END IF;
行计数有时也不太直观。它表示为10/4,而似乎为12/4。但是前2行不被视为触发器代码的一部分,因此计数是第10行第4列。
答案 1 :(得分:0)
SQL> CREATE OR REPLACE TRIGGER customer_count_check
2 BEFORE INSERT OR UPDATE ON customer2
3 FOR EACH ROW
4 DECLARE
5 count_customer NUMBER(5);
6 BEGIN
7 SELECT COUNT(*) INTO count_customer FROM customer2
8 WHERE cusid = :new.cusid;
9 IF count_customer > 10 THEN
10 Raise_application_error(-20000,'Maximum customer count:' ||
11 count_customer || 'reached');
12 END IF;
13 END;
14 /