PostgreSQL functions and triggers

孤街醉人 提交于 2021-02-04 13:38:25

问题


I am trying out functions and triggers int postgreSQL, however i am having a problem, when the function is triggered it is giving me an error

ERROR: control reached end of trigger procedure without RETURN

this particular procedure is only executing an insert into command so i do not see why it needs a return

this is the script:

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:


The error message tells you all. You need to do a RETURN from the trigger function:

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;

From the manual:

A trigger function must return either NULL or a record/row value having exactly the structure of the table the trigger was fired for.



来源:https://stackoverflow.com/questions/8805475/postgresql-functions-and-triggers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!