Re: plpgsql question

Поиск
Список
Период
Сортировка
От Michael Fuhr
Тема Re: plpgsql question
Дата
Msg-id 20060110014553.GA40249@winnie.fuhr.org
обсуждение исходный текст
Ответ на Re: plpgsql question  (Matthew Peter <survivedsushi@yahoo.com>)
Ответы Re: plpgsql question  (Matthew Peter <survivedsushi@yahoo.com>)
Список pgsql-general
On Mon, Jan 09, 2006 at 01:01:33PM -0800, Matthew Peter wrote:
> One other quick question, (figure it still applies to the subject
> line :) when returning a row  from a function I'm trying to include an
> aggregate, but it's not  showing up  in the query result and I think
> it's because it's not included in the  RETURN NEXT row;?  How do I
> return it as part of the resultset...

Terminology point: you used the word "aggregate" but the function
below doesn't have an aggregate.  Aggregates are functions that
operate on multiple rows, like count() and sum(); substr() doesn't
do that so it's not an aggregate.

> create or replace function getrecord(int,text) RETURNS SETOF my_tbl as $$
> DECLARE
>  row my_tbl%rowtype;
>
> BEGIN
> FOR row IN  SELECT *, SUBSTR(title,1,25) as short_title FROM my_tbl
[...]

You've declared the row variable to be of type my_tbl so whatever
columns my_tbl has are the columns you get.  If you want to return
additional columns then you have a few choices:

1. Create a composite type with the desired columns, declare the
   function to return SETOF that type, and declare row to be of
   that type.

2. Declare the function to return SETOF record, declare row to
   be of type record, and provide a column definition list when
   you call the function.

3. Use OUT parameters (new in 8.1).

--
Michael Fuhr

В списке pgsql-general по дате отправления:

Предыдущее
От: Michael Fuhr
Дата:
Сообщение: Re: calling stored procedure with array paramenter (for psql)
Следующее
От: Assad Jarrahian
Дата:
Сообщение: Re: calling stored procedure with array paramenter (for psql)