pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Retrieve the column values of a record without knowing the names
9+ messages / 4 participants
[nested] [flat]

* Retrieve the column values of a record without knowing the names
@ 2011-02-16 16:16  arthur_info <arthur_info@yahoo.com.br>
  0 siblings, 1 reply; 9+ messages in thread

From: arthur_info @ 2011-02-16 16:16 UTC (permalink / raw)
  To: pgsql-sql


Hello,

I've got the following function and I want to access the fields values of my
record by index. The problem is that my select is retrieving each record
line with all values and not each one of each row on my view... How can I
solve this problem? 

Thanks in advance.


CREATE FUNCTION fc_teste_tce(aluno integer) RETURNS character varying AS
$BODY$ 
DECLARE
  reg record;
BEGIN
  for reg in execute 'SELECT ARRAY (SELECT vw_teste_tce FROM
estagio.vw_teste_tce where aluno = ''3043'' LIMIT 20) AS campos' loop
    for j in 1..array_upper(reg.campos,1) loop
      raise notice 'Field Value: %',reg.campos[j];
    end loop;
  end loop;
  return 'ok';
END;  
$BODY$

LANGUAGE plpgsql VOLATILE;


-- 
View this message in context: http://postgresql.1045698.n5.nabble.com/Retrieve-the-column-values-of-a-record-without-knowing-the-n...
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Retrieve the column values of a record without knowing the names
@ 2011-02-16 16:18  arthur_info <arthur_info@yahoo.com.br>
  0 siblings, 2 replies; 9+ messages in thread

From: arthur_info @ 2011-02-16 16:18 UTC (permalink / raw)
  To: pgsql-sql


Hello,

I've got the following function and I want to access the fields values of my
record by index. The problem is that my select is retrieving each record
line with all values and not each one of each row on my view... How can I
solve this problem? 

Thanks in advance.


CREATE FUNCTION fc_teste_tce(aluno integer) RETURNS character varying AS
$BODY$ 
DECLARE
  reg record;
BEGIN
  for reg in execute 'SELECT ARRAY (SELECT vw_teste_tce FROM
estagio.vw_teste_tce where aluno = ''3043'' LIMIT 20) AS campos' loop
    for j in 1..array_upper(reg.campos,1) loop
      raise notice 'Field Value: %',reg.campos[j];
    end loop;
  end loop;
  return 'ok';
END;  
$BODY$

LANGUAGE plpgsql VOLATILE;


-- 
View this message in context: http://postgresql.1045698.n5.nabble.com/Retrieve-the-column-values-of-a-record-without-knowing-the-n...
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Retrieve the column values of a record without knowing the names
@ 2011-02-18 08:34  Pavel Stehule <pavel.stehule@gmail.com>
  parent: arthur_info <arthur_info@yahoo.com.br>
  0 siblings, 1 reply; 9+ messages in thread

From: Pavel Stehule @ 2011-02-18 08:34 UTC (permalink / raw)
  To: arthur_info <arthur_info@yahoo.com.br>; +Cc: pgsql-sql

Hello

you can't simply iterate over record in plpgsql. You can use a some
toolkits like PLToolkit, or different PL language like PLPerl, or some
dirty trick

http://okbob.blogspot.com/2010/12/iteration-over-record-in-plpgsql.html

regards

Pavel Stehule

2011/2/16 arthur_info <arthur_info@yahoo.com.br>:
>
> Hello,
>
> I've got the following function and I want to access the fields values of my
> record by index. The problem is that my select is retrieving each record
> line with all values and not each one of each row on my view... How can I
> solve this problem?
>
> Thanks in advance.
>
>
> CREATE FUNCTION fc_teste_tce(aluno integer) RETURNS character varying AS
> $BODY$
> DECLARE
>  reg record;
> BEGIN
>  for reg in execute 'SELECT ARRAY (SELECT vw_teste_tce FROM
> estagio.vw_teste_tce where aluno = ''3043'' LIMIT 20) AS campos' loop
>    for j in 1..array_upper(reg.campos,1) loop
>      raise notice 'Field Value: %',reg.campos[j];
>    end loop;
>  end loop;
>  return 'ok';
> END;
> $BODY$
>
> LANGUAGE plpgsql VOLATILE;
>
>
> --
> View this message in context: http://postgresql.1045698.n5.nabble.com/Retrieve-the-column-values-of-a-record-without-knowing-the-n...
> Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Retrieve the column values of a record without knowing the names
@ 2011-02-18 09:10  Achilleas Mantzios <achill@matrix.gatewaynet.com>
  parent: arthur_info <arthur_info@yahoo.com.br>
  1 sibling, 1 reply; 9+ messages in thread

