August 21st, 2001, 01:09 AM
Creating Trigger for loging table
Iam trying to make a trigger for backup table. When a delete operation for table1, it will copy deleted data to table2.
create table1 (id int4, name text);
create table2 (id int4, name text);
function execute by trigger:
create function backup_func() returns OPAQUE as '
insert into tabel2(id, nama) values (OLD.id, OLD.nama);
' language 'plpgsql';
trigger for table1:
create trigger mytrigger after delete on tabel1 for each row execute procedure backup_func();
then insert some data:
insert into table1 (id, name) values (1, 'Mao Tze Dong');
insert into table1 (id, name) values (2, 'Hitler');
All above operations are doing fine. but when deleting data in table1 an error occor:
delete from table1 where id = 2;
ERROR: control reaches end of trigger procedure without RETURN
please help.. how to make a trigger for a table so that when the table do delete operation it can backup into anoter table?
Last edited by bambanghendra; August 22nd, 2001 at 03:35 AM.
August 22nd, 2001, 11:48 PM
I have never written a trigger in Postgresql. The documentation says:
"...they must return either NULL or a record/row containing exactly the structure of the table the trigger was fired for."
Based on your error message and the documentation above, would adding a RETURN NULL; before the end; statement solve your problem? Postgresql does not appear to like that your trigger doesn't have a return value.
This is just a guess.
August 23rd, 2001, 05:33 AM
Yes... a function shoud have a return value
But RETURN OLD or RETURN NEW will be fine.