pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
stored procedures and type of returned result.
4+ messages / 3 participants
[nested] [flat]

* stored procedures and type of returned result.
@ 2004-08-24 08:39  Patrice OLIVER <oliverp21@free.fr>
  0 siblings, 1 reply; 4+ messages in thread

From: Patrice OLIVER @ 2004-08-24 08:39 UTC (permalink / raw)
  To: pgsql-sql

***********************
Aucun virus n'a été détecté dans la pièce-jointe no filename
---
No virus was detected in the attachment no filename

Votre courrier a été inspecté par InterScan Messaging Security Suite
---
Your mail has been scanned by InterScan MSS.
***********************


Hello,

I'm new in PostgreSQL. It's very cool.

I would like to know how to return a set of records from a stored procedure.

For example, i would like to execute these sql command from a stored 
procedure :

select t.typnum, t.typcom, t.typcateg, s.symurlgraph from structure.type t
   left join structure.symbole s
   on t.typcode = s.typcode;

Is it possible ?
Do i have to return results in an array ?
If yes, how could i do ?

Do you think using plPHP mays help me ?

Regards,
Patrice OLIVER.





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

* Re: stored procedures and type of returned result.
@ 2004-08-24 09:26  Richard Huxton <dev@archonet.com>
  parent: Patrice OLIVER <oliverp21@free.fr>
  0 siblings, 1 reply; 4+ messages in thread

From: Richard Huxton @ 2004-08-24 09:26 UTC (permalink / raw)
  To: Patrice OLIVER <oliverp21@free.fr>; +Cc: pgsql-sql

Patrice OLIVER wrote:
> Hello,
> 
> I'm new in PostgreSQL. It's very cool.

Hello, and yes it is isn't it :-)

> I would like to know how to return a set of records from a stored 
> procedure.
> 
> For example, i would like to execute these sql command from a stored 
> procedure :
> 
> select t.typnum, t.typcom, t.typcateg, s.symurlgraph from structure.type t
>   left join structure.symbole s
>   on t.typcode = s.typcode;

Broadly speaking you'll want something like:

CREATE TYPE my_return_type AS (
   a  integer,
   b  text
);

CREATE FUNCTION my_function(integer) RETURNS SETOF my_return_type AS '
   SELECT foo_a, foo_b FROM foo WHERE foo_c = $1;
' LANGUAGE SQL;

You don't need to define your own type if you want to return the same 
columns as a table, you can use the table-name instead.

For more complex cases where you need procedural code, you probably want 
to read Stephan Szabo's set-returning-functions article on techdocs.
   http://techdocs.postgresql.org/guides/SetReturningFunctions

HTH
-- 
   Richard Huxton
   Archonet Ltd



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

* Re: stored procedures and type of returned result.
@ 2004-08-24 09:56  Philippe Lang <philippe.lang@attiksystem.ch>
  0 siblings, 0 replies; 4+ messages in thread

From: Philippe Lang @ 2004-08-24 09:56 UTC (permalink / raw)
  To: pgsql-sql

Patrice,

You might have a look at "views" as well. That's not strictily speaking a stored procedure, but maybe it's what you are searching for?

Philippe Lang

-----Message d'origine-----
De : pgsql-sql-owner@postgresql.org [mailto:pgsql-sql-owner@postgresql.org] De la part de Richard Huxton
Envoyé : mardi, 24. août 2004 11:26
À : Patrice OLIVER
Cc : pgsql-sql@postgresql.org
Objet : Re: [SQL] stored procedures and type of returned result.

Patrice OLIVER wrote:
> Hello,
> 
> I'm new in PostgreSQL. It's very cool.

Hello, and yes it is isn't it :-)

> I would like to know how to return a set of records from a stored 
> procedure.
> 
> For example, i would like to execute these sql command from a stored 
> procedure :
> 
> select t.typnum, t.typcom, t.typcateg, s.symurlgraph from structure.type t
>   left join structure.symbole s
>   on t.typcode = s.typcode;

Broadly speaking you'll want something like:

CREATE TYPE my_return_type AS (
   a  integer,
   b  text
);

CREATE FUNCTION my_function(integer) RETURNS SETOF my_return_type AS '
   SELECT foo_a, foo_b FROM foo WHERE foo_c = $1; ' LANGUAGE SQL;

You don't need to define your own type if you want to return the same columns as a table, you can use the table-name instead.

For more complex cases where you need procedural code, you probably want to read Stephan Szabo's set-returning-functions article on techdocs.
   http://techdocs.postgresql.org/guides/SetReturningFunctions

HTH
-- 
   Richard Huxton
   Archonet Ltd

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
      subscribe-nomail command to majordomo@postgresql.org so that your
      message can get through to the mailing list cleanly





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

* Re: stored procedures and type of returned result.
@ 2004-08-24 10:41  Richard Huxton <dev@archonet.com>
  parent: Richard Huxton <dev@archonet.com>
  0 siblings, 0 replies; 4+ messages in thread

From: Richard Huxton @ 2004-08-24 10:41 UTC (permalink / raw)
  To: Patrice OLIVER <oliverp21@free.fr>; +Cc: pgsql-sql

Patrice OLIVER wrote:
> 
> Hello, I got this error message :
> 
> ERROR:  set-valued function called in context that cannot accept a set
> CONTEXT:  PL/pgSQL function "lsttable" line 5 at return next

You need to treat set-returning functions as though they are tables.
   SELECT * FROM lsttable();
NOT
   SELECT lsttable();

-- 
   Richard Huxton
   Archonet Ltd



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


end of thread, other threads:[~2004-08-24 10:41 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-08-24 08:39 stored procedures and type of returned result. Patrice OLIVER <oliverp21@free.fr>
2004-08-24 09:26 ` Richard Huxton <dev@archonet.com>
2004-08-24 10:41   ` Richard Huxton <dev@archonet.com>
2004-08-24 09:56 Re: stored procedures and type of returned result. Philippe Lang <philippe.lang@attiksystem.ch>

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