Re: Need help to dynamically access to colomns in function!

From: "Hoover, Jeffrey" <jhoover(at)jcvi(dot)org>
To: <pgsql-general(at)postgresql(dot)org>
Subject: Re: Need help to dynamically access to colomns in function!
Date: 2008-12-16 21:48:27
Message-ID: E92C2B1CB12A7A4683697273BD5DCCE402DF0D83@EXCHANGE.TIGR.ORG
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Create a plpgsql function that reformats your row as an array.

Example:
I have a table named task_parameter with three columns:

Table "camera.task_parameter"
Column | Type | Modifiers
--------------------------+---------------------------------+-----------
parameter_name | character varying(255) | not null
parameter_value | text |
task_id | bigint | not null

I create a plpgsql function that takes a task_parameter row and returns an array, one column per array entry:

create or replace function task_parameter_array(tp task_parameter) returns text[] as $$
declare
result text[];
begin
result[1] := tp.parameter_name;
result[2] := tp.parameter_value;
result[3] := tp.task_id;
return result;
end $$ language plpgsql;

select task_parameter_array(task_parameter) from task_parameter limit 1;
task_parameter_array
----------------------------------------------------
{"db alignments per query",25,1286428019358957945}
(1 row)

You can write a similar function for your table and then your code would look like:

OPEN curs FOR select classif_to_array(classif) as group_array from classif;
loop
fetch curs into tmprec;
exit when not found;

for I in 1..20 loop
...
value := tmprec.group_array{I};

of course, this begs the question, whjy not define you table to store an array...?

-----Original Message-----
From: pgsql-general-owner(at)postgresql(dot)org [mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of Ivan Pavlov
Sent: Tuesday, December 16, 2008 3:55 PM
To: pgsql-general(at)postgresql(dot)org
Subject: Re: [GENERAL] Need help to dynamically access to colomns in function!

If you need them one by one why fetch them into tmprec? Take a look at
the docs for FETCH:
http://www.postgresql.org/docs/8.3/static/sql-fetch.html

especially the FETCH ABSOLUTE...

regards,
Ivan Pavlov

On Dec 16, 3:37 pm, aesthete2(dot)(dot)(dot)(at)gmail(dot)com ("Иван Марков") wrote:
> Hello. I have table classif with columns:
> ... , group1, group2, group3, ... , group48, ...
>
> In function i do query and want run on every row and dynamically operate on
> columns from group1 to group20. I do something like this:
>
> OPEN curs FOR select * from classif;
>  loop
>     fetch curs into tmprec;
>     exit when not found;
>
>     for I in 1..20 loop
>         ...
>     -- problem code is:
>         value := tmprec.group{I};
>     -- i cannot dynamically access to group1, group2, ... colomns according
> to "I" variable.
>     ...
>
>     end loop;
> end loop;
>
> I have to manually identify and handle each entry without a cycle do
> something like this:
> value := tmprec.group1;
> ...
> value := tmprec.group2;
> ...
> value := tmprec.group2;
> ...
> value := tmprec.group20;
>
> Please help me to do it dynamically with a loop, depending on the I?
> something like this in a loop:
> value := tmprec.group{I};
>
> Thanks.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2008-12-17 00:28:04 Re: Isolating a record column from a PL-Pgsql function call ?
Previous Message Ivan Pavlov 2008-12-16 21:04:53 Re: Need help to dynamically access to colomns in function!