VARIADIC模式
最近更新时间: 2025-02-18 16:02:00
VARIADICS模式是参数个数可变模式,系统用一个数组对传入的参数进行处理,VARIADIC参数必需是所有最后一个声明,如下所示:
postgres=# CREATE OR REPLACE FUNCTION f1(VARIADIC a_int integer[]) RETURNS void AS
postgres-# $$
postgres$# BEGIN
postgres$# RAISE NOTICE 'a_int = %',a_int;
postgres$# RAISE NOTICE 'a_int[1] = %',a_int[1];
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# SELECT f1(1);
NOTICE: a_int = {1}
NOTICE: a_int[1] = 1
f1
----
(1 行记录)
postgres=# SELECT f1(1,2);
NOTICE: a_int = {1,2}
NOTICE: a_int[1] = 1
f1
----
(1 行记录)
postgres=# CREATE OR REPLACE FUNCTION f1(a_xm TEXT,VARIADIC a_int integer[]) RETURNS void AS
postgres-# $$
postgres$# BEGIN
postgres$# RAISE NOTICE 'a_int = %',a_int;
postgres$# RAISE NOTICE 'a_int[1] = %',a_int[1];
postgres$# RAISE NOTICE 'a_xm = %',a_xm;
postgres$# END;
postgres$# $$
postgres-# LANGUAGE PLPGSQL;
CREATE FUNCTION
postgres=#
postgres=# SELECT f1('TDSQL PG',1,2);
NOTICE: a_int = {1,2}
NOTICE: a_int[1] = 1
NOTICE: a_xm = TDSQL PG
f1
----
(1 行记录)