Thread: require some suggestion to write trigger function

require some suggestion to write trigger function

From
Jignesh Shah
Date:
Hi, could any one please help me with below situation. Thanks for your time in advance.
 
I have two tables Foo1(c1,c2,c3) and Foo2(x1,x2,x3). I have written trigger for Foo1 and it is working fine. The trigger updates the log table when new insertion happened in Foo1. For this, I have used below code.
CREATE OR REPLACE FUNCTION my_triggert() RETURNS trigger AS $$
    $rv = spi_exec_query("insert into logtable VALUES($_TD->{new}{c1}, '$_TD->{new}{c2}','$_TD->{new}{c3}');");
    ($rv->{processed} == 0 && $rv->{status} eq "SPI_OK_INSERT") ?
            elog(INFO, "Success") :
            elog(INFO, "Failed");
$$ LANGUAGE plperl;
 
I have hard coded the columns name c1, c2, c3. Is it possible to get rid of hard coding c1, c2 and c3 so that I can use same my_trigger function for Foo2(x1, x2, x3) as well? Any suggestion would be really helpful for me.
 
Thanks,
Jignesh

Re: require some suggestion to write trigger function

From
jr
Date:
hi

> I have hard coded the columns name c1, c2, c3. Is it possible to get rid
> of hard coding c1, c2 and c3 so that I can use same my_trigger function
> for Foo2(x1, x2, x3) as well? Any suggestion would be really helpful for me.

read manual re. OLD.* and NEW.* variables.

--

regards, jr.  (jr@tailorware.org.uk)