X-Original-To: pgsql-sql@postgresql.org Received: from anchor-post-35.mail.demon.net (anchor-post-35.mail.demon.net [194.217.242.85]) by svr1.postgresql.org (Postfix) with ESMTP id 98C825E470B for ; Tue, 24 Aug 2004 06:26:12 -0300 (ADT) Received: from mwynhau.demon.co.uk ([193.237.186.96] helo=mainbox.archonet.com) by anchor-post-35.mail.demon.net with esmtp (Exim 3.35 #1) id 1BzXZF-000BbF-0Z; Tue, 24 Aug 2004 09:26:14 +0000 Received: from [192.168.1.17] (client17.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id A161918C3C; Tue, 24 Aug 2004 10:26:12 +0100 (BST) Message-ID: <412B09B4.1080407@archonet.com> Date: Tue, 24 Aug 2004 10:26:12 +0100 From: Richard Huxton User-Agent: Mozilla Thunderbird 0.7 (X11/20040615) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Patrice OLIVER Cc: pgsql-sql@postgresql.org Subject: Re: stored procedures and type of returned result. References: <412AFEBC.4060404@free.fr> In-Reply-To: <412AFEBC.4060404@free.fr> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200408/251 X-Sequence-Number: 18748 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