Skip site navigation (1) Skip section navigation (2)

Re: How to dynamically call a column in plpgsql

From: "Henshall, Stuart - Design & Print" <SHenshall(at)westcountry-design-print(dot)co(dot)uk>
To: 'Sigurdur Helgason' <shelgason(at)yahoo(dot)com>,pgsql-novice(at)postgresql(dot)org
Subject: Re: How to dynamically call a column in plpgsql
Date: 2002-10-28 12:48:33
Message-ID: E2870D8CE1CCD311BAF50008C71EDE8E0506DC76@MAIL_EXCHANGE (view raw or flat)
Thread:
Lists: pgsql-novice
Sigurdur Helgason wrote:
> Hi!
> 
> I am trying to fetch a value from a column, where the
> columnname is a value in another column.
> 
> See below ..
> 
> table constraint_1 (
> ...
> cons_col VARCHAR(15) NOT NULL,
> ...
> )
> 
> 
> Then in the trigger (written in plpgsql) I want to
> fetch the appropriate column from the NEW record.
> --- trigger --
> bla bla bla...
> 
> select cons_col from constraint_1 where bla = bla ...
> -- the_val := NEW.cons_col;
> 
> bla. bla .bla.
> return NEW;
> bla bla bla..
> ----
> 
> Well I have tried a couple different things .. none
> seem to work. I know that this is possible if the
> trigger is written in C .. but it might be nice to
> know how to do this in plpgsql.
> Anyone out there that knows how to do this?
> 
> 
I think something like the following should do:
SELECT INTO cl_nm cons_col FROM constrait_1 WHERE ..........
EXECUTE ''SELECT * FROM tbl WHERE '' || quote_ident(cl_nm) || ''=.......'';
the plpgsql docs contain more docs for EXECUTE (such as when to use
quote_ident)
hth,
- Stuart

Responses

pgsql-novice by date

Next:From: Sigurdur HelgasonDate: 2002-10-28 13:12:19
Subject: Re: How to dynamically call a column in plpgsql
Previous:From: Sigurdur HelgasonDate: 2002-10-28 08:30:10
Subject: How to dynamically call a column in plpgsql

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group