Обсуждение: plpgsql default arguments

Поиск
Список
Период
Сортировка

plpgsql default arguments

От
Greg Lindstrom
Дата:
Hello-

I am running postgres on a Linux system and have been using plpgsql to
write functions.  Is there a way to specify default arguments?  For
example, I would like to offer the user to specify a date to use in my
function but if no date is specified I would like to use the current
date.  Easy to do in C, C++, Perl, and Python (and, I assume Java), but
I can't find anything in the documentation or on Google.  Is it possible
in plpgsql?

Thanks,
--greg

--
Greg Lindstrom               501 975.4859 (office)
Senior Programmer            501 219-4455 (fax)
NovaSys Health               greg.lindstrom@novasyshealth.com
Little Rock, Arkansas

"We are the music makers, and we are the dreamers of dreams."  W.W.



Re: plpgsql default arguments

От
Pavel Stehule
Дата:
> I am running postgres on a Linux system and have been using plpgsql to
> write functions.  Is there a way to specify default arguments?

Hello,

there is no possibility define default arguments. But you can write
functions with different count of arguments.

CREATE OR REPLACE FUNCTION foo(integer, integer) RETURNS integer AS $$
BEGIN
  RETURN $1 + $2;
END; $$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION foo(integer) RETURNS integer AS $$
BEGIN
  RETURN foo($1,10); -- 10 is "default" value
END; $$ LANGUAGE plpgsql;

Regards
Pavel Stehule


Re: plpgsql default arguments

От
Neil Conway
Дата:
Pavel Stehule wrote:
> CREATE OR REPLACE FUNCTION foo(integer, integer) RETURNS integer AS $$
> BEGIN
>   RETURN $1 + $2;
> END; $$ LANGUAGE plpgsql;
>
> CREATE OR REPLACE FUNCTION foo(integer) RETURNS integer AS $$
> BEGIN
>   RETURN foo($1,10); -- 10 is "default" value
> END; $$ LANGUAGE plpgsql;

Note that if you define the "short cut function" in SQL (the second one
above that supplies the default argument), you can take advantage of
function inlining. Granted, it's probably not a huge win, but if all the
function does is return the result of evaluating another function, it
need not be pl/pgsql anyway.

-Neil