From: Achilleas Mantzios @ 2011-02-18 09:10 UTC (permalink / raw)
  To: pgsql-sql

Στις Wednesday 16 February 2011 18:18:19 ο/η arthur_info έγραψε:
> 
> Hello,
> 
> I've got the following function and I want to access the fields values of my
> record by index. The problem is that my select is retrieving each record
> line with all values and not each one of each row on my view... How can I
> solve this problem? 
> 

Well by your function, it seems that it does not do what you want.
You want to access the values of one row without knowing the names, but what you do
is create an array over the values of a *known* column.
Also where aluno = ''3043'' does not make any sense either.

Anyways, you might want to look at database metada as found in the information_schema.
For a start you might want to look in information_schema.columns

> Thanks in advance.
> 
> 
> CREATE FUNCTION fc_teste_tce(aluno integer) RETURNS character varying AS
> $BODY$ 
> DECLARE
>   reg record;
> BEGIN
>   for reg in execute 'SELECT ARRAY (SELECT vw_teste_tce FROM
> estagio.vw_teste_tce where aluno = ''3043'' LIMIT 20) AS campos' loop
>     for j in 1..array_upper(reg.campos,1) loop
>       raise notice 'Field Value: %',reg.campos[j];
>     end loop;
>   end loop;
>   return 'ok';
> END;  
> $BODY$
> 
> LANGUAGE plpgsql VOLATILE;
> 
> 
> -- 
> View this message in context: http://postgresql.1045698.n5.nabble.com/Retrieve-the-column-values-of-a-record-without-knowing-the-n...
> Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
> 



-- 
Achilleas Mantzios



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Retrieve the column values of a record without knowing the names
@ 2011-02-18 11:46  arthur_info <arthur_info@yahoo.com.br>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 1 reply; 9+ messages in thread

From: arthur_info @ 2011-02-18 11:46 UTC (permalink / raw)
  To: pgsql-sql


Very interesting... But with information_schema.columns is easier... Thanks
anyway... 
-- 
View this message in context: http://postgresql.1045698.n5.nabble.com/Retrieve-the-column-values-of-a-record-without-knowing-the-n...
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Retrieve the column values of a record without knowing the names
@ 2011-02-18 11:47  arthur_info <arthur_info@yahoo.com.br>
  parent: Achilleas Mantzios <achill@matrix.gatewaynet.com>
  0 siblings, 0 replies; 9+ messages in thread

From: arthur_info @ 2011-02-18 11:47 UTC (permalink / raw)
  To: pgsql-sql


Wow, how I didn't think about this solution? Thanks a lot! So easy...
-- 
View this message in context: http://postgresql.1045698.n5.nabble.com/Retrieve-the-column-values-of-a-record-without-knowing-the-n...
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Retrieve the column values of a record without knowing the names
@ 2011-02-18 11:59  arthur_info <arthur_info@yahoo.com.br>
  parent: arthur_info <arthur_info@yahoo.com.br>
  0 siblings, 0 replies; 9+ messages in thread

From: arthur_info @ 2011-02-18 11:59 UTC (permalink / raw)
  To: pgsql-sql


Wow, I don't know if I exposed the problem in the right way, but I'm not
achieving the solution with information_schema... Let me show the function:

CREATE OR REPLACE FUNCTION fc_teste_tce2(codigo_aluno integer)
  RETURNS character varying AS
$BODY$ 
DECLARE
  reg record;
  reg2 record;
  msg character varying;
  i integer;
BEGIN
  msg := '';
  i := 0;
  for reg in SELECT * FROM estagio.vw_aluno_tce where aluno::integer =
codigo_aluno loop
    for reg2 in select column_name
		from information_schema.columns 
		where table_catalog = 'seduc' and 
		      table_schema = 'estagio' and 
		      table_name = 'vw_aluno_tce' loop
          if reg.reg2.column_name = '' then --> how can I do this to check
