Обсуждение: Function with default value?

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

Function with default value?

От
Daniel CAUNE
Дата:
<div class="Section1"><p class="MsoNormal"><font face="Arial" size="2"><span lang="EN-GB" style="font-size:
10.0pt;font-family:Arial">Hi,</span></font><p class="MsoNormal"><font face="Arial" size="2"><span lang="EN-GB"
style="font-size:
10.0pt;font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span lang="EN-GB"
style="font-size:
10.0pt;font-family:Arial">Is it possible to define a function with some default values?</span></font><p
class="MsoNormal"><fontface="Arial" size="2"><span lang="EN-GB" style="font-size: 
10.0pt;font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span lang="EN-GB"
style="font-size:
10.0pt;font-family:Arial">CREATE OR REPLACE FUNCTION foo(i IN int, j IN int DEFAULT := 1)</span></font><p
class="MsoNormal"style="text-indent:6.0pt"><font face="Arial" size="2"><span lang="EN-GB"
style="font-size:10.0pt;font-family:Arial">…</span></font><pclass="MsoNormal"><font face="Arial" size="2"><span
lang="EN-GB"style="font-size: 
10.0pt;font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span lang="EN-GB"
style="font-size:
10.0pt;font-family:Arial">Anyway, I didn’t find such a feature described in the PostgreSQL 8.1 documentation, but
sometimes,that doesn’t mean that the feature doesn’t exist! </span></font><font face="Wingdings" size="2"><span
lang="EN-GB"style="font-size:10.0pt;font-family:Wingdings">J</span></font><p class="MsoNormal"><font face="Arial"
size="2"><spanlang="EN-GB" style="font-size: 
10.0pt;font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span lang="EN-GB"
style="font-size:
10.0pt;font-family:Arial">Thanks,</span></font><p class="MsoNormal"><font face="Arial" size="2"><span lang="EN-GB"
style="font-size:
10.0pt;font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span lang="EN-GB"
style="font-size:
10.0pt;font-family:Arial"> </span></font><p class="MsoNormal"><font face="Arial" size="2"><span lang="EN-GB"
style="font-size:
10.0pt;font-family:Arial">Daniel</span></font></div>

Re: Function with default value?

От
Tom Lane
Дата:
Daniel CAUNE <d.caune@free.fr> writes:
> Is it possible to define a function with some default values?
> CREATE OR REPLACE FUNCTION foo(i IN int, j IN int DEFAULT := 1)

No.  But you can fake many versions of this with a family of functions:

CREATE OR REPLACE FUNCTION foo(i IN int, j IN int) ...

CREATE OR REPLACE FUNCTION foo(i IN int) ... return foo(i, 1) ...

Remember that PG lets you "overload" a function name by using the same
name with different parameter lists.
        regards, tom lane


Re: Function with default value?

От
Daniel CAUNE
Дата:
> -----Message d'origine-----
> De : Tom Lane [mailto:tgl@sss.pgh.pa.us]
> Envoyé : dimanche 29 janvier 2006 10:48
> À : Daniel CAUNE
> Cc : pgsql-sql@postgresql.org
> Objet : Re: [SQL] Function with default value?
>
> Daniel CAUNE <d.caune@free.fr> writes:
> > Is it possible to define a function with some default values?
> > CREATE OR REPLACE FUNCTION foo(i IN int, j IN int DEFAULT := 1)
>
> No.  But you can fake many versions of this with a family of functions:
>
> CREATE OR REPLACE FUNCTION foo(i IN int, j IN int) ...
>
> CREATE OR REPLACE FUNCTION foo(i IN int) ... return foo(i, 1) ...
>
> Remember that PG lets you "overload" a function name by using the same
> name with different parameter lists.
>
>             regards, tom lane

Yes, thanks Tom, for the second time.