在Oracle中触发给出错误

时间:2015-03-06 18:30:47

标签: sql oracle triggers

这是我的sql代码:

CREATE OR REPLACE TRIGGER PLACE_NO_TRIGGER
BEFORE INSERT or UPDATE ON UHA_LEASE
FOR EACH ROW BEGIN
  INSERT INTO UHA_TEMPVAL SELECT PLACE_NO FROM UHA_RESHALL; 
  INSERT INTO UHA_TEMPVAL SELECT PLACE_NO FROM UHA_GENERAL;
  IF(:NEW.PLACE_NO NOT IN UHA_TEMPVAL.TEMP_VALUE AND :NEW.PLACE_NO IS NOT NULL) THEN
    RAISE_APPLICATION_ERROR(-10001, 'PLACE_NO MUST BE IN UHA_RESHALL OR IN UHA_GENERAL OR NULL');
  END IF;
  DELETE FROM UHA_TEMPVAL;
END;

这是出错:

Error(4,27): PLS-00103: Encountered the symbol "UHA_TEMPVAL" when expecting one of the following:     ( The symbol "(" was substituted for "UHA_TEMPVAL" to continue. 
Error(4,69): PLS-00103: Encountered the symbol "THEN" when expecting one of the following:     ) , and or as The symbol ")" was substituted for "THEN" to continue. 

有人可以帮助我理解为什么会发生这些错误吗?它特别奇怪,因为第4行在第60列结束。

谢谢!

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试将子表中的引用完整性强制实施为两个父表之一。您不需要从临时表中插入和删除;您可以计算两个表中匹配的行数:

CREATE OR REPLACE TRIGGER PLACE_NO_TRIGGER
BEFORE INSERT or UPDATE ON UHA_LEASE
FOR EACH ROW
DECLARE
  CNT NUMBER;
BEGIN
  SELECT COUNT(*)
  INTO CNT
  FROM (
    SELECT PLACE_NO FROM UHA_RESHALL
    WHERE PLACE_NO = :NEW.PLACE_NO
    UNION ALL
    SELECT PLACE_NO FROM UHA_GENERAL
    WHERE PLACE_NO = :NEW.PLACE_NO
  );

  IF :NEW.PLACE_NO IS NOT NULL AND CNT = 0 THEN
    RAISE_APPLICATION_ERROR(-10001,
      'PLACE_NO MUST BE IN UHA_RESHALL OR IN UHA_GENERAL OR NULL');
  END IF;
END;
/

如果:NEW.PLACE_NO不为null,您可以选择仅运行查询,但如果对列进行索引,则可能没有太大区别。