dynamic plpgsql question

From: Marc Evans <Marc(at)SoftwareHackery(dot)Com>
To: pgsql-general(at)postgresql(dot)org
Subject: dynamic plpgsql question
Date: 2006-12-13 15:58:53
Message-ID: 20061213105033.S4899@me.softwarehackery.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi -

I am struggling with a trigger function in plpgsql, and am hoping that
someone on this list can't show me a way to do what I need.

In the trigger, TG_ARGV[0] is the name of a column that I want to
evaluate. This code shows the concept, though is not functional:

CREATE OR REPLACE FUNCTION foo() RETURNS TRIGGER AS $$
DECLARE
column_name TEXT := TG_ARGV[0];
data TEXT;
BEGIN
EXECUTE 'SELECT NEW.' || column_name INTO data;
-- ...
END;
$$ LANGUAGE plpgsql;

When I try to use that code, I receive:

c3i=> insert into test_table values (1,1);
ERROR: NEW used in query that is not in a rule
CONTEXT: SQL statement "SELECT NEW.magic"

How can I get the value of NEW.{column_name} (aka NEW.magic in this
specific test case) into the variable data?

For completeness, I am using version 8.2.0.

Thanks in advance - Marc

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2006-12-13 16:04:54 Re: [girgen@FreeBSD.org: Re: port fault on pg_ctl's place]
Previous Message Gurjeet Singh 2006-12-13 14:53:54 Re: PG Admin