PostgreSQL 的VARIDIC 学习

开始

VARIDIC,可以使用变参。例子:

postgres=# CREATE or replace FUNCTION gao_concat(VARIADIC param_args text[]) RETURNS text AS
$$
  SELECT array_to_string($1,'*');
$$
LANGUAGE SQL;
CREATE FUNCTION
postgres=# 
postgres=# SELECT gao_concat('My', 'dog', 'likes', 'chocolate') As result;
         result         
------------------------
 My*dog*likes*chocolate
(1 row)

postgres=# 

再看PostgreSQL 的官方例子:

CREATE FUNCTION mleast(VARIADIC numeric[]) RETURNS numeric AS $$
    SELECT min($1[i]) FROM generate_subscripts($1, 1) g(i);
$$ LANGUAGE SQL;

SELECT mleast(10, -1, 5, 4.4);
 mleast 
--------
     -1
(1 row)

变参数对数组是不适用的,需要把数组也声明为 variadic:

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

postgres=# select mleast(array[10.1,3,4,1.5,6.2]);
ERROR:  function mleast(numeric[]) does not exist
LINE 1: select mleast(array[10.1,3,4,1.5,6.2]);
               ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.
postgres=# select mleast( variadic array[10.1,3,4,1.5,6.2]);
 mleast 
--------
    1.5
(1 row)

postgres=# 

结束

原文地址:https://www.cnblogs.com/gaojian/p/2762510.html