1

有没有办法解决这个错误,或者我的代码中缺少一些东西,我正在学习postgresql并且我一直在研究table inheritance and triggers我有两个表,temporary_object table并且persons tablepersons表继承了临时对象具有的所有属性,然后我有在persons表上创建了一个触发器函数,它检查记录与我尝试运行时出现的问题相同id before updating,我得到了这些tablesinsert queryerrors

ERROR: end of trigger procedure achieved without RETURN
CONTEXT: function PL / pgSQL muli_function ()

ERROR: end of trigger procedure achieved without RETURN
SQL-state: 2F005
Context: The PL / pgSQL muli_function ()

这是通过插入查询

INSERT INTO persons (id, time_create, time_dead, First_name, Last_name) values (1, '2011-10-07 15:25:00 EDT', '2011-10-07 3:25 PM EDT', 'sathiya', 'james');

这是我的触发功能并自行触发

CREATE FUNCTION muli_function() RETURNS trigger AS '
BEGIN
  IF tg_op = ''UPDATE'' THEN
     UPDATE persons
     SET time_dead = NEW.time_create
     Where
         id = NEW.id
         AND time_dead IS NULL
         ;
  RETURN new;
  END IF;
END
' LANGUAGE plpgsql;

CREATE TRIGGER sofgr BEFORE INSERT OR DELETE OR UPDATE
        ON persons FOR each ROW
        EXECUTE PROCEDURE muli_function();

这是我的两个表查询

CREATE TABLE temporary_object
(
  id integer NOT NULL,
  time_create timestamp without time zone NOT NULL,
  time_dead timestamp without time zone,
  PRIMARY KEY (id, time_create)
);

CREATE TABLE persons
(

  First_Name text,
  Last_Name text
)
INHERITS (temporary_object);
4

2 回答 2

2

试试这个:

CREATE FUNCTION muli_function() RETURNS trigger AS '
BEGIN
  IF tg_op = ''UPDATE'' THEN
     UPDATE persons
     SET time_dead = NEW.time_create
     Where
         id = NEW.id
         AND time_dead IS NULL
         ;

  END IF;
  RETURN new;
END
' LANGUAGE plpgsql;

UPD 更好的是这样的:

CREATE FUNCTION muli_function() RETURNS trigger AS '
BEGIN
  IF tg_op = ''UPDATE'' THEN
    IF NEW.time_dead IS NULL THEN
     NEW.time_dead = NEW.time_create
    END IF;      
  END IF;
  RETURN new;
END
' LANGUAGE plpgsql;
于 2012-12-07T08:20:22.880 回答
1

您为触发器定义了触发器过程INSERT OR UPDATE,但没有为 提供案例INSERT(就像注释 mu 太短一样),只有UPDATE. 所以触发器执行 a 的过程INSERT,然后它没有找到任何要执行的东西并没有 a 就退出RETURN,这显然是一个错误。

于 2012-12-07T08:35:16.270 回答