0
CREATE TRIGGER ci_insert
AFTER INSERT ON tb 
REFERENCING NEW AS nr
FOR EACH ROW
DECLARE
  date_from DATE;
  date_to   DATE;
  diff  NUMBER;
BEGIN
  SELECT DATE_FROM INTO date_from WHERE id = :nr.id;
  SELECT DATE_TO INTO date_to WHERE id = :nr.id;
  diff = date_to - date_from;
  UPDATE TB SET (diff) VALUES (diff) WHERE id = :nr.id;
END;

我使用 sqlplus,但它不起作用。它甚至没有显示任何错误。会是什么?

4

3 回答 3

1

错误很少。首先,你必须FROM在每一个选择中都有。其次,您将遇到关于变异表的运行时错误 - 您无法读取/写入在行级触发器中触发操作的同一个表。最后,你的UPDATE语法错误,它看起来更像INSERT; 更新语法是UPDATE table1 SET field1 = value1 WHERE ....

于 2012-12-21T19:39:04.123 回答
0
CREATE TRIGGER ci_insert
AFTER INSERT ON tb 
REFERENCING NEW AS nr
FOR EACH ROW
DECLARE
  date_from DATE;
  date_to   DATE;
  diff  NUMBER;
BEGIN
  SELECT DATE_FROM INTO date_from from TB WHERE id = :nr.id;
  SELECT DATE_TO INTO date_to from TB WHERE id = :nr.id;
  diff = date_to - date_from;
  UPDATE TB SET (diff) VALUES (diff) WHERE id = :nr.id;
END;
于 2012-12-21T18:03:22.847 回答
0

我想你所需要的就是这个——如果可能的话,我总是在现有的表上尝试我的查询。用 your_table... 替换表名:

   Begin
     Update emp_test
        Set empno = (Select trunc(sysdate)-to_date('20-DEC-2012') days_btwn From dual);
   End;
于 2012-12-26T21:15:22.987 回答