if there's no value for the field?
            i := 1;
          end if; 
	end loop;	      
  end loop;
  if (i = 1) then
    return 'Problems...';
  else
    return 'ok';
  end if;
END;  
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;


Thanks a lot!
-- 
View this message in context: http://postgresql.1045698.n5.nabble.com/Retrieve-the-column-values-of-a-record-without-knowing-the-n...
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.



^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Retrieve the column values of a record without knowing the names
@ 2011-02-19 09:47  Dmitriy Igrishin <dmitigr@gmail.com>
  parent: arthur_info <arthur_info@yahoo.com.br>
  1 sibling, 1 reply; 9+ messages in thread

From: Dmitriy Igrishin @ 2011-02-19 09:47 UTC (permalink / raw)
  To: arthur_info <arthur_info@yahoo.com.br>; +Cc: pgsql-sql

Hey,

2011/2/16 arthur_info <arthur_info@yahoo.com.br>

>
> Hello,
>
> I've got the following function and I want to access the fields values of
> my
> record by index. The problem is that my select is retrieving each record
> line with all values and not each one of each row on my view... How can I
> solve this problem?
>
You can easily iterate across records from PL/pgSQL by using hstore, e.g:
SELECT (avals(hstore(ROW(83,6,4))))[3];
dmitigr=> SELECT (avals(hstore(ROW(83,6,4))))[3] AS thirdfield;
 thirdfield
------------
 4
(1 row)

See http://www.postgresql.org/docs/9.0/static/hstore.html


>
> Thanks in advance.
>
>
> CREATE FUNCTION fc_teste_tce(aluno integer) RETURNS character varying AS
> $BODY$
> DECLARE
>  reg record;
> BEGIN
>  for reg in execute 'SELECT ARRAY (SELECT vw_teste_tce FROM
> estagio.vw_teste_tce where aluno = ''3043'' LIMIT 20) AS campos' loop
>    for j in 1..array_upper(reg.campos,1) loop
>      raise notice 'Field Value: %',reg.campos[j];
>    end loop;
>  end loop;
>  return 'ok';
> END;
> $BODY$
>
> LANGUAGE plpgsql VOLATILE;
>
>
> --
> View this message in context:
> http://postgresql.1045698.n5.nabble.com/Retrieve-the-column-values-of-a-record-without-knowing-the-n...
> Sent from the PostgreSQL - sql mailing list archive at Nabble.com.
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>



-- 
// Dmitriy.

^ permalink  raw  reply  [nested|flat] 9+ messages in thread

* Re: Retrieve the column values of a record without knowing the names
@ 2011-02-22 19:39  arthur_info <arthur_info@yahoo.com.br>
  parent: Dmitriy Igrishin <dmitigr@gmail.com>
  0 siblings, 0 replies; 9+ messages in thread

From: arthur_info @ 2011-02-22 19:39 UTC (permalink / raw)
  To: pgsql-sql


Thanks Dmitry Grishin. The only problem is that my DBA didn't permit install
this command... :(
But, I quit this problem...
Thanks by attention...
-- 
View this message in context: http://postgresql.1045698.n5.nabble.com/Retrieve-the-column-values-of-a-record-without-knowing-the-n...
Sent from the PostgreSQL - sql mailing list archive at Nabble.com.



^ permalink  raw  reply  [nested|flat] 9+ messages in thread


end of thread, other threads:[~2011-02-22 19:39 UTC | newest]

Thread overview: 9+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-02-16 16:16 Retrieve the column values of a record without knowing the names arthur_info <arthur_info@yahoo.com.br>
2011-02-18 08:34 ` Pavel Stehule <pavel.stehule@gmail.com>
2011-02-18 11:46   ` arthur_info <arthur_info@yahoo.com.br>
2011-02-18 11:59     ` arthur_info <arthur_info@yahoo.com.br>
2011-02-16 16:18 Retrieve the column values of a record without knowing the names arthur_info <arthur_info@yahoo.com.br>
2011-02-18 09:10 ` Achilleas Mantzios <achill@matrix.gatewaynet.com>
2011-02-18 11:47   ` arthur_info <arthur_info@yahoo.com.br>
2011-02-19 09:47 ` Dmitriy Igrishin <dmitigr@gmail.com>
2011-02-22 19:39   ` arthur_info <arthur_info@yahoo.com.br>

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