Function with DEFAULT arguments

From: "dario(dot)ber(at)libero(dot)it" <dario(dot)ber(at)libero(dot)it>
To: pgsql-general(at)postgresql(dot)org
Subject: Function with DEFAULT arguments
Date: 2010-03-12 16:28:57
Message-ID: 32871558.187291268411337102.JavaMail.defaultUser@defaultHost
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,

I'm trying to use the DEFAULT option to pass parameters to the arguments of a
function.
When I call that function, how can I change the default value of some
arguments and leave as default the value of other arguments? In other words, is
there a way to 'call' the arguments by their names so to specify which should
have their default value changed?

Here's a toy example, a function that takes three strings as arguments and
concatenate them:

CREATE FUNCTION test_default(string1 text default 'a', string2 text default
'b', string3 text default 'c') RETURNS text AS $$
BEGIN
RETURN string1 || string2 || string3;
END;
$$ language 'plpgsql';

-- Only default args:
SELECT test_default(); --> abc

-- With custom values:
SELECT test_default('X', 'Y', 'Z'); --> XYZ

-- Now, how can I leave as default the 1st and 3rd argument (string1 and
string3) and change only the second one (string2)? I would like to do something
like:
SELECT test_default(string2= 'Y'); -- To return 'aYb'

And in general, are there any examples/documentation that show how to use the
option default?
I'm using postgresql 8.4.2 on Windows XP.

Many thanks

Dario

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hubert depesz lubaczewski 2010-03-12 16:38:32 Re: Function with DEFAULT arguments
Previous Message Franclin Foping 2010-03-12 16:04:55 Re: Unable to call functions defined in XML2 contrib