stored procedures and type of returned result.

***********************
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.



---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings
oliverp21 [ Di, 24 August 2004 10:39 ] [ ID #453378 ]

Re: 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 [at] postgresql.org so that your
message can get through to the mailing list cleanly
dev [ Di, 24 August 2004 11:26 ] [ ID #453381 ]

Re: stored procedures and type of returned result.

Patrice,

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

Philippe Lang

-----Message d'origine-----
De : pgsql-sql-owner [at] postgresql.org [mailto:pgsql-sql-owner [at] postgresql.org]=
De la part de Richard Huxton
Envoy=E9 : mardi, 24. ao=FBt 2004 11:26
=C0 : Patrice OLIVER
Cc : pgsql-sql [at] 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 =3D 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 =3D $1; ' LANGUAGE SQL;

You don't need to define your own type if you want to return the same colum=
ns 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 [at] postgresql.org so that your
message can get through to the mailing list cleanly



---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html
philippe.lang [ Di, 24 August 2004 11:56 ] [ ID #453382 ]

Re: stored procedures and type of returned result.

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

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend
dev [ Di, 24 August 2004 12:41 ] [ ID #453383 ]
Datenbanken » comp.databases.postgresql.sql » stored procedures and type of returned result.

Vorheriges Thema: Disable trigger with Postgresql 7.4.x?
Nächstes Thema: surrogate key or not?