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

Поиск
Список
Период
Сортировка
От Hoover, Jeffrey
Тема Re: Need help to dynamically access to colomns in function!
Дата
Msg-id E92C2B1CB12A7A4683697273BD5DCCE402DF0D83@EXCHANGE.TIGR.ORG
обсуждение исходный текст
Ответ на Re: Need help to dynamically access to colomns in function!  (Ivan Pavlov <ivan.pavlov@gmail.com>)
Список 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@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of Ivan Pavlov
Sent: Tuesday, December 16, 2008 3:55 PM
To: pgsql-general@postgresql.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...@gmail.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.


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

В списке pgsql-general по дате отправления:

Предыдущее
От: Allan Kamau
Дата:
Сообщение: Re: Rows count in the cursor ?
Следующее
От: "Albe Laurenz"
Дата:
Сообщение: Re: Is this a security risk?