Aliases for Function Parameters
4607 단어 parameter
Parameters passed to functions are named with the identifiers
$1
, $2
, etc. Optionally, aliases can be declared for $n
parameter names for increased readability. Either the alias or the numeric identifier can then be used to refer to the parameter value. There are two ways to create an alias. The preferred way is to give a name to the parameter in the
CREATE FUNCTION
command, for example: CREATE FUNCTION sales_tax(subtotal real) RETURNS real AS $$
BEGIN
RETURN subtotal * 0.06;
END;
$$ LANGUAGE plpgsql;
The other way, which was the only way available before PostgreSQL 8.0, is to explicitly declare an alias, using the declaration syntax
name ALIAS FOR $n;
The same example in this style looks like
CREATE FUNCTION sales_tax(real) RETURNS real AS $$
DECLARE
subtotal ALIAS FOR $1;
BEGIN
RETURN subtotal * 0.06;
END;
$$ LANGUAGE plpgsql;
Some more examples:
CREATE FUNCTION instr(varchar, integer) RETURNS integer AS $$
DECLARE
v_string ALIAS FOR $1;
index ALIAS FOR $2;
BEGIN
-- some computations using v_string and index here
END;
$$ LANGUAGE plpgsql;
CREATE FUNCTION concat_selected_fields(in_t sometablename)
RETURNS text AS $$
BEGIN
RETURN in_t.f1 || in_t.f3 || in_t.f5 || in_t.f7;
END;
$$ LANGUAGE plpgsql;
When a PL/pgSQL function is declared with output parameters, the output parameters are given
$n
names and optional aliases in just the same way as the normal input parameters. An output parameter is effectively a variable that starts out NULL; it should be assigned to during the execution of the function. The final value of the parameter is what is returned. For instance, the sales-tax example could also be done this way: CREATE FUNCTION sales_tax(subtotal real, OUT tax real) AS $$
BEGIN
tax := subtotal * 0.06;
END;
$$ LANGUAGE plpgsql;
Notice that we omitted
RETURNS real
---we could have included it, but it would be redundant. Output parameters are most useful when returning multiple values. A trivial example is:
CREATE FUNCTION sum_n_product(x int, y int, OUT sum int, OUT
prod int) AS $$
BEGIN
sum := x + y;
prod := x * y;
END;
$$ LANGUAGE plpgsql;
As discussed in section 5.4.3 Functions with Output Parameters , this effectively creates an anonymous record type for the function's results. If a
RETURNS
clause is given, it must say RETURNS record
. When the return type of a PL/pgSQL function is declared as a polymorphic type (
anyelement
or anyarray
), a special parameter $0
is created. Its data type is the actual return type of the function, as deduced from the actual input types (see section 5.2.5 Polymorphic Types ). This allows the function to access its actual return type as shown in section 9.4.2 Copying Types . $0
is initialized to null and can be modified by the function, so it can be used to hold the return value if desired, though that is not required. $0
can also be given an alias. For example, this function works on any data type that has a +
operator: CREATE FUNCTION add_three_values(v1 anyelement, v2
anyelement, v3 anyelement)
RETURNS anyelement AS $$
DECLARE
result ALIAS FOR $0;
BEGIN
result := v1 + v2 + v3;
RETURN result;
END;
$$ LANGUAGE plpgsql;
The same effect can be had by declaring one or more output parameters as
anyelement
or anyarray
. In this case the special $0
parameter is not used; the output parameters themselves serve the same purpose. For example: CREATE FUNCTION add_three_values(v1 anyelement, v2
anyelement, v3 anyelement,
OUT sum anyelement)
AS $$
BEGIN
sum := v1 + v2 + v3;
END;
$$ LANGUAGE plpgsql;
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[Python]Function Parameters(2)매개변수 이름 앞에 *을 붙이면 입력값을 전부 모아서 튜플로 만들어 주기 때문에 여러 개의 입력값을 받을 수 있다. 위와 같이 함수를 호출하면 num1, num2에는 각각 1과 6이 전달되고, *args라는 Para...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.