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

Trouble accessing %ROWTYPE attributes returned by function

From: Leon Starr <leon_starr(at)modelint(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Trouble accessing %ROWTYPE attributes returned by function
Date: 2010-07-29 22:31:55
Message-ID: (view raw, whole thread or download thread mbox)
Lists: pgsql-novice
At the moment I am using tcl to experiment, but the problem is apparent even in the psql interpreter.

The problem is that the value returned from the function below lumps all of the attribute values together
in each row as opposed to keeping them separate.  So instead of seeing three distinct attributes on each tuple,
I am just getting a single concatenated string with no attributes.

The example is below, and my question is:  What is the proper way to return multiple tuples from a function so that
the attribute values are properly separated (available for use by an external program such as java/python/tcl)?

Help greatly appreciated!  - Leon

Example follows:

I've got a table named contracts with three attributes: number integer, org_code text, type text

Here's the function in question:

create or replace function getContracts() returns setof contract as
	r contract%rowtype;
	for r in select * from contract 
		return next r;
	end loop;
language plpgsql;

Here is the contrasting output seen in the psql interpreter:
GOOD ( attributes separated)
contracts=# select * from contract;
 number | org_code | type  
      1 | USGE     | Renew
      2 | USGE     | Renew
      3 | USGE     | Renew

and via tcl - also good:
% set result [pg_exec $db "select * from contract"]
% pg_result $result -numTuples
% pg_result $result -numAttrs
BAD (attributes concatenated)  why?
contracts=# select getContracts();

 and via tcl - not what I wanted:
% set result2 [pg_exec $db "select getContracts()"]
% pg_result $result2 -numTuples
% pg_result $result2 -numAttrs


pgsql-novice by date

Next:From: Phil DagostoDate: 2010-07-30 20:28:44
Subject: NOTIFY action invoked when it shouldn't be
Previous:From: John DeSoiDate: 2010-07-29 18:13:33
Subject: Re: importing a data file without defining a table structure

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