Re: Variable column name in plpgsql function

From: mark <postgres(at)markreid(dot)org>
To:
Cc: George Woodring <george(dot)woodring(at)iglass(dot)net>, pgsql-general(at)postgresql(dot)org
Subject: Re: Variable column name in plpgsql function
Date: 2004-12-01 17:46:45
Message-ID: 41AE0385.5040505@markreid.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Richard Huxton wrote:

> George Woodring wrote:
>
>>
>> Is possible to declare a variable to build the column name
>>
>> Tableid varchar(20) := TG_RELNAME || ''id'';
>>
>> And then use this variable to get the PK value?
>>
>> Tableidvalue int4 := NEW.tableid;
>
>
> No. You can build a dynamic query via EXECUTE, but that can't access
> NEW/OLD.
>
If there's only the two possibilities, you could just use something like
IF TG_RELNAME = 'table1' THEN
id := NEW.table1id;
ELSE
id := NEW.table2id;
END IF;

But dynamically referring to NEW.variable doesn't work in plpgsql.
Apparently you can do this in some of the other pl languages though
(plperl for example).

-Mark.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sally Sally 2004-12-01 18:09:37 autocommit and transactions
Previous Message Richard Huxton 2004-12-01 17:32:38 Re: Variable column name in plpgsql function