pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Variable number or arguments to a function possible?
3+ messages / 3 participants
[nested] [flat]

* Variable number or arguments to a function possible?
@ 2009-04-25 02:04  Chris Ruprecht <chris@ruprecht.org>
  0 siblings, 2 replies; 3+ messages in thread

From: Chris Ruprecht @ 2009-04-25 02:04 UTC (permalink / raw)
  To: pgsql-sql

Hello everybody,

Is it possible to create a function that can take a variable number of  
arguments?
I would like to write a function that creates a new record in the  
database. Based on what I send it, it should create a record in the  
appropriate table.

Simple pseudo-code example:

... function create_record( varchar [,...] ) returns bigint as

if $1 = 'state' then insert into state ( $2, $3 ) // $2 being state  
name, and $3 state code
if $1 = 'phone' then insert into phone ( $4::bigint, $2, $3 ) // $2 =  
phone number, $3 = phone type, $4 = id of person that ownes the phone

and so on.

How would I declare that function?

Thanks.


best regards,
chris
-- 
chris ruprecht
database grunt and bit pusher extraordinaíre




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

* Re: Variable number or arguments to a function possible?
@ 2009-04-25 03:08  Milen A. Radev <milen@radev.net>
  parent: Chris Ruprecht <chris@ruprecht.org>
  1 sibling, 0 replies; 3+ messages in thread

From: Milen A. Radev @ 2009-04-25 03:08 UTC (permalink / raw)
  To: pgsql-sql

Chris Ruprecht написа:
> Hello everybody,
> 
> Is it possible to create a function that can take a variable number of
> arguments?
> I would like to write a function that creates a new record in the
> database. Based on what I send it, it should create a record in the
> appropriate table.
> 
> Simple pseudo-code example:
> 
> ... function create_record( varchar [,...] ) returns bigint as
> 
> if $1 = 'state' then insert into state ( $2, $3 ) // $2 being state
> name, and $3 state code
> if $1 = 'phone' then insert into phone ( $4::bigint, $2, $3 ) // $2 =
> phone number, $3 = phone type, $4 = id of person that ownes the phone
> 
> and so on.
> 
> How would I declare that function?

You'll be able to do that (or something similar) in the next, still
in beta, version 8.4
(http://www.postgresql.org/docs/8.4/static/xfunc-sql.html#XFUNC-SQL-VARIADIC-FUNCTIONS),
but right now the closest to what you want is function overloading
(http://www.postgresql.org/docs/current/static/xfunc-overload.html).



-- 
Milen A. Radev




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

* Re: Variable number or arguments to a function possible?
@ 2009-04-25 03:09  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Chris Ruprecht <chris@ruprecht.org>
  1 sibling, 0 replies; 3+ messages in thread

From: Tom Lane @ 2009-04-25 03:09 UTC (permalink / raw)
  To: Chris Ruprecht <chris@ruprecht.org>; +Cc: pgsql-sql

Chris Ruprecht <chris@ruprecht.org> writes:
> Is it possible to create a function that can take a variable number of  
> arguments?

No, but you can make it take an array and then write something like

	myfunc(array[x, y, ...])

8.4 will have some syntactic sugar that looks like variable numbers of
arguments, but it's reducing to the above underneath.

			regards, tom lane



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


end of thread, other threads:[~2009-04-25 03:09 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-04-25 02:04 Variable number or arguments to a function possible? Chris Ruprecht <chris@ruprecht.org>
2009-04-25 03:08 ` Milen A. Radev <milen@radev.net>
2009-04-25 03:09 ` Tom Lane <tgl@sss.pgh.pa.us>

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