Re: function SETOF return type with variable columns?

Поиск
Список
Период
Сортировка
От Merlin Moncure
Тема Re: function SETOF return type with variable columns?
Дата
Msg-id b42b73150808201900l42f20277v128676ec82cec74f@mail.gmail.com
обсуждение исходный текст
Ответ на function SETOF return type with variable columns?  (James Neff <james.neff@tethyshealth.com>)
Ответы Re: function SETOF return type with variable columns?
Список pgsql-general
On Wed, Aug 20, 2008 at 12:59 PM, James Neff
<james.neff@tethyshealth.com> wrote:
> Greetings,
>
> Is it possible to have a function with a return type of  SETOF that has
> variable number of return columns?
>
> The input parameter for this function will be a String containing a number
> of codes separated by a tilde character.  I would like to have 1 output
> column for each of these codes, but the number of input codes may change for
> each time the function is called.
>
> For example:
>
> Input:  ABC1~XYZ2~MNO3
>
>
> Output result set will then look like this where name, ABC1, XYZ2, and MNO3
> are column headers and not a data row:
>
> name | ABC1 | XYZ2 | MNO3
> bob | 9 | 3 | 1
> john | 5 | 2 | 1
> ...
>
>
> Every row in the output set will contain a name and then a count of the
> number of codes matched for that name.  But the codes queried for would
> change with each call of the function.
> Does it make sense what I am asking for?

PostgreSQL functions are for the most part strictly bound to their
return type.  If you are willing to coerce everything to text, you
might be able to return 'setof text[]' instead of a record.  This may
require more acrobatics inside the function than you really want to
get in to (especially if you are getting into deep dynamic sql,
iterating the column lists in information_schema and building
queries).

Another possibility is to make a custom type that has at least as many
columns as you are likely to use, and make them all text...set the
ones you want and leave the rest null.   This is, uh, fairly lame but
I'm trying to think outside the box here :-).

merlin

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

Предыдущее
От: "Merlin Moncure"
Дата:
Сообщение: Re: plpgsql - sorting result set
Следующее
От: Tom Lane
Дата:
Сообщение: Re: function SETOF return type with variable columns?