| From: | Tuukka Norri <tuukka(dot)norri(at)iki(dot)fi> |
|---|---|
| To: | pgsql-general(at)postgresql(dot)org |
| Subject: | Field names and NEW |
| Date: | 2006-05-05 08:50:05 |
| Message-ID: | C8F7A441-9DF5-48C8-812C-C537D2C461C2@iki.fi |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-general |
Greetings to everyone on the list
I am attempting to write a system which would copy primary key values
to another table after insert. To achieve this, I wrote a trigger
function in Perl. I also wanted to make the function reusable, so the
fields included in the query are passed as function arguments. Here
is the code:
CREATE OR REPLACE FUNCTION ModifyInsert () RETURNS TRIGGER AS $$
my $fieldnames = '';
my $fieldvalues = '';
my $mTablename = $_TD->{args}[1];
for (my $i = 2; $i < $_TD->{argc}; $i++)
{
my $currentName = $_TD->{args}[$i];
$fieldnames .= ", \"$currentName\"";
$fieldvalues .= ", '$_TD->{new}{$currentName}'";
}
my $query = "INSERT INTO $mTablename (pgts_modification_type
$fieldnames) VALUES ('I' $fieldvalues)";
spi_exec_query ($query);
return;
$$ LANGUAGE PLPERL;
However, I would rather use PL/PgSQL, since the Perl interpreter
might not be installed by default. Is there a way to access the NEW
record without knowing the field names in advance?
--
Best regards,
Tuukka Norri
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Rich Doughty | 2006-05-05 08:53:49 | Re: Adding ON UPDATE CASCADE to an existing foreign key |
| Previous Message | Paul Mackay | 2006-05-05 07:42:12 | Function query plan |