查找最大计数是否超过允许的设置限制

时间:2014-11-08 18:00:56

标签: database oracle

根据计划中设定的限制,有以下触发器来查找每月允许的最大租金。代码编译,但我错过了支票。有人可以帮忙吗?

CREATE OR REPLACE TRIGGER max_dvd_count
BEFORE UPDATE OR INSERT
ON RENTAL
FOR EACH ROW

DECLARE
member_limit NUMBER(2);

BEGIN


SELECT COUNT(RENTAL.MEMBERID) INTO member_limit
FROM MEMBER, MEMBERSHIP, RENTAL
WHERE MEMBERSHIP.MEMBERSHIPID = MEMBER.MEMBERSHIPID
AND MEMBER.MEMBERID = RENTAL.MEMBERID;


raise_application_error (-20000, 'You have exceeded monthly rental limit');
END;

1 个答案:

答案 0 :(得分:0)

假设plan1 / plan2在字段plan_name中。您需要group byplan_name并获得如下计数。另请注意,我已更改您的查询,以使用显式连接语法而不是旧式隐式连接语法。

SELECT COUNT(RENTAL.MEMBERID) INTO member_limit
FROM MEMBER
JOIN MEMBERSHIP ON MEMBERSHIP.MEMBERSHIPID = MEMBER.MEMBERSHIPID
JOIN RENTAL ON MEMBER.MEMBERID = RENTAL.MEMBERID
GROUP BY plan_name;

现在,您可以使用IF条件轻松检查条件,例如

IF(member_limit > 4) THEN
raise_application_error (-20000, 'You have exceeded monthly rental limit for Plan1');
ELSE
raise_application_error (-20000, 'You have exceeded monthly rental limit for Plan2');
END IF;