我有一段代码不起作用,Oracle告诉我触发器创建时出现了构建错误。很明显,我无法获得更精确的构建错误信息...
我之前并没有接触过太多的SQL,所以对语法不是很熟悉。我猜想可能是我的 IF EXISTS (SELECT ...) THEN 语句让 Oracle 不满意。我在 Google 上搜索了类似的示例,但在我的情况下没有找到可行的解决方案。
那么关于这段代码:
- "debut" is a date attribute (it means start)
- "fin" is another date attribute (it means end)
- I want to make sure the NEW line's dates don't overlap with any other line's in the table if these 2 lines have the same "numInfirmier" attribute.
- So I SELECT all the lines that have the same numInfirmier as the NEW line and overlapping dates.
And IF anything EXISTS in that select, I raise an error.
CREATE OR REPLACE TRIGGER chev_surv BEFORE INSERT OR UPDATE ON surveillance FOR EACH ROW BEGIN IF EXISTS ( SELECT * FROM surveillance WHERE surveillance.numInfirmier = :NEW.numInfirmier AND ((surveillance.debut > :NEW.debut AND surveillance.debut < :NEW.fin) OR (surveillance.fin > :NEW.debut AND surveillance.fin < :NEW.fin)) ) THEN RAISE_APPLICATION_ERROR(-20001, 'Il ne doit pas y avoir de chevauchement entre deux périodes surveillance pour un surveillant.'); END IF; END; /
有什么问题吗?
show errors
获取有关实际错误的更精确信息,或者使用select * from user_errors where type = 'TRIGGER' and name = 'CHEV_SURV'
。这适用于任何存储的PL/SQL。您是正确的,您不能在select
条件之外使用exists
。但即使您将其编译,也会出现表变异错误 - 您无法查询触发器所针对的表。 - Alex Pooleselect ... from ... where exists (select ... from ...)
. Justin的答案向你展示了在这种情况下应该如何检查,以及为什么它仍然无法工作 *8-) - Alex Poole