A trigger that acts on a column with a given column name

From: BigSmoke <bigsmoke(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: A trigger that acts on a column with a given column name
Date: 2005-07-07 19:28:23
Message-ID: b9b59ba0050707122873540efd@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

In a trigger function, I'm trying to refer to a column given by an argument
to the trigger function.

The trigger function fires on deletes and is responsible for deleting messages
that are referred to using foreign keys which are named differently in different
tables (thus the foreign key's column name as an argument).

Thus, in my trigger function, I need something like this:

DECLARE
message_id INTEGER;
BEGIN
message_id := quote_literal('OLD.' || TG_ARGV[0]);

This, of course, doesn't get me the value of the column named TG_ARGV[0] in
OLD, but instead just gives me an error because I'm trying to assign the
string value returned by quote_literal() to an integer.

So, my question is: how can I convince PostgreSQL that a string which I've
constructed is in fact the name of a column? I've tries some variation with
SELECT INTO, but I keep bumping into the same question ...

Any help much so appreciated.

- Rowan

--
Morality is usually taught by the immoral.

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Michael Fuhr 2005-07-07 20:41:09 Re: table name as variable within Function
Previous Message Ying Lu 2005-07-07 18:35:47 find objects under a specific tablespace