Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL functions and triggers

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')
like image 316
Karl Avatar asked Jan 10 '12 15:01

Karl


1 Answers

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.

like image 191
a_horse_with_no_name Avatar answered Sep 19 '22 01:09

a_horse_with_no_name