pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ken Winter <ken@sunward.org>
To: PostgreSQL pg-sql list <pgsql-sql@postgresql.org>
Date: Wed, 23 Nov 2005 23:23:05 -0500
Message-ID: <004e01c5f0ae$c4d4bfe0$6503a8c0@kenxp> (raw)

In PL/pgSQL, is there a way to put a *variable* column-name in a dot
notation reference to a RECORD column?

For example, suppose I want to write a function like the following, which is
to be called by a "BEFORE INSERT" trigger:

CREATE OR REPLACE FUNCTION foo (  ) RETURNS TRIGGER AS 
    '
    DECLARE 
        var VARCHAR;
    BEGIN
        var := TG_ARGV[0]       
        NEW.<the column whose name is the value of var> := ''whatever'';
        RETURN NEW;     
    END;
    '
    LANGUAGE 'plpgsql'
;

The aim of this uninteresting function is to assign the value 'whatever' to
the table column that is passed in by the calling trigger as TG_ARGV[0],
i.e. the first calling argument.  

What I don't know is what to put into the dot notation in place of ".<the
column whose name is the value of var>" so that the column of NEW that is
addressed by the assignment statement is the one passed in as the first
argument.  Is there any PL/pgSQL construct that could be substituted in here
to achieve this result?

If not, can anybody suggest a way to write a trigger-called function that
would accomplish the same result?

In case it's not obvious, the underlying goal is to write a single
trigger-called function that could modify different columns for each trigger
that called it, where each trigger specified the target column by a calling
argument (or by any other viable mechanism).

~ TIA
~ Ken




view thread (3+ messages)  latest in thread

Message-ID: <004e01c5f0ae$c4d4bfe0$6503a8c0@kenxp>
Permalink:  ../004e01c5f0ae$c4d4bfe0$6503a8c0@kenxp/
Also on:    postgresql.org/message-id/004e01c5f0ae$c4d4bfe0$6503a8c0@kenxp

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: ken@sunward.org
  Subject: Re: 
  In-Reply-To: <004e01c5f0ae$c4d4bfe0$6503a8c0@kenxp>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox