Re: PLPGSQL - Mailing list pgsql-general

From Sean Davis
Subject Re: PLPGSQL
Date
Msg-id 1ab1ef1d7ebff80f0b683bb9f33ea259@mail.nih.gov
Whole thread Raw
In response to PLPGSQL  (Shaun Clements <ShaunC@relyant.co.za>)
List pgsql-general
On Mar 23, 2005, at 7:56 AM, Shaun Clements wrote:

> Is there no way in pgplsql
> to call on a dynamic column.
> I need to be able to dynamically determine the latest month column
> within a dataset, and to get that columns data.
> I am unfamiliar with other languages within Postgres
>
>  Kind Regards,
> Shaun Clements
>

You can get all the column names for a table called 'testtable' using:

select a.attname
    from
        pg_attribute a,
        pg_class c
    where
        a.attrelid=c.oid and
        a.attnum>0 and
        c.relname='testtable';

You can then decide what column to use based on whatever logic you
like.  You will then need to construct the SQL statement using ||
(concatenate) and execute it using EXECUTE.

http://www.postgresql.org/docs/current/static/plpgsql-
statements.html#PLPGSQL-STATEMENTS-EXECUTING-DYN

Does this help?  Another simpler way to do this would be a different
table structure where you put the month in a column by itself rather
than a different column for each month.

Sean


pgsql-general by date:

Previous
From: Shaun Clements
Date:
Subject: PLPGSQL
Next
From: Shaun Clements
Date:
Subject: FW: PLPGSQL