4.3. Calling Functions

4.3. Calling Functions

4.3.函数调用

PostgreSQL allows functions that have named parameters to be called using either positional or named notation. Named notation is especially useful for functions that have a large number of parameters,since it makes the associations between parameters and actual arguments more explicit and reliable.In positional notation, a function call is written with its argument values in the same order as they are defined in the function declaration. In named notation, the arguments are matched to the function parameters by name and can be written in any order. For each notation, also consider the effect of function argument types, documented in Section 10.3.

PostgreSQL允许使用位置或命名表示法来调用具有命名参数的函数。 命名符号对于具有大量参数的函数特别有用,因为它使参数和实际参数之间的关联更加明确和可靠。在位置表示法中,函数调用的参数值按与它们在函数声明中定义的相同的顺序编写。在命名表示法中,参数按名称与功能参数匹配,并且可以按任何顺序编写。 对于每种表示法,还应考虑函数参数类型的影响,如第10.3节所述。

In either notation, parameters that have default values given in the function declaration need not be written in the call at all. But this is particularly useful in named notation, since any combination of parameters can be  omitted; while in positional notation parameters can only be omitted from right to left.

无论使用哪种表示法,在函数声明中具有默认值的参数并不需要在调用时指定。这在命名表示法中尤其有用,因为可以忽略参数的任意组合;而在位置表示法中,只能从右到左忽略。

PostgreSQL also supports mixed notation, which combines positional and named notation. In this case,positional parameters are written first and named parameters appear after them.

PostgreSQL也支持混合表示法,即同时包含位置和命名表示法。此种情形下,位置表示法写于命名表示法之前。

The following examples will illustrate the usage of all three notations, using the following function definition:

以下示例展示三种表示法的用法,使用如下函数定义:

CREATE FUNCTION concat_lower_or_upper(a text, b text, uppercase

boolean DEFAULT false)

RETURNS text

AS

$$

SELECT CASE

WHEN $3 THEN UPPER($1 || ' ' || $2)

ELSE LOWER($1 || ' ' || $2)

END;

$$

LANGUAGE SQL IMMUTABLE STRICT;

Function concat_lower_or_upper has two mandatory parameters, a and b. Additionally there is one optional parameter uppercase which defaults to false. The a and b inputs will be concatenated,and forced to either upper or lower case depending on the uppercase parameter. The remaining details of this function definition are not important here (see Chapter 38 for more information).

函数concat_lower_or_upper有两个参数,a和b。还有一个可选参数uppercase,其默认值为false。输入的a和b会连接到一起,并依据uppercase不同的值返回大写或小写。函数定义的其他细节在此处先暂不探讨(详见第38章)。

发布了341 篇原创文章 · 获赞 53 · 访问量 88万+

猜你喜欢

转载自blog.csdn.net/ghostliming/article/details/104307452