PostgreSQL函数和触发器

时间:2012-01-10 15:09:57

标签: function postgresql stored-procedures triggers return

我正在尝试函数并触发int postgreSQL,但是我遇到了问题,当函数被触发时它给了我一个错误

错误:控制在没有RETURN的情况下达到了触发程序的结束

这个特殊的过程只是执行插入命令,所以我不明白为什么它需要返回

这是剧本:

CREATE OR REPLACE FUNCTION forest_aud_func() returns trigger as $tree_stamp$ 
BEGIN
    insert into Audit values('k',124,'l');
END;
$tree_stamp$
LANGUAGE plpgsql;

create trigger forest_aud_ins after insert on forest
for each row execute procedure forest_aud_func()

insert into forest values('Blue',1600,'Malta','Health Ltd')

1 个答案:

答案 0 :(得分:16)

错误消息告诉您所有。您需要从触发器功能执行RETURN:

CREATE OR REPLACE FUNCTION forest_aud_func() returns trigger as $tree_stamp$ 
BEGIN
    insert into Audit values('k',124,'l');
    return new;
END;
$tree_stamp$
LANGUAGE plpgsql;

来自the manual

触发器函数必须返回NULL或具有触发器触发的表格结构的记录/行